Beispiel #1
0
        public Boolean makeMove(Move move)
        {
            if (move == null)
                return false;
            if (!isValidSquare(move.To) || !isValidSquare(move.From))
                return false;

            Piece from = this.getPiece(move.From);
            if (from == null)
                return false;
            if (from.PieceColor != CurrentColor)
                return false;

            Piece to = this.getPiece(move.To);
            if (to != null && to.PieceColor == CurrentColor)
                return false;

            if (from.isValidMove(move)) {
                this.board[move.To.x, move.To.y] = from;
                this.board[move.From.x, move.From.y] = null;
                PreviousMove lastMove = new PreviousMove();
                lastMove.Move = move;
                lastMove.FromPiece = from;
                lastMove.ToPiece = to;
                lastMove.CurrentState = this.State;
                lastMove.CurrentColor = this.CurrentColor;
                lastMove.CurrentTurn = this.CurrentTurn;
                lastMove.CurrentHash = this.Hash;
                this.lastMoves.Push(lastMove);

                if (from.PieceType == Piece.Type.p) {
                    if (from.PieceColor == Piece.Color.Black) {
                        if (move.To.x == HEIGHT - 1) {
                            Piece nq = new Piece(Piece.Type.q, from.PieceColor);
                            this.board[move.To.x, move.To.y] = nq;
                        }
                    }
                    else {
                        if (move.To.x == 0) {
                            Piece nq = new Piece(Piece.Type.q, from.PieceColor);
                            this.board[move.To.x, move.To.y] = nq;
                        }
                    }
                }

                this.Hash = 0;

                if (to != null && to.PieceType == Piece.Type.k)
                    this.State = from.PieceColor == Piece.Color.Black ? BoardState.BlackWinner : BoardState.WhiteWinner;
                else {
                    this.CurrentColor = this.CurrentColor == Piece.Color.Black ? Piece.Color.White : Piece.Color.Black;
                    if (this.CurrentColor == Piece.Color.White)
                        ++this.CurrentTurn;
                    if (CurrentTurn > 40)
                        this.State = BoardState.Draw;
                }

                return true;
            }

            return false;
        }
Beispiel #2
0
 public Boolean setPiece(Square square, Piece piece)
 {
     if (isValidSquare(square)) {
         this.board[square.x, square.y] = piece;
         return true;
     }
     return false;
 }
Beispiel #3
0
        public List<Move> getMoves(Piece.Color c)
        {
            List<Move> list = MoveCache.getValueByHash(this.Hash);
            if (list == null) {
                list = new List<Move>();
                for (int x = 0; x < HEIGHT; ++x) {
                    for (int y = 0; y < WIDTH; ++y) {
                        Piece p = board[x, y];
                        if (p != null && p.PieceColor == c) {
                            list.AddRange(getPieceMoves(new Square(x, y)));
                        }
                    }
                }
                MoveCache.storeValue(this.Hash, list);
            }

            if (list.Count < 1)
                this.State = CurrentColor == Piece.Color.Black ? BoardState.WhiteWinner : BoardState.BlackWinner;
            return list;
        }