Пример #1
0
        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
        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;
                    }
                }

            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            Console.Write("Board Width? ");
            colNum = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();

            Console.Write("Board Height? ");
            rowNum = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();

            Console.Write("Connect? ");
            r = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();

            Console.WriteLine("1: Human vs Human");
            Console.WriteLine("2: Human vs AI");
            Console.WriteLine("3: AI vs AI");
            Console.Write("Select Players: ");
            int choice = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();

            Board board = new Board(rowNum, colNum, r);
            Region.findConnectedCells(board);
            board.UpdateCellObservers();

            Player p1 = null;
            Player p2 = null;

            switch (choice)
            {
                case 1:
                    p1 = new HumanPlayer();
                    p1.setAsRed();
                    p2 = new HumanPlayer();
                    p2.setAsBlack();
                    break;

                case 2:
                    Console.WriteLine("1: Human 2: AI");
                    Console.Write("Who will go first? ");
                    choice = Convert.ToInt32(Console.ReadLine());
                    if(choice == 1)
                    {
                        p1 = new HumanPlayer();
                        p1.setAsRed();
                        p2 = new AIPlayer();
                        p2.setAsBlack();
                    }
                    else
                    {
                        p1 = new AIPlayer();
                        p1.setAsRed();
                        p2 = new HumanPlayer();
                        p2.setAsBlack();
                    }
                    p1.setOpponent(p2);
                    p2.setOpponent(p1);
                    break;

                case 3:
                    break;

            }

            int turn = RED;

            while(!board.isGameOver())
            {
                switch (turn)
                {
                    case RED:
                        p1.Move(board);
                        turn = BLACK;
                        break;
                    case BLACK:
                        p2.Move(board);
                        turn = RED;
                        break;
                }
            }

            Console.Read();
        }