Exemplo n.º 1
0
        public void ResetBoard()
        {
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    chessboard[i, j] = new cell();
                }
            }

            chessboard[0, 0].state = cell.status.BlackRook;
            chessboard[0, 1].state = cell.status.BlackKnight;
            chessboard[0, 2].state = cell.status.BlackBishop;
            chessboard[0, 3].state = cell.status.BlackQueen;
            chessboard[0, 4].state = cell.status.BlackKing;
            chessboard[0, 5].state = cell.status.BlackBishop;
            chessboard[0, 6].state = cell.status.BlackKnight;
            chessboard[0, 7].state = cell.status.BlackRook;

            chessboard[7, 0].state = cell.status.WhiteRook;
            chessboard[7, 1].state = cell.status.WhiteKnight;
            chessboard[7, 2].state = cell.status.WhiteBishop;
            chessboard[7, 3].state = cell.status.WhiteQueen;
            chessboard[7, 4].state = cell.status.WhiteKing;
            chessboard[7, 5].state = cell.status.WhiteBishop;
            chessboard[7, 6].state = cell.status.WhiteKnight;
            chessboard[7, 7].state = cell.status.WhiteRook;

            for (int j = 0; j < 8; j++)
            {
                chessboard[1, j].state = cell.status.BlackPawn;
                chessboard[6, j].state = cell.status.WhitePawn;
            }
            for (int i = 0; i < 6; i++)                // reset castling restrictions
            {
                a1h1e1a8h8e8[i] = true;
            }
            history.Clear();
            white_move = true;
            show();
        }
Exemplo n.º 2
0
        public void move(string coords)
        {
            if (coords.Length != 4)
            {
                Console.WriteLine("Invalid command");
                return;
            }
            int x1, y1, x2, y2;

            x1 = Convert.ToInt32(coords[0] - 'a');
            y1 = 8 - Convert.ToInt32(coords[1] - '0');
            x2 = Convert.ToInt32(coords[2] - 'a');
            y2 = 8 - Convert.ToInt32(coords[3] - '0');
            if (legal_move(x1, y1, x2, y2))
            {
                if (y1 == 0)                         // if rooks or kings moved
                {
                    if (x1 == 0)
                    {
                        a1h1e1a8h8e8[3] = false;
                    }
                    else if (x1 == 4)
                    {
                        a1h1e1a8h8e8[5] = false;
                    }
                    else if (x1 == 7)
                    {
                        a1h1e1a8h8e8[4] = false;
                    }
                }
                if (y1 == 7)
                {
                    if (x1 == 0)
                    {
                        a1h1e1a8h8e8[0] = false;
                    }
                    else if (x1 == 4)
                    {
                        a1h1e1a8h8e8[2] = false;
                    }
                    else if (x1 == 7)
                    {
                        a1h1e1a8h8e8[1] = false;
                    }
                }
                if (y1 == white_king[0] && x1 == white_king[1]) // looking for castling
                {
                    if (x2 - x1 == 2)                           // white 0-0
                    {
                        chessboard[7, 5].state = cell.status.BlackRook;
                        chessboard[7, 7].state = cell.status.empty;
                        a1h1e1a8h8e8[1]        = false;
                    }
                    if (x2 - x1 == -2)                         // white 0-0-0
                    {
                        chessboard[7, 3].state = cell.status.BlackRook;
                        chessboard[7, 0].state = cell.status.empty;
                        a1h1e1a8h8e8[0]        = false;
                    }
                }
                if (y1 == black_king[0] && x1 == black_king[1])
                {
                    if (x2 - x1 == 2)                          // black 0-0
                    {
                        chessboard[0, 5].state = cell.status.WhiteRook;
                        chessboard[0, 7].state = cell.status.empty;
                        a1h1e1a8h8e8[4]        = false;
                    }
                    if (x2 - x1 == -2)                         // black 0-0-0
                    {
                        chessboard[0, 3].state = cell.status.WhiteRook;
                        chessboard[0, 0].state = cell.status.empty;
                        a1h1e1a8h8e8[3]        = false;
                    }
                }

                movement temp = new movement();
                temp.square = coords;
                temp.beaten = chessboard[y2, x2].state;
                history.Add(temp);

                cell temp_cell = new cell();
                temp_cell.state          = chessboard[y1, x1].state;
                chessboard[y2, x2]       = temp_cell;
                chessboard[y1, x1].state = cell.status.empty;

                for (int i = 0; i < 8; i++)                          // searching for the kings
                {
                    for (int j = 0; j < 8; j++)
                    {
                        if (chessboard[i, j].state == cell.status.BlackKing)
                        {
                            black_king[0] = i;
                            black_king[1] = j;
                        }
                        if (chessboard[i, j].state == cell.status.WhiteKing)
                        {
                            white_king[0] = i;
                            white_king[1] = j;
                        }
                    }
                }

                if (white_move)
                {
                    white_move = !white_move;
                    if (under_attack(white_king[0], white_king[1], true))                       // if king is under attack after move, we reverse it
                    {
                        reverse();
                        Console.WriteLine("Illegal move: king under attack");
                        return;
                    }
                }
                else
                {
                    white_move = !white_move;
                    if (under_attack(black_king[0], black_king[1], false))
                    {
                        reverse();
                        Console.WriteLine("Illegal move: king under attack");
                        return;
                    }
                }

                if (y2 == 0 && chessboard[y2, x2].state == cell.status.WhitePawn)                           // white pawn promotion
                {
                    Console.WriteLine("Promote to what? (Queen, Rook, Bishop or Knight)");
                    while (chessboard[y2, x2].state == cell.status.WhitePawn)
                    {
                        string answer = Console.ReadLine().ToLower();
                        switch (answer)
                        {
                        case "queen":
                            chessboard[y2, x2].state = cell.status.WhiteQueen;
                            break;

                        case "rook":
                            chessboard[y2, x2].state = cell.status.WhiteRook;
                            break;

                        case "bishop":
                            chessboard[y2, x2].state = cell.status.WhiteBishop;
                            break;

                        case "knight":
                            chessboard[y2, x2].state = cell.status.WhiteKnight;
                            break;

                        default:
                            Console.WriteLine("No such piece");
                            break;
                        }
                    }
                }
                if (y2 == 7 && chessboard[y2, x2].state == cell.status.BlackPawn)                         // black pawn promotion
                {
                    Console.WriteLine("Promote to what? (Queen, Rook, Bishop or Knight)");
                    while (chessboard[y2, x2].state == cell.status.BlackPawn)
                    {
                        string answer = Console.ReadLine().ToLower();
                        switch (answer)
                        {
                        case "queen":
                            chessboard[y2, x2].state = cell.status.BlackQueen;
                            break;

                        case "rook":
                            chessboard[y2, x2].state = cell.status.BlackRook;
                            break;

                        case "bishop":
                            chessboard[y2, x2].state = cell.status.BlackBishop;
                            break;

                        case "knight":
                            chessboard[y2, x2].state = cell.status.BlackKnight;
                            break;

                        default:
                            Console.WriteLine("No such piece");
                            break;
                        }
                    }
                }

                show();
            }
            else
            {
                Console.WriteLine("Illegal move!");
            }
        }