コード例 #1
0
ファイル: AIPlayer.cs プロジェクト: moconno/ConnectFour
        public override void Move(Board b)
        {
            {
                //Create a new game state for the root of the state space
                GameState gameState = new GameState(GameState.State.initial, b, this, null, cell, true);

                //Set the state as the root
                MiniMaxTree m = new MiniMaxTree(gameState);

                //Find children states if they exist
                m.GenerateStates(gameState, 4, true);

                //The value returned by the recursive minimax function
                int value = m.MiniMax(gameState, 4, true);

                cell = null;

                foreach(GameState child in gameState.GetChildren())
                {
                    if (child.GetHeuristicValue() == value)
                    {
                        cell = child.GetCell();
                    }
                }

                //Set the cell on the playing board
                b.getCell(cell.getRow(), cell.getColumn()).setState((int)cell.getState());

                //if a row exists above this cell, make it playable
                if (cell.getRow() != 0)
                {
                    b.getCell(cell.getRow() - 1, cell.getColumn()).isPlayable(true);
                }

                //Test if the game is over
                if(MiniMaxTree.TerminalTest(cell))
                {
                    b.printBoard();
                    Console.WriteLine("GameOver: " + this.getColorToString() + " wins");
                    b.isGameOver(true);
                }

                moveCount++;

            }
        }
コード例 #2
0
ファイル: HumanPlayer.cs プロジェクト: moconno/ConnectFour
        public override void Move(Board b)
        {
            if(moveCount == b.GetLength() * b.GetWidth())
            {
                Console.WriteLine("Game Over, you both lose");
            }

            Boolean moveMade = false;

            b.printBoard();

            if(this.getOpponent() != null)
            {
                Console.WriteLine(" ");
            }

            Console.WriteLine(this.getColorToString() + " player's turn");

            while(!moveMade)
            {
                Console.Write("Select a Move: " + "0 - " + (b.GetWidth() - 1) + ": ");

                int choice = Convert.ToInt32(Console.ReadLine());

                for (int i = b.GetLength() - 1; i >= 0; i--)
                {
                    if(choice < 0 || choice > b.GetWidth() - 1)
                    {
                        break;
                    }

                    cell = b.getCell(i, choice);

                    if (cell.isPlayable())
                    {
                        cell.setState((int)color);

                        cell.isPlayable(false);

                        if (i != 0)
                        {
                            b.getCell(i - 1, choice).isPlayable(true);
                        }

                        moveMade = true;

                        moveCount++;

                        if (MiniMaxTree.TerminalTest(cell))
                        {
                            b.printBoard();
                            Console.WriteLine("GameOver: " + this.getColorToString() + " wins");
                            b.isGameOver(true);
                        }

                        break;
                    }
                }

            }
        }