Пример #1
0
        //Used to select and move White's pieces
        public void whiteTurn(string tile)
        {
            //IF the user tries to move to the same tile as their selected piece, (i.e double clicks the tile)
            //      Deselect the piece and re-enable the board
            if (selectedTile == tile)
            {
                setBoard(true);
                hasSelectedPiece = false;
                selectedTile = "";
            }

            //ELSE IF the user has not selected an empty tile OR has selected a piece,
            else if (board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))] != "" || hasSelectedPiece)
            {
                White accWhite = new White(hasSelectedPiece, tile, board, piece, selectedTile, takenBlackPieces);
                ScoreBoard accScore = new ScoreBoard();

                //Does:
                //  SETS integral variables that need to be accessed later
                //  Moves pieces
                //      Changes the appropriate values in the board array to simulate movement
                accWhite.action();

                //Used to hold the list of possible moves generated from the White class.
                ArrayList list = new ArrayList();

                //Generate a list of possible moves based on the selected piece and its location
                list = (ArrayList)accWhite.generateListOfPossibleMoves().Clone();

                //GET the selected tile
                selectedTile = accWhite.getSelectedTile();

                //IF the user has not already selected a piece,
                //      Enable all tiles that can be moved to and disable the rest
                //          SET hasSelectedPiece to true to reflect that a piece is now selected
                if (!hasSelectedPiece && accWhite.getTile() != "")
                {
                    setBoard(false);
                    activateButtons(list);
                    hasSelectedPiece = true;
                }

                //ELSE IF the user has already selected a piece,
                //      Enable all tiles
                //      Deselect the piece
                //      SET isWhiteTurn to false to allow for Black's turn.
                else
                {
                    setBoard(true);
                    hasSelectedPiece = false;
                    selectedTile = "";
                    isWhiteTurn = false;
                }

                //SET the local board array to the White class' updated board array
                updateBoard(accWhite.getBoard());

                //Check for and promote pawns that have reached the opposing side of the board
                checkForPawns();

                //UPDATE the visuals to reflect the changes in the board
                refreshBoard();

                //SET the local takenBlackPieces array to the White class' takenBlackPieces array
                takenBlackPieces = (string[])accWhite.getTakenBlackPieces().Clone();

                //IF White has taken Black's King,
                //      Reset the board
                //      Update the visuals
                //      Display a message stating White's victory
                //      Add one point to White's score
                //      Updatee the score label
                if (accScore.hasWon(takenBlackPieces, "bKing"))
                {
                    resetGame();
                    MessageBox.Show("White has won!");
                    whitePoints++;
                    updateScore();
                }
            }
        }
Пример #2
0
        //Organizes and calls the various AI-related functions located in the Ai class.
        public void blackTurn()
        {
            ScoreBoard accScore = new ScoreBoard();

            //Used to temporarily hold a tile string value
            string tempTile = "";

            //The selected tile
            string tile = "";

            //The tile that will be moved to.
            //      Additionally, it will be used to determine a checkmate.
            string moveTo = "";

            //Infinite loop.
            while(1 != 100)
            {
                tile = "";
                moveTo = "";

                //Loops until a valid move is determined.
                while (moveTo == "noMoves"  || moveTo == "")
                {
                    Ai accAi = new Ai(board);
                    accAi.action();

                    //Sets tile to the tile that was selected by the AI.
                    tile = accAi.getSelected();

                    //Sets moveTo to the tile that was selected by the AI to be moved to
                    moveTo = accAi.getMoveTo();

                    //Sets tempTile to the tile that was selected by the AI.
                    tempTile = tile;

                    //If there is a checkmate,
                    //      BREAK out of the loop
                    if(moveTo == "surrender")
                        break;
                }

                //If there is a checkmate,
                //      BREAK out of the loop
                if (moveTo == "surrender")
                    break;

                //If the tile that was selected to be moved to contains a White piece,
                //      Take the White piece and place the Black piece on that tile
                if (board[Convert.ToInt32(moveTo.Substring(0, 1)), Convert.ToInt32(moveTo.Substring(1, 1))].Length > 0)
                {
                    if (board[Convert.ToInt32(moveTo.Substring(0, 1)), Convert.ToInt32(moveTo.Substring(1, 1))].Substring(0,1) == "w")
                    {
                        for(int i = 0; i < board.Length - 1; i++)
                        {
                            if(takenWhitePieces[i] == null)
                            {
                                takenWhitePieces[i] = board[Convert.ToInt32(moveTo.Substring(0, 1)), Convert.ToInt32(moveTo.Substring(1, 1))];
                                break;
                            }
                        }

                        break;
                    }
                }
                else if (board[Convert.ToInt32(moveTo.Substring(0, 1)), Convert.ToInt32(moveTo.Substring(1, 1))]== "")
                {
                    break;
                }
            }

            //If there isn't a checkmate
            if (moveTo != "surrender")
            {
                if (tile == "")
                {
                    if (board[Convert.ToInt32(moveTo.Substring(2, 1)), Convert.ToInt32(moveTo.Substring(3, 1))] == "bFirstMovePawn")
                    {
                        board[Convert.ToInt32(moveTo.Substring(2, 1)), Convert.ToInt32(moveTo.Substring(3, 1))] = "bPawn";
                    }

                    board[Convert.ToInt32(moveTo.Substring(0, 1)), Convert.ToInt32(moveTo.Substring(1, 1))] = board[Convert.ToInt32(moveTo.Substring(2, 1)), Convert.ToInt32(moveTo.Substring(3, 1))];

                    board[Convert.ToInt32(moveTo.Substring(2, 1)), Convert.ToInt32(moveTo.Substring(3, 1))] = "";
                }
                else
                {
                    if (board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))] == "bFirstMovePawn")
                    {
                        board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))] = "bPawn";
                    }

                    board[Convert.ToInt32(moveTo.Substring(0, 1)), Convert.ToInt32(moveTo.Substring(1, 1))] = board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))];

                    board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))] = "";
                }

                updateBoard(board);

                checkForPawns();

                refreshBoard();

                //If Black has taken White's king,
                //      Add one point to Black's score
                //      Reset the game
                //      Display a message stating Black has won
                if (accScore.hasWon(takenWhitePieces, "wKing"))
                {
                    blackPoints++;
                    resetGame();
                    MessageBox.Show("Black has won!");
                }
            }
            //If Black's King is in a checkmate,
            //      Add one point to White's score
            //      Reset the game
            //      Display a message stating White has won
            else
            {
                whitePoints++;
                resetGame();
                MessageBox.Show("White has won!");
            }
        }
Пример #3
0
        //Used to select and move White's pieces
        public void whiteTurn(string tile)
        {
            if (selectedTile == tile || board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))] == "" && !hasSelectedPiece)
            {
                setBoard(true);
                hasSelectedPiece = false;
                selectedTile = "";
                count = -1;
            }
            else if (board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))] != "" || hasSelectedPiece)
            {
                White accWhite = new White(hasSelectedPiece, tile, board, piece, selectedTile, takenBlackPieces);
                ScoreBoard accScore = new ScoreBoard();

                accWhite.action();

                ArrayList list = new ArrayList();

                list = (ArrayList)accWhite.generateListOfPossibleMoves().Clone();

                selectedTile = accWhite.getSelectedTile();

                if (!hasSelectedPiece && accWhite.getTile() != "")
                {
                    setBoard(false);
                    activateButtons(list);
                    hasSelectedPiece = true;
                }
                else
                {
                    setBoard(true);
                    hasSelectedPiece = false;
                    selectedTile = "";
                    isWhiteTurn = false;
                }

                updateBoard(accWhite.getBoard());

                checkForPawns();

                refreshBoard();

                takenBlackPieces = (string[])accWhite.getTakenBlackPieces().Clone();

                if (accScore.hasWon(takenBlackPieces, "bKing"))
                {
                    whitePoints++;
                    resetGame();
                    MessageBox.Show("White has won!");
                }

            }
        }
Пример #4
0
        public void action()
        {
            ScoreBoard accSb = new ScoreBoard();

            if (hasSelectedPiece)
            {
                    //IF the newly selected tile contains a piece THEN
                        //IF the tile contains a white piece
                            //Display a message stating it is an invalid move
                    if (board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))].Length > 0)
                    {
                        if (board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))].Substring(0, 1) == "b")
                        {

                        }
                        else if (board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))].Substring(0, 1) == "w")
                        {
                            for (int i = 0; i < takenWhitePieces.Length - 1; i++)
                            {
                                if (takenWhitePieces[i] == null)
                                {
                                    takenWhitePieces[i] = board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))];
                                    break;
                                }
                            }
                        }

                    }

                    if (tile != selectedTile && selectedTile != "")
                    {
                        if (board[Convert.ToInt32(selectedTile.Substring(0, 1)), Convert.ToInt32(selectedTile.Substring(1, 1))] == "bFirstMovePawn")
                        {
                            board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))] = "bPawn";
                        }
                        else
                        {
                            board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))] = board[Convert.ToInt32(selectedTile.Substring(0, 1)), Convert.ToInt32(selectedTile.Substring(1, 1))];
                        }

                        board[Convert.ToInt32(selectedTile.Substring(0, 1)), Convert.ToInt32(selectedTile.Substring(1, 1))] = "";

                        hasMoved = true;

                    }

                    selectedTile = "";

                    hasSelectedPiece = false;

            }
            else
            {
                piece = board[Convert.ToInt32(tile.Substring(0, 1)), Convert.ToInt32(tile.Substring(1, 1))];

                if (piece != "")
                {
                    hasSelectedPiece = true;
                }

                selectedTile = tile;

            }
        }