Exemple #1
0
        void cellClick(Object sender, EventArgs e)
        {
            Button currentCell = (Button)sender;

            if (!isEmpty(currentCell) || game.getBoard().getIndexValue(Convert.ToInt32(currentCell.Name)) != 0)
            {
                MessageBox.Show("This cell is already occupied", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                if (playerTurn == 1)
                {
                    currentCell.Text      = "X";
                    currentCell.Tag       = "1";
                    currentCell.BackColor = Color.Green;
                    game.getBoard().setIndex(Convert.ToInt32(currentCell.Name), playerOne);
                    if (checkWin(playerTurn) == 1)
                    {
                        MessageBox.Show("Player one is the Winner!!!", "Winner!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        resetGame();
                        game.getBoard().resetBoard();
                        playerTurn = 1;
                        return;
                    }
                    playerTurn = 2;
                    if (playerTwo.makeStrategyMove() && playerTurn == 2)
                    {
                        markOpponentMove();
                        playerTwo.printEvaluatedBoard();
                        checkWin(playerTurn);
                        if (checkWin(playerTurn) == 2)
                        {
                            MessageBox.Show("Player two is the Winner!!!", "Winner!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            resetGame();
                            game.getBoard().resetBoard();
                            playerTwo.makeStrategyMove();
                            markOpponentMove();
                            playerTurn = 1;
                            return;
                        }
                        playerTurn = 1;
                    }
                }
            }
        }
        public int playGame(Individual startingPlayer, Individual secondPlayer)
        {
            int result = -1;

            // set the starting player value to 1 ('X')
            startingPlayer.setValue(1);
            // set the second player value to 2 ('O')
            secondPlayer.setValue(2);
            while (true)
            {
                // starting player code block
                //////////////////////////////////////
                // can make either a random move or a move using the strategy tree
                if (!startingPlayer.makeStrategyMove())
                {
                    // an attempt to make a move on an occupied space was made
                    // abort the game
                    Console.WriteLine("Move " + startingPlayer.getValue() + " Failed");
                    Console.WriteLine("SHUTTING GAME DOWN: OBSOLETE GARBAGE");
                    break;
                }
                // check if the starting player move resulted in a win
                result = board.checkWin(startingPlayer);
                if (result != 0)
                {
                    break;
                }
                //////////////////////////////////////
                // starting player code block end

                // second player code block
                //////////////////////////////////////
                if (!secondPlayer.makeStrategyMove())
                {
                    Console.WriteLine("Move " + secondPlayer.getValue() + " Failed");
                    Console.WriteLine("SHUTTING GAME DOWN: OBSOLETE GARBAGE");
                    break;
                }

                result = board.checkWin(secondPlayer);
                if (result != 0)
                {
                    break;
                }
                //////////////////////////////////////
            }
            // add the result accordingly
            switch (result)
            {
            case DRAW:
                startingPlayer.addDraw(true);
                secondPlayer.addDraw(false);
                break;

            case START_WIN:
                startingPlayer.addWin(true);
                secondPlayer.addLoss(false);
                break;

            case SECOND_WIN:
                secondPlayer.addWin(false);
                startingPlayer.addLoss(true);
                break;

            case INCOMPLETE_TREE:
                Console.WriteLine("Incomplete Tree Error");
                break;

            default:
                Console.WriteLine("CATASTROPHIC FAILURE!!!");
                break;
            }
            return(result);
        }