将数组保存在数组中
我想将 kingarray[x1+1,y1-1],king array[x1+1,y1] 等保存在一个数组中(国际象棋游戏中国王的走法)。我该怎么做?或者如果不是可能你建议我怎样保持国王可以走的路?谢谢
int[,] kingarray = new int[8, 8];
for (i = 0; i < 1; i++)
{
return kingarray[x1 + 1, y1];
}
for (i = 1; i > 0; i--)
{
return kingarray[x1 - 1, y1];
}
for (j = 0; j < 1; j++)
{
return kingarray[x1, y1 + 1];
}
for (j = 1; j > 0; j--)
{
return kingarray[x1, y1 - 1];
}
for (i = 0; i < 1; i++)
for (j = 1; j > 0; j--)
{
return kingarray[x1 + 1, y1 - 1];
}
for (i = 1; i > 0; i--)
for (j = 0; j < 1; j++)
{
return kingarray[x1 - 1, y1 + 1];
}
for (i = 0; i < 1; i++)
for (j = 0; j < 1; j++)
{
return kingarray[x1 + 1, y1 + 1];
}
for (i = 1; i > 0; i--)
for (j = 1; j > 0; j--)
{
return kingarray[x1 - 1, y1 - 1];
}
i want to save kingarray[x1+1,y1-1],king array[x1+1,y1],etc in an array(ways that king in chess game can go).how can i do it?or if its not possible what do you suggest me to keep the ways that king can go?thanks
int[,] kingarray = new int[8, 8];
for (i = 0; i < 1; i++)
{
return kingarray[x1 + 1, y1];
}
for (i = 1; i > 0; i--)
{
return kingarray[x1 - 1, y1];
}
for (j = 0; j < 1; j++)
{
return kingarray[x1, y1 + 1];
}
for (j = 1; j > 0; j--)
{
return kingarray[x1, y1 - 1];
}
for (i = 0; i < 1; i++)
for (j = 1; j > 0; j--)
{
return kingarray[x1 + 1, y1 - 1];
}
for (i = 1; i > 0; i--)
for (j = 0; j < 1; j++)
{
return kingarray[x1 - 1, y1 + 1];
}
for (i = 0; i < 1; i++)
for (j = 0; j < 1; j++)
{
return kingarray[x1 + 1, y1 + 1];
}
for (i = 1; i > 0; i--)
for (j = 1; j > 0; j--)
{
return kingarray[x1 - 1, y1 - 1];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能无法直接回答您的问题,并且您已经标记了正确答案。但只是回答上面的问题。
我不会保留国王可以走的所有位置,而是保留允许其走的位置并计算运行时可能的路线。
对于任何棋子(K、Pawn 等),有 8 个可以移动的位置。左、右、上、下、左上、右上、左下、右下。根据作品的类型,您可以控制运动。
您可以创建一个
ChessPiece
类。声明 8 个位置标志,可能是bool
标志,这将定义棋子可以移动的可能位置。声明一个棋子可以跳过的块数,例如 Directions(;并驱动
ChessPiece
中的类型并允许。--EDIT--
例如,以下:
Btw ,如果您刚刚开始编写代码,我建议您先停下来看看 Chess 类设计,看看您是否想要理解象棋对象,例如 ChessBoard、Game、Players。 、棋子、移动、允许位置等。
查看与国际象棋相关的问题/Google abit,看看问题是否存在/answers 并且您的逻辑已经内联。
This may not answer your question directly, and that you have already marked the correct answer. But just to answer the above.
Rather than keeping the all the positions a king can go, I would keep the positions it is allowed to go and calculate the possible routes during run-time.
For any piece(King, Pawn, etc), there are 8 places that it can move. Left, right, up, down, top-left, top-right, bottom-left, bottom-right. Based upon the type of piece, you can control the movement.
For You can create a
ChessPiece
class. Declare 8 positional flags,bool
flags probably, that would define the possible positions that a piece can move.Declare the number of blocks a piece can skip, for instance directions(; and drive the types from the
ChessPiece
and allow.--EDIT--
For instance, following:
Btw, if you just started writing the code, I would suggest you stop. Look around for Chess class designs first, and see if you want make sense out of Chess Objects. For instance, ChessBoard, Game, Players, Piece, Movements, AllowablePositions, etc.
Take a look at questions related to Chess/Google abit, and see if the questions/answers and your logic is already inline.
大批?
确定国王可能的行动是相当容易的。
Array?
It is rather easy to determine a King's possible movement.
int[,][]
声明一个包含 int 二维数组的一维数组。这就是你想要的吗?kingmoves 可以简单地计算为:
int[,][]
declares a 1D array containing a 2D array of int. Is that what you want?And the kingmoves can simply be calculated as: