예제 #1
0
 //Constructor that sets up default game
 public GameState()
 {
     HumanPlayer = new Human();
     AIPlayer = new AI();
     util = new Common();
     gameboard = new char[9];
     fillboard();
     GameStatus = 1;
     ReturnStatus = 0;
 }
예제 #2
0
 public GameManager(GameType gameSize, int cellSize, Grid grid, PlayerType playerOne, PlayerType playerTwo)
 {
     size       = gameSize;
     refGrid    = grid;
     squareSize = cellSize;
     if (playerOne == PlayerType.AI) //Create the AIs
     {
         player1 = new AI(gameSize);
     }
     if (playerTwo == PlayerType.AI)
     {
         player2 = new AI(gameSize);
     }
     if (size == GameType.Small)
     {
         SmallGrid = new SmallGrid(0, 0, 0, 0, GameType.Small, squareSize, refGrid); //Create a small grid and pass it to the AIS
         if (playerOne == PlayerType.AI)
         {
             player1.AddGrid(SmallGrid);
         }
         if (playerTwo == PlayerType.AI)
         {
             player2.AddGrid(SmallGrid);
         }
     }
     else if (size == GameType.Big)
     {
         BigGrid = new BigGrid(0, 0, GameType.Big, squareSize, refGrid); //Create a big grid and pass it to the AIs
         if (playerOne == PlayerType.AI)
         {
             player1.AddGrid(BigGrid);
         }
         if (playerTwo == PlayerType.AI)
         {
             player2.AddGrid(BigGrid);
         }
     }
     else if (size == GameType.Huge)
     {
         HugeGrid = new HugeGrid(squareSize, refGrid); //Create a huge grid and pass it to the AIs
         if (playerOne == PlayerType.AI)
         {
             player1.AddGrid(HugeGrid);
         }
         if (playerTwo == PlayerType.AI)
         {
             player2.AddGrid(HugeGrid);
         }
     }
     if (playerOne == PlayerType.AI)
     {
         player1.AddManager(this);
     }
     if (playerTwo == PlayerType.AI)
     {
         player2.AddManager(this);
     }
     if (playerOne == PlayerType.AI)
     {
         player1.MakeChoice();
     }
 }
예제 #3
0
        static void Main(string[] args)
        {
            AI ai = new AI();

            do
            {
                Console.Clear();// whenever loop will be again start then screen will be clear
                Console.WriteLine("Player1:X and AI:O");
                Console.WriteLine("\n");

                Board();             // calling the board Function

                if (player % 2 == 0) //checking the chance of the player
                {
                    Console.WriteLine('\n' + "AI Chance");
                    choice = ai.getMove(arr);
                }

                else
                {
                    Console.WriteLine('\n' + "Player 1 Chance");
                    choice = int.Parse(Console.ReadLine());//Taking users choice
                }



                //checking that position where user want to run is marked(with X or O) or not
                if (isValid(arr, choice))
                {
                    if (player % 2 == 0)
                    { //if chance is of player 2 then mark O else mark X
                        arr[choice] = 'O';
                        player++;
                    }

                    else
                    {
                        arr[choice] = 'X';
                        player++;
                    }
                }

                else
                { //If there is any possition where user want to run and that is already marked then show message and load board again
                    Console.WriteLine("Sorry the row {0} is already marked with {1}", choice, arr[choice]);
                    Console.WriteLine("\n");
                    Console.WriteLine("Please wait 2 second board is loading again.....");

                    Thread.Sleep(2000);
                }

                flag = CheckWin();             // calling of check win
            } while (flag != 1 && flag != -1); // This loof will be run until all cell of the grid is not marked with X and O or some player is not win

            Console.Clear();                   // clearing the console

            Board();                           // getting filled board again

            if (flag == 1)                     // if flag value is 1 then some one has win or means who played marked last time which has win
            {
                Console.WriteLine("Player {0} has won", (player % 2) + 1);
            }

            else // if flag value is -1 the match will be draw and no one is winner
            {
                Console.WriteLine("Draw");
            }

            Console.ReadLine();
        }
예제 #4
0
 private void EndHandler(Status status)
 {
     MessageBox.Show(status == Status.Tie ? "It's a tie!" : status == Status.CircleWin ? "Circles win!" : "Crosses win!"); //Show the result
     player1 = null;                                                                                                       //Delete the AIs so the game stops
     player2 = null;
 }
예제 #5
0
        static void Main(string[] args)
        {

            while (true)
            {
                Console.WriteLine("Please enter X or O as your token for the game: ");
                char userToken = Console.ReadLine().Trim().ToUpper()[0];
                if (userToken == '0')
                    userToken = 'O';
                char aiToken = userToken.Equals('X') ? 'O' : 'X';

                Console.WriteLine();
                Console.WriteLine("Do you want to start the game?");
                Console.WriteLine("Type Y for YES");
                Console.WriteLine("Type N for NO");
                char start = Console.ReadLine().Trim().ToUpper()[0];

                bool userStarts = start.Equals('Y') ? true : false;

                TicTacToe ttt = new TicTacToe(userToken, aiToken, userStarts);
                AI ai = new AI(ttt);

                bool isThereWinner = false;
                bool isDraw = false;

                while (!isThereWinner && !isDraw)
                {
                    ttt.toggleTurn();
                    if (ttt.isUserTurn())
                    {
                        short pos;
                        do
                        {
                            Console.WriteLine("Enter a valid position to insert your token: ");
                            pos = Int16.Parse(Console.ReadLine()[0].ToString());
                        } while (!ttt.isValidMove(pos));
                        
                        ttt.play(pos);
                    }
                    else
                    {
                        ai.play();
                    }
                    isThereWinner = ttt.isThereAWinner();
                    isDraw = ttt.isDraw();
                }

                if (isThereWinner)
                {
                    Console.WriteLine("We have a WINNER!");
                    Console.WriteLine(ttt.getWinner() + " is the winner!!!");
                }
                else
                {
                    Console.WriteLine("Its a DRAW!");
                }
                
                Console.WriteLine();
                Console.WriteLine("Do you want to play another game? If yes, press Y else press any other key.");
                string toContinue = Console.ReadLine()[0].ToString();
                if (!toContinue.Equals("Y", StringComparison.InvariantCultureIgnoreCase))
                    break;
            }
        }