tags:

views:

85

answers:

3

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];
    }
+1  A: 

int[,][] declares a 1D array containing a 2D array of int. Is that what you want?

And the kingmoves can simply be calculated as:

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);
}
CodeInChaos
can you explain your code alittle?
arash
first I calculate the borders of the 3x3 square containing the king (goes from x-1 to x+1 and y-1 to +1 then I exclude the field where the king currently is. This doesn't take castling into account.
CodeInChaos
+1  A: 

Array?

It is rather easy to determine a King's possible movement.

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;
} 
xandy
your coordinates go from 0 to 8 ;-)
BrokenGlass
sorry, I don't play chess much :P
xandy
i understood this.thanks
arash
+2  A: 

what do you suggest me to keep the ways that king can go?

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:

//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.
    }



}

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.

KMan
wow,thanks very much
arash
but its alittle hard for me and i need your help to completely understand your code,1.what does public object Image do?
arash
`Image` is an optional object that contains the picture or vector of the Piece; that you might want Drawn or Redrawn. Btw, I have updated the code with comments.
KMan
@KMan: Please do not mix domain concerns with UI concerns!
Jason
thanks so much.if i had question i ask here later.thanks
arash