Ejemplo n.º 1
0
        /**
         *
         */
        private static void hardLevel(int size)
        {
            Board.Blank b = new Board.Blank();
            if (!isWin(size))
            {
                Console.WriteLine("Computer is making the move...");
                if (gb.blankPlaces(size, size).Count == gb.Size(size))
                {
                    Random r = new Random();
                    b = new Board.Blank(r.Next(0, size), r.Next(0, size));
                }
                else
                {
                    b = AI.GetBestMove(gb, Board.user.X, size, size);
                }
                gb[b.A, b.B] = Board.user.X;
                dispBoard(size);

                /**
                 * Check win conditions to stop the game or continue to the next move
                 */
                if (!isWin(size))
                {
                    NextMove(size);
                }
            }
        }
Ejemplo n.º 2
0
 private static void easyLevel(int size)
 {
     Board.Blank b = new Board.Blank();
     if (!isWin(size))
     {
         Console.WriteLine("Computer is making the move...");
         if (gb.blankPlaces(size, size).Count == gb.Size(size)) //if all spaces are open, randomly pick one for excitement
         {
             Random r = new Random();
             b = new Board.Blank(r.Next(0, size), r.Next(0, size));
         }
         else
         {
             Random r = new Random();
             do
             {
                 b = new Board.Blank(r.Next(0, size), r.Next(0, size));
             } while (gb.isFull(b.A, b.B));
         }
         gb[b.A, b.B] = Board.user.X;
         dispBoard(size);
         if (!isWin(size))
         {
             NextMove(size);
         }
     }
 }
Ejemplo n.º 3
0
        public static Board.Blank GetBestMove(BoardInitialize gb, Board.user p, int length, int width)
        {
            Board.Blank?       bestSpace  = null;
            List <Board.Blank> openSpaces = gb.blankPlaces(length, width);
            BoardInitialize    newBoard;

            for (int i = 0; i < openSpaces.Count; i++)
            {
                newBoard = gb.Clone(length, width);
                Board.Blank newSpace = openSpaces[i];

                newBoard[newSpace.A, newSpace.B] = p;

                if (newBoard.Winner(length, width) == Board.user.space && newBoard.blankPlaces(length, width).Count > 0)
                {
                    Board.Blank tempMove = GetBestMove(newBoard, (Board.user)(-(int)p), length, width);
                    newSpace.Rank = tempMove.Rank;
                }
                else
                {
                    if (newBoard.Winner(length, width) == Board.user.space)
                    {
                        newSpace.Rank = 0;
                    }
                    else if (newBoard.Winner(length, width) == Board.user.X)
                    {
                        newSpace.Rank = -1;
                    }
                    else if (newBoard.Winner(length, width) == Board.user.O)
                    {
                        newSpace.Rank = 1;
                    }
                }

                if (bestSpace == null ||
                    (p == Board.user.X && newSpace.Rank < ((Board.Blank)bestSpace).Rank) ||
                    (p == Board.user.O && newSpace.Rank > ((Board.Blank)bestSpace).Rank))
                {
                    bestSpace = newSpace;
                }
            }

            return((Board.Blank)bestSpace);
        }
Ejemplo n.º 4
0
        /**
         * Prompt user for their next move
         */
        private static void NextMove(int size)
        {
            int    x = 0, y = 0;
            string input = "";

            Board.Blank b = new Board.Blank();
            do
            {
                Console.WriteLine("What is your move?");

                /**
                 * Error handling for when user selects a row or column that is out of bounds
                 */
                do
                {
                    Console.WriteLine("Please type a row number from 1 to " + size);
                    input = Console.ReadLine().Trim();
                    x     = Convert.ToInt32(input) - 1;
                    Console.WriteLine("Please type a column number from 1 to " + size);
                    input = Console.ReadLine().Trim();
                    y     = Convert.ToInt32(input) - 1;
                    if (!(x >= 0 && x < size))
                    {
                        Console.WriteLine("Wrong input, please enter row between 1 to " + size);
                    }
                    if (!(y >= 0 && y < size))
                    {
                        Console.WriteLine("Wrong input, please enter column between 1 to " + size);
                    }
                } while (!((x >= 0 && x < size) && (y >= 0 && y < size)));
                b.A = x;
                b.B = y;

                if (gb.isFull(x, y))
                {
                    Console.WriteLine("Place is already occupied.");
                }
            } while (gb.isFull(x, y));
            gb[b.A, b.B] = Board.user.O;

            /**
             * Display the board with corrected user inputs and choose AI move based on selected difficulty
             */
            dispBoard(size);
            if (Level == 1)
            {
                easyLevel(size);
                //break;
            }
            else if (Level == 2)
            {
                mediumLevel(size);
                // break;
            }
            else
            {
                hardLevel(size);
            }

            /**
             * Game is finished.
             * Prompt to restart
             */
            input = "";
            do
            {
                Console.WriteLine("Want to continue the game(Y/N)");
                input = Console.ReadLine().ToLower();
                if (input != "y" && input != "n")
                {
                    Console.WriteLine("Invalid input, please enter y/n");
                }
                else if (input == "y")
                {
                    Console.Clear();
                    boardLoad();
                }
                else
                {
                    Console.WriteLine("Thank you for playing Tic-Tac-Toe.");
                    Thread.Sleep(800);
                    Environment.Exit(0);
                }
            } while (input != "y" && input != "n");
        }