0

我想将 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];
    }
4

3 回答 3

2

你有什么建议我保持国王可以走的路?

这可能不会直接回答您的问题,并且您已经标记了正确答案。但只是为了回答上面的问题。

我不会保留国王可以走的所有位置,而是保留允许走的位置并在运行时计算可能的路线。

对于任何棋子(King、Pawn 等),它可以移动 8 个位置。左,右,上,下,左上,右上,左下,右下。根据工件的类型,您可以控制运动。

对于您可以创建一个ChessPiece类。声明 8 个位置标志,bool可能是标志,这将定义一个棋子可以移动的可能位置。

声明一块可以跳过的块数,例如方向(; 并从 and 驱动类型ChessPiece允许。

- 编辑 -

例如,以下:

//Class that contains the position of the Piece over the Tile
class PiecePosition
{
    //Set the bounds an image/vector can move.
    public int X, Y;

    public PiecePosition(int x, int y) { this.X = x; this.Y = y; }
    public PiecePosition(int x, int y, int width, int height) { this.X = x; this.Y = y; }
}

//Base ChessPeice class that shall be used to drive all types of chess pieces.
//Sixteen pieces: one king, one queen, two rooks, two knights, two bishops, and eight pawns
//http://en.wikipedia.org/wiki/Chess
abstract class ChessPiece
{
    public object Image;//This is an "optional" object that contains the Picture of the Peice, 
    //alternatively, it may contain vector of the image that you want 
    //to draw. Right now this is object is here just for the sake of 
    //understanding that you can use this object here to Draw() it
    //based upon its position.

    //Possible movements of the unhindered piece=8
    protected const int MaxDirectionsCount = 8;

    public enum PieceType { King, Pawn, SomeOtherType }//Types of chess peice.
    public enum Moves { Up, Down, Left, Right, TopLeft, Etc }//Possible positions a piece can move

    protected PieceType Type; //Contains type of piece
    protected Moves MoveableDirections;//Shall contain the allowable directions

    public List<PiecePosition> listPositions;//List of possible positions to be calculated during runtime

    //Defines a piece can skip
    protected int SkippableBlocks;

    public abstract void PossiblePositions(PiecePosition CurrentPosition);//Calculates possible positions
    public abstract void Draw();//Draws the piece

}

//The King Chess piece
//http://en.wikipedia.org/wiki/King_%28chess%29
class King : ChessPiece
{
    //Constructor that sets the type of piece
    public King()
    {
        //Set the directions a King can move.
        base.MoveableDirections = Moves.Down | Moves.Left | Moves.Right;
        base.Type = PieceType.King;
        SkippableBlocks = 1; //Max a king can move is one block in the base.Directions set above.

    }

    //Calculates possible available positions to move to, during runtime; based upon current position.
    public override void PossiblePositions(PiecePosition CurrentPosition)
    {
        //Calculate position
        //Since you know this is king piece, you can calculate the possible positions
        //And add that the list of possible positions.
        //For instance, a King can move 
        int X = 0; int Y = 0;
        for (int i = 0; i < MaxDirectionsCount; i++)
        {
            //Calculate directions.
            if (base.MoveableDirections == Moves.Down) { X = CurrentPosition.X - 1; Y = CurrentPosition.Y; }
            if (base.MoveableDirections == Moves.Up) { X = CurrentPosition.X + 1; Y = CurrentPosition.Y; }

            //Rest of the directions go here...
            //...Btw, what would you do for cross directions? 
            //One way could be to pass a Rectangle in the ChessTile(x,y,width,height) constructor

            //Add to list of possible directions.
            listPositions.Add(new PiecePosition(X, Y));

        }
    }

    public override void Draw()
    {
        //You can actually draw/redraw using the Image object
        //based upon the current/moved position.
    }



}

顺便说一句,如果您刚刚开始编写代码,我建议您停止。首先四处寻找 Chess 类的设计,看看你是否想从Chess Objects中获得意义。例如,ChessBoard、Game、Players、Piece、Moves、AllowablePositions 等。

查看与 Chess /Google abit 相关的问题,看看问题/答案和您的逻辑是否已经内联。

于 2010-10-30T16:31:12.920 回答
1

int[,][]声明一个包含 int 的二维数组的一维数组。那是你要的吗?

Kingmoves 可以简单地计算为:

IEnumerable<Position> ValidKingTargets(Position p)
{
  int top=Math.Max(0,y-1);
  int left=Math.Max(0,x-1);
  int bottom=Math.Min(8,y+2);
  int right=Math.Min(8,x+2);
  for(int y=top;y<bottom;y++)
    for(int x=left;x<right;x++)
      if(x!=p.X || y!=p.Y)
        yield return new Position(x,y);
}
于 2010-10-30T15:04:54.987 回答
1

大批?

确定国王可能的移动是相当容易的。

class position { public int x, y }

...

public ArrayList<position> KingPossibleMove(position current)
{
    var list = new ArrayList();
    if (current.x>0) {
        list.add(new position() { x= current.x - 1, y = current.y });
    if (current.x<8) {
        list.add(new position() { x= current.x + 1, y = current.y });
    // The rest follows, try to determine if the move is within bound
    // you can also look for if the move will cause immediate checkmate.

    return list;
} 
于 2010-10-30T15:11:34.857 回答