搜索数组 [英] searching an array

查看:48
本文介绍了搜索数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个模拟国际象棋的程序,以便棋子X位于2D阵列中,并且如果它们能够移动到X'的位置则测试其他棋子。

展开 | 选择 < span class =codeDivider> | Wrap | 行号

解决方案

您是否必须使用特定的作品进行搜索 - 在换句话说,你是否需要搜索X片,或者你可以在某个位置搜索片段吗?


我最近用Java写了一个国际象棋游戏,我使用的功能检查过如果一个位置受到威胁而不是一块 - 它会绕过每个可能的位置并检查那里的那块是否可以在第一个位置捕获一块。


例如。在一个8x8阵列中我有件如


king(3,4)

queen(1,2)

rook (2,3)

x(4,4)


来自我上面的代码,我可以将这些部分输入到数组中。但现在我必须看看这些作品是否能够根据每件作品的限制移动到(4,4)这是X'的位置。


你可以使用表示每个棋子的类 - 在类中,有两个实例变量在棋盘中保存棋子的x和y位置 - 类似于:

展开 | 选择 | Wrap | 行号

I have to write a program that simulates chess such that a chess piece X is located in a 2D array and other pieces are tested if they are capable of moving to X''s position.

Expand|Select|Wrap|Line Numbers

解决方案

Do you have to use the specific piece to search for - in other words, do you have to search for piece X, or could you search for a piece in a Location?

I recently wrote a chess game in Java, and the function I used checked if a location was threatened rather than a piece - it looped through every possible location and checked if the piece there could capture a piece at the first location.


well for example. in a 8x8 array i have pieces such as

king (3,4)
queen(1,2)
rook(2,3)
x(4,4)

from the code i have above, I am able to input these pieces into the array. But now I have to see if those piece will be able to move to (4,4) which is X''s position based on each piece''s limitations.


You could use a class to represent each chess piece - in the class, have two instance variables holding the piece''s x and y position in the board - something like:

Expand|Select|Wrap|Line Numbers


这篇关于搜索数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆