Beispiel #1
0
 public void DrawFigures(Figure.ColorEnum color)
 {
     if (color == Figure.ColorEnum.Black)
     {
         selectRook.BackgroundImage   = Resources.BlackRook;
         selectKnight.BackgroundImage = Resources.BlackKnight;
         selectBishop.BackgroundImage = Resources.BlackBishop;
         selectQueen.BackgroundImage  = Resources.BlackQueen;
     }
 }
Beispiel #2
0
 public bool FigureIsOnCell(Figure.TypeEnum type, Figure.ColorEnum color, int col, int row) //checks if there is a figure(figure, color) on cell(col, row)
 {
     if (type == Figure.TypeEnum.Rook || type == Figure.TypeEnum.Bishop)
     {
         if (CellExists(col, row) &&
             board[col, row].figure != null &&
             (board[col, row].figure.type == type || board[col, row].figure.type == Figure.TypeEnum.Queen) &&
             board[col, row].figure.color == color)
         {
             return(true);
         }
     }
     else if (CellExists(col, row) &&
              board[col, row].figure != null &&
              board[col, row].figure.type == type &&
              board[col, row].figure.color == color)
     {
         return(true);
     }
     return(false);
 }
Beispiel #3
0
        public void BoardSetup() //Sets the board up
        {
            form.ResetTime();
            form.firstMove = true;
            form.TimerStart();

            BlackKingPosition[0] = 5;
            BlackKingPosition[1] = 8;
            WhiteKingPosition[0] = 5;
            WhiteKingPosition[1] = 1;

            moveCount      = 0;
            GUI_Count.Text = "Liczba ruchów:" + Environment.NewLine + moveCount;

            for (int iCol = 1; iCol <= 9; iCol++)
            {
                for (int iRow = 1; iRow <= 9; iRow++)
                {
                    board[iCol, iRow].figure     = null;
                    boardCopy[iCol, iRow].figure = null;
                }
            }

            foreach (Figure figure in figures)
            {
                figure.firstMove = true;
            }
            currentColor = Figure.ColorEnum.White;

            for (int i = 1; i <= 8; i++)
            {
                //Setting up pawns
                board[i, 2].figure = figures[i];
                board[i, 7].figure = figures[i + 16];

                //Setting up other figures
                board[i, 1].figure = figures[i + 8];
                board[i, 8].figure = figures[i + 24];
            }
        }
Beispiel #4
0
        public bool CellIsAttacked(int col, int row, Figure.ColorEnum color) //checks if a figure of color(color) is or will be attacked on cell(col, row)
        {
            if (color == Figure.ColorEnum.White)
            {
                color = Figure.ColorEnum.Black;
            }
            else
            {
                color = Figure.ColorEnum.White;
            }

            if (CellExists(col, row))
            {
                //Checking if opponents pawn checks
                if (color == Figure.ColorEnum.White)
                {
                    if (FigureIsOnCell(Figure.TypeEnum.Pawn, color, col + 1, row - 1))
                    {
                        return(true);
                    }
                    if (FigureIsOnCell(Figure.TypeEnum.Pawn, color, col - 1, row - 1))
                    {
                        return(true);
                    }
                }
                else
                {
                    if (FigureIsOnCell(Figure.TypeEnum.Pawn, color, col + 1, row + 1))
                    {
                        return(true);
                    }
                    if (FigureIsOnCell(Figure.TypeEnum.Pawn, color, col - 1, row + 1))
                    {
                        return(true);
                    }
                }

                //Checking if opponents knight ckecks
                if (FigureIsOnCell(Figure.TypeEnum.Knight, color, col + 2, row + 1))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.Knight, color, col + 2, row - 1))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.Knight, color, col + 1, row + 2))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.Knight, color, col + 1, row - 2))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.Knight, color, col - 1, row + 2))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.Knight, color, col - 1, row - 2))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.Knight, color, col - 2, row + 1))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.Knight, color, col - 2, row - 1))
                {
                    return(true);
                }

                //Checking if opponents bishop or queen checks
                for (int i = 1; i <= 8; i++)
                {
                    if (CellExists(col + i, row + i) && !CellIsEmpty(col + i, row + i))
                    {
                        if (FigureIsOnCell(Figure.TypeEnum.Bishop, color, col + i, row + i))
                        {
                            return(true);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                for (int i = 1; i <= 8; i++)
                {
                    if (CellExists(col - i, row + i) && !CellIsEmpty(col - i, row + i))
                    {
                        if (FigureIsOnCell(Figure.TypeEnum.Bishop, color, col - i, row + i))
                        {
                            return(true);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                for (int i = 1; i <= 8; i++)
                {
                    if (CellExists(col + i, row - i) && !CellIsEmpty(col + i, row - i))
                    {
                        if (FigureIsOnCell(Figure.TypeEnum.Bishop, color, col + i, row - i))
                        {
                            return(true);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                for (int i = 1; i <= 8; i++)
                {
                    if (CellExists(col - i, row - i) && !CellIsEmpty(col - i, row - i))
                    {
                        if (FigureIsOnCell(Figure.TypeEnum.Bishop, color, col - i, row - i))
                        {
                            return(true);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                //Checking if opponents rook or queen checks
                for (int i = 1; i <= 8; i++)
                {
                    if (CellExists(col + i, row) && !CellIsEmpty(col + i, row))
                    {
                        if (FigureIsOnCell(Figure.TypeEnum.Rook, color, col + i, row))
                        {
                            return(true);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                for (int i = 1; i <= 8; i++)
                {
                    if (CellExists(col, row + i) && !CellIsEmpty(col, row + i))
                    {
                        if (FigureIsOnCell(Figure.TypeEnum.Rook, color, col, row + i))
                        {
                            return(true);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                for (int i = 1; i <= 8; i++)
                {
                    if (CellExists(col - i, row) && !CellIsEmpty(col - i, row))
                    {
                        if (FigureIsOnCell(Figure.TypeEnum.Rook, color, col - i, row))
                        {
                            return(true);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                for (int i = 1; i <= 8; i++)
                {
                    if (CellExists(col, row - i) && !CellIsEmpty(col, row - i))
                    {
                        if (FigureIsOnCell(Figure.TypeEnum.Rook, color, col, row - i))
                        {
                            return(true);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                //Checking if opponents king checks
                if (FigureIsOnCell(Figure.TypeEnum.King, color, col + 1, row + 1))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.King, color, col + 1, row - 1))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.King, color, col + 1, row))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.King, color, col - 1, row + 1))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.King, color, col - 1, row - 1))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.King, color, col - 1, row))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.King, color, col, row + 1))
                {
                    return(true);
                }
                if (FigureIsOnCell(Figure.TypeEnum.King, color, col, row - 1))
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #5
0
        public void SelectCell(int selectedCol, int selectedRow) //Called after clicking on a pictureBox of (selectedCol, selectedRow) cell
        {
            if (board[selectedCol, selectedRow].figure != null &&
                board[selectedCol, selectedRow].figure.color == currentColor)     //checks move ability
            {                                                                     //if there is an alllied figure on a clicked cell
                currentSelection[0] = selectedCol;
                currentSelection[1] = selectedRow;
                GetMoveAbility(selectedCol, selectedRow);
                DrawMoveAbility();
            }
            else if (moveAbility[selectedCol, selectedRow] == "Yes" ||
                     moveAbility[selectedCol, selectedRow] == "Attack")           //check if move on a clicked cell is avalible
            {                                                                     //and move the figure
                MoveFigure(currentSelection[0], currentSelection[1], selectedCol, selectedRow);
                DrawFigures();
                ResetMoveAbility();
                //form.fileCounter++;
                form.boardEncode(null, null);

                bool stalemate = true;

                if (currentColor == Figure.ColorEnum.White)
                {
                    currentColor   = Figure.ColorEnum.Black;
                    GUI_Color.Text = "Ruch:" + Environment.NewLine + "Czarnych";

                    whiteDownside = false;
                    form.RotateBoard(null, null);

                    checkingMate = true;

                    for (int iCol = 1; iCol <= 8; iCol++) //checking if there are any possible moves
                    {
                        for (int iRow = 1; iRow <= 8; iRow++)
                        {
                            if (!CellIsEmpty(iCol, iRow) &&// && board[iCol, iRow].figure != null
                                board[iCol, iRow].figure.color == Figure.ColorEnum.Black)
                            {
                                checkingMateFrom[0] = iCol;
                                checkingMateFrom[1] = iRow;

                                Debug.WriteLine("Czarnego " + board[iCol, iRow].figure.type + " na polu " + iCol + " " + iRow);
                                GetMoveAbility(iCol, iRow);
                                moveAbility[WhiteKingPosition[0], WhiteKingPosition[1]] = "No";

                                for (int jCol = 1; jCol <= 8; jCol++)
                                {
                                    for (int jRow = 1; jRow <= 8; jRow++)
                                    {
                                        if (moveAbility[jCol, jRow] == "Yes" || moveAbility[jCol, jRow] == "Attack")
                                        {
                                            Debug.WriteLine("Czarny " + board[iCol, iRow].figure.type + " moze sie ruszyc na " + jCol + " " + jRow + " " + moveAbility[jCol, jRow]);
                                            stalemate = false;
                                            break;
                                        }
                                    }
                                    if (!stalemate)
                                    {
                                        break;
                                    }
                                }
                            }
                            if (!stalemate)
                            {
                                break;
                            }
                        }
                        if (!stalemate)
                        {
                            break;
                        }
                    }
                    if (stalemate)
                    {
                        Debug.WriteLine("Pat lub mat");
                    }
                    checkingMate = false;

                    ResetMoveAbility();
                    DrawMoveAbility();
                }
                else
                {
                    currentColor   = Figure.ColorEnum.White;
                    GUI_Color.Text = "Ruch:" + Environment.NewLine + "Białych";

                    whiteDownside = true;
                    form.RotateBoard(null, null);

                    checkingMate = true;

                    for (int iCol = 1; iCol <= 8; iCol++) //checking if there are any possible moves
                    {
                        for (int iRow = 1; iRow <= 8; iRow++)
                        {
                            if (!CellIsEmpty(iCol, iRow) && board[iCol, iRow].figure != null &&
                                board[iCol, iRow].figure.color == Figure.ColorEnum.White)
                            {
                                checkingMateFrom[0] = iCol;
                                checkingMateFrom[1] = iRow;

                                Debug.WriteLine("Sprawdzam bialego " + board[iCol, iRow].figure.type + " na polu " + iCol + " " + iRow);
                                GetMoveAbility(iCol, iRow);
                                moveAbility[WhiteKingPosition[0], WhiteKingPosition[1]] = "No";

                                for (int jCol = 1; jCol <= 8; jCol++)
                                {
                                    for (int jRow = 1; jRow <= 8; jRow++)
                                    {
                                        if (moveAbility[jCol, jRow] == "Yes" || moveAbility[jCol, jRow] == "Attack")
                                        {
                                            Debug.WriteLine("Bialy " + board[iCol, iRow].figure.type + " moze sie ruszyc na " + jCol + " " + jRow + " " + moveAbility[jCol, jRow]);
                                            stalemate = false;
                                            break;
                                        }
                                    }
                                    if (!stalemate)
                                    {
                                        break;
                                    }
                                }
                            }
                            if (!stalemate)
                            {
                                break;
                            }
                        }
                        if (!stalemate)
                        {
                            break;
                        }
                    }
                    if (stalemate)
                    {
                        Debug.WriteLine("Pat lub mat");
                    }
                    checkingMate = false;

                    ResetMoveAbility();
                    DrawMoveAbility();
                }

                if (currentColor == Figure.ColorEnum.Black)
                {
                    moveCount++;
                }
                GUI_Count.Text = "Liczba Ruchów:" + Environment.NewLine + moveCount;



                if (CellIsAttacked(WhiteKingPosition[0], WhiteKingPosition[1], Figure.ColorEnum.White)) //checks if the white king is checked
                {
                    PictureBox pb = (PictureBox)form.Controls.Find("c" + WhiteKingPosition[0].ToString() + WhiteKingPosition[1].ToString(), false)[0];
                    pb.BackgroundImage = Resources.CellEnemy; //marking checked king
                    Debug.WriteLine("White Check!!!");


                    if (stalemate) //checking if it's a check mate
                    {
                        form.TimerStop();
                        if (form.whitePlayer == 1)
                        {
                            Debug.WriteLine("Gracz 2 zwyciezyl czarnymi");
                            form.player2Win();
                        }
                        else
                        {
                            Debug.WriteLine("Gracz 1 zwyciezyl czarnymi");
                            form.player1Win();
                        }
                        Debug.WriteLine("Szach mat, czarne wygrywaja");
                        MessageBox.Show("Szach i mat\nCzarne wygrywaja w " + moveCount + " ruchach", "Szach mat", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        stalemate = false;
                    }
                }

                if (CellIsAttacked(BlackKingPosition[0], BlackKingPosition[1], Figure.ColorEnum.Black)) //checks if the black king is checked
                {
                    PictureBox pb = (PictureBox)form.Controls.Find("c" + BlackKingPosition[0].ToString() + BlackKingPosition[1].ToString(), false)[0];
                    pb.BackgroundImage = Resources.CellEnemy; //marking checked king
                    Debug.WriteLine("Black Check!!!");

                    if (stalemate)  //checking if it's a check mate
                    {
                        form.TimerStop();
                        if (form.whitePlayer == 1)
                        {
                            Debug.WriteLine("Gracz 1 zwyciezyl bialymi");
                            form.player1Win();
                        }
                        else
                        {
                            Debug.WriteLine("Gracz 2 zwyciezyl białymi");
                            form.player2Win();
                        }
                        Debug.WriteLine("Szach mat, białe wygrywaja");
                        MessageBox.Show("Szach i mat\nBiałe wygrywaja w " + moveCount + " ruchach", "Szach mat", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        stalemate = false;
                    }
                }

                if (stalemate && currentColor == Figure.ColorEnum.White)
                {
                    form.TimerStop();
                    form.draw();
                    Debug.WriteLine("PAT");
                    MessageBox.Show("Pat\nBiałe nie mają możliwości ruchu", "Pat", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (stalemate)
                {
                    form.TimerStop();
                    form.draw();
                    Debug.WriteLine("PAT");
                    MessageBox.Show("Pat\nCzarne nie mają możliwości ruchu", "Pat", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }