コード例 #1
0
        public void PlayGame(Player currentPlayer, Player otherPlayer)
        {
            board.DisplayBoard();
            Tuple <int, int> selectedMove = currentPlayer.GetMove(board);

            board[selectedMove.Item1, selectedMove.Item2] = currentPlayer.counter;
            if (IsOver(board, currentPlayer))
            {
                if (currentPlayer.Win(board, currentPlayer.counter))
                {
                    board.DisplayBoard();
                    if (currentPlayer.GetType() == typeof(AIPlayer))
                    {
                        Console.WriteLine("The computer has beaten you. Better luck next time...");
                    }
                    else
                    {
                        Console.WriteLine("Congratulations! {0} has won!", currentPlayer.name);
                    }
                    Console.ReadLine();
                    Program.Main();
                }
                Console.WriteLine("The game is a draw.");
                Console.ReadLine();
                Program.Main();
            }
            PlayGame(otherPlayer, currentPlayer);
        }
コード例 #2
0
        public Tuple <int, Tuple <int, int>, GameBoard <counters>, GameBoard <int> > MakeRandomMove(GameBoard <counters> board, counters counter, int ply, Tuple <int, int> positions, bool mmax, GameBoard <int> scoreBoard, ref int cont)
        {
            List <Tuple <int, int> > availableMoves = getAvailableMoves(board, positions);
            Tuple <int, int>         Move           = new Tuple <int, int>(0, 0);
            Random           rnd       = new Random();
            int              randMoveX = rnd.Next(1, 7);   // creates a number between 1 and 7
            int              randMoveY = rnd.Next(1, 7);   // creates a number between 1 and 7
            Tuple <int, int> randMove  = new Tuple <int, int>(randMoveX, randMoveY);
            int              score     = Consts.MIN_SCORE; // current score of move
            counters         us        = Flip(counter);
            int              ourindex  = 1;
            // Create new stopwatch.
            Stopwatch stopwatch = new Stopwatch();

            // Begin timing.
            stopwatch.Start();
            Move      = randMove;
            positions = randMove; // HWL: NB: this overwrites the arg positions; which means you are actually not considering the move in positions, but a different, random move; this probably explains why you get such strange moves in your gameplay
            board.DisplayBoard();
            //cont = 1;
            if (ply > 0)
            {
                score = EvalCurrentBoard(board, scoreBoard, ourindex, us);  // is current pos a win?
            }
            // Stop timing.
            stopwatch.Stop();
            Console.WriteLine("========================================================================================================================" +
                              "RANDOMLY SELECTED MOVE:" + Environment.NewLine + "------------------------------------------------------------------------------------------------------------------------" +
                              "position: " + positions + "; " +
                              "for player: " + counter + "; " +
                              "score: " + score + "; " +
                              "positions visited " + cont + "; " +
                              "depth level: " + ply + Environment.NewLine +
                              "elapsed time for move: " + stopwatch.Elapsed + "; " +
                              "no. of remaining moves left: " + availableMoves.Count + Environment.NewLine +
                              "two in a row detected at: " + "Cell 1: " + IsLeftofTwo(board, counter) + ", " + "Cell 2: " + IsRightofTwo(board, counter)
                              + Environment.NewLine +
                              "build on two-in-row? " + "left: " + board.IsTwoLeftNeighbourEmpty(board, counter) + " at position " + board.PrintTwoLeftNeighbour(board, counter) +
                              ", right: " + board.IsTwoRightNeighbourEmpty(board, counter) + " at position " + board.PrintTwoRightNeighbour(board, counter) + Environment.NewLine +
                              "top: " + board.IsTwoTopNeighbourEmpty(board, counter) + " at position " + board.PrintTwoTopNeighbour(board, counter) +
                              ", bottom: " + board.IsTwoBottomNeighbourEmpty(board, counter) + " at position " + board.PrintTwoBottomNeighbour(board, counter) + Environment.NewLine
                              + "build on one-in-row? " + "left: " + board.IsOneLeftNeighbourEmpty(board, counter) + " at position " + board.PrintOneLeftNeighbour(board, counter) +
                              ", right: " + board.IsOneRightNeighbourEmpty(board, counter) + " at position " + board.PrintOneRightNeighbour(board, counter) + Environment.NewLine +
                              "top: " + board.IsOneTopNeighbourEmpty(board, counter) + " at position " + board.PrintOneTopNeighbour(board, counter) +
                              ", bottom: " + board.IsOneBottomNeighbourEmpty(board, counter) + " at position " + board.PrintOneBottomNeighbour(board, counter) + Environment.NewLine +
                              "build on two-in-row?:" + " with horzi gap? " + board.IsTwoWithHorziGapEmpty(board, counter) + " at position " + board.PrintTwoWithHorziGap(board, counter) +
                              ", with vertical gap?: " + board.IsTwoWithVerticalGapEmpty(board, counter) + " at position " + board.PrintTwoWithVerticalGap(board, counter));
            Console.WriteLine("========================================================================================================================");
            // Console.ReadLine();
            // assign score to correct cell in score
            scoreBoard[randMoveX, randMoveY] = score;
            // RandScoringSummary(board, scoreBoard);
            //  scoreBoard.DisplayBoard();
            // Console.ReadLine();
            return(new Tuple <int, Tuple <int, int>, GameBoard <counters>, GameBoard <int> >(score, positions, board, scoreBoard));
        }
コード例 #3
0
        // GET MOVE
        public override Tuple <int, int> GetMove(GameBoard <counters> board, GameBoard <int> scoreBoard)
        {
            // Create new stopwatch.
            Stopwatch stopwatch = new Stopwatch();

            // Begin timing.
            stopwatch.Start();
            // Do work
            List <Tuple <int, int> > availableMoves = getAvailableMoves(board, positions);
            Tuple <int, Tuple <int, int>, GameBoard <counters>, GameBoard <int> > result;

            result = Minimax(board, counter, ply, positions, true, scoreBoard, ref cont, alpha, beta); // 0,0
            int score = result.Item1;

            board.DisplayBoard();
            // Stop timing.
            stopwatch.Stop();
            // Return positions
            return(result.Item2);
        }
コード例 #4
0
        public void PlayGame(Player currentPlayer, Player otherPlayer)
        {
            // Create new stopwatch.
            Stopwatch stopwatch_minimax = new Stopwatch();

            // Begin timing.
            stopwatch_minimax.Start();
            board[1, 3] = counters.CROSSES;
            board[1, 1] = counters.CROSSES;
            board[1, 2] = counters.CROSSES;
            board[3, 1] = counters.CROSSES;
            Tuple <int, int> selectedMove = currentPlayer.GetMove(board, scoreBoard);

            board[selectedMove.Item1, selectedMove.Item2] = currentPlayer.counter;
            Tuple <int, int> centreof3inarow = new Tuple <int, int> (0, 0);

            if (IsOver(board, currentPlayer))
            {
                if (currentPlayer.Win(board, currentPlayer.counter))
                {
                    board.DisplayBoard();

                    if (currentPlayer.GetType() == typeof(AIPlayer))
                    {
                        int score = 0;
                        if (AIPlayer.FindThreeInARow(board, currentPlayer.counter) == true)
                        {
                            score = 1000;
                        }

                        Console.WriteLine("========================================================================================================================"
                                          + Environment.NewLine + "GAME OVER! " + Environment.NewLine +
                                          "------------------------------------------------------------------------------------------------------------------------" +
                                          "Winner: " + currentPlayer.counter
                                          + Environment.NewLine + "Score: " + score + Environment.NewLine +
                                          "Positions visited: " + AIPlayer.cont + Environment.NewLine +
                                          "Coordinations of winning three-in-a-row at: "
                                          + Environment.NewLine + "Cell 1: " + AIPlayer.IsLeftOfThree(board, currentPlayer.counter)
                                          + Environment.NewLine + "Cell 2: " + AIPlayer.IsCentreOfThree(board, currentPlayer.counter)
                                          + Environment.NewLine + "Cell 3: " + AIPlayer.IsRightOfThree(board, currentPlayer.counter));
                    }
                    else
                    {
                        int score = 0;
                        if (AIPlayer.FindThreeInARow(board, otherPlayer.counter) == true)
                        {
                            score = -1000;
                        }

                        Console.WriteLine("======================================================================================================================"
                                          + Environment.NewLine + "GAME OVER! " + Environment.NewLine +
                                          "------------------------------------------------------------------------------------------------------------------------" +
                                          "Winner: " + otherPlayer.counter
                                          + Environment.NewLine + "Score: " + score
                                          + Environment.NewLine + "Coordinations of winning three-in-a-row at: "
                                          + Environment.NewLine
                                          + "Cell 1: " + AIPlayer.IsLeftOfThree(board, otherPlayer.counter) + Environment.NewLine
                                          + "Cell 2: " + AIPlayer.IsCentreOfThree(board, otherPlayer.counter) + Environment.NewLine
                                          + "Cell 3: " + AIPlayer.IsRightOfThree(board, otherPlayer.counter));
                    }
                    // Stop timing.
                    stopwatch_minimax.Stop();
                    // Write result.
                    Console.WriteLine("Total elapsed for Minimax over full game execution: " + stopwatch_minimax.Elapsed + Environment.NewLine +
                                      "========================================================================================================================");
                    Console.ReadLine();
                    Program.Main();
                }
                Console.WriteLine("The game is a draw.");
                Program.Main();
            }
            if (stopMe)
            {
                stopwatch_minimax.Stop();
                Console.WriteLine("**HWL One move made. ");
                Console.WriteLine("**HWL elapsed time for one move: " + stopwatch_minimax.Elapsed + Environment.NewLine + "-------------------------------------------------------");
            }
            else
            {
                stopMe = true;
            }
            PlayGame(otherPlayer, currentPlayer);
        }