Пример #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Tic Tac Toe. To choose a spot, type 1A, replace 1 with numbers 1, 2, or 3, and A with A, B, or C to determine move");
            TicTacToe t = new TicTacToe();

            t.initializeBoard();
            t.handleMoves();
            //Console.ReadLine();
        }
Пример #2
0
        static void Main(string[] args)
        {
            var t = new TicTacToe();

            char[] board  = t.CreateBoard();
            char   letter = t.ChooseLetter();

            t.Toss();
            t.Play();
            Console.WriteLine("\nCheck if won : " + t.CheckWinner(letter));
        }
Пример #3
0
        static void Main(string[] args)
        {
            char[] letter = { 'X', 'O' };
            Console.WriteLine(" Welcome to Tic Tac Toe Game");

            // TicTacToe.chooseLetter(letter);
            TicTacToe.play(letter);
            string anotherGame = Console.ReadLine();

            if (anotherGame == "YES")
            {
                Console.WriteLine(" Play Another Game ");
                TicTacToe.emptyTheBoard();
                TicTacToe.play(letter);
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            var game = new TicTacToe("X", "O");

            Console.WriteLine("Board");
            game.DisplayBoard();

            //var play = "X, X, O, O, X, O, X, O, X, O";
            //game.ImportBoard(play);
            //game.DisplayBoard();

            Console.WriteLine(game.IsAWin());



            // game.DisplayHelpBoard();
        }
Пример #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Tic Tac Toe Game");
            TicTacToe tic = new TicTacToe();

            // To create a new board
            char [] board = tic.CreateBoard();
            // To show board
            tic.ShowBoard(board);
            tic.Toss();
            // To choose a letter by a user
            char userLetter     = tic.ChooseLetter();
            char computerLetter = (userLetter == 'X') ? 'O' : 'X';
            // To move desired location
            int userMove = tic.getUserMove(board);

            tic.makeMove(board, userMove, userLetter);
            tic.ShowBoard(board);
            //Computer Move
            int computerMove = tic.getComputerMove(board, computerLetter, userLetter);

            Console.WriteLine("Check if Won: " + tic.isWinner(board, userLetter));
        }
Пример #6
0
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        static void Main()
        {
            bool flag = true;

            while (flag)
            {
                Console.WriteLine("Welocome to TicTacToe Game");
                //Toss to decide, who will move, either computer or user.
                int tossResult = TicTacToe.TossForGame();
                //Array created for board positions
                char[] boardPositions = TicTacToe.CreatingBoard();
                //choosing character for player
                char[] charactersArray = TicTacToe.ChoosingCharacter();
                Console.WriteLine($"you will play with :{charactersArray[0]}");
                Console.WriteLine($"CPU will play with :{charactersArray[1]}");
                //displaying the board.
                TicTacToe.Board(boardPositions);
                //filling the values
                TicTacToe.MarkingPositions(boardPositions, charactersArray, tossResult);

                Console.WriteLine("Do you want to play again, press enter to Exit, Y to continue");
                string inputForRematch = Console.ReadLine();
                if (inputForRematch == "")
                {
                    flag = false;
                }
                else if (inputForRematch.ToLower() == "y")
                {
                    flag = true;
                }
                else
                {
                    break;
                }
            }
        }
Пример #7
0
        static void Main(string[] args)
        {
            char[] board          = TicTacToe.CreateBoard();
            char   userLetter     = TicTacToe.chooseUserLetter();
            char   computerLetter = (userLetter == 'X') ? 'O' : 'X';

            TicTacToe.Player player        = TicTacToe.getWhoStartsFirst();
            bool             gameIsPlaying = true;

            TicTacToe.GameStatus gameStatus;
            while (gameIsPlaying)
            {
                //players turn
                if (player.Equals(TicTacToe.Player.USER))
                {
                    TicTacToe.showBoard(board);
                    int    userMove   = TicTacToe.getUserMove(board);
                    String WonMessage = "Hooray! you won";
                    gameStatus = TicTacToe.getGameStatus(board, userMove, userLetter, WonMessage);
                    player     = TicTacToe.Player.COMPUTER;
                }
                else
                {
                    // computer turn
                    String WonMessage   = "The computer has beaten you";
                    int    computerMove = TicTacToe.getComputerMove(board, computerLetter, userLetter);
                    gameStatus = TicTacToe.getGameStatus(board, computerMove, computerLetter, WonMessage);
                    player     = TicTacToe.Player.USER;
                }
                if (gameStatus.Equals(TicTacToe.GameStatus.CONTINUE))
                {
                    continue;
                }
                TicTacToe.playAgain();
            }
        }
Пример #8
0
        public static void Play()
        {
            TicTacToe game = new TicTacToe();

            Console.WriteLine("Welcome to Tic Tac Toe Game!");

            Console.WriteLine("Enter the first player's name:");
            game.PlayerOne = Console.ReadLine();

            Console.WriteLine("Enter the second player's name:");
            game.PlayerTwo = Console.ReadLine();

            bool markerValid = false;

            while (!markerValid)
            {
                Console.WriteLine("Select a letter as " + game.PlayerOne + "'s marker:");

                string marker = Console.ReadLine();

                if (marker.Length == 1 && char.IsLetter(marker.ToCharArray()[0]))
                {
                    markerValid  = true;
                    game.Marker1 = marker.ToCharArray()[0];
                }
                else
                {
                    Console.WriteLine("Invalid marker, try again with another character");
                }
            }

            markerValid = false;

            while (!markerValid)
            {
                Console.WriteLine("Select any letter as " + game.PlayerTwo + "'s marker: ");
                string marker = Console.ReadLine();

                if (marker.Length == 1 && char.IsLetter(marker.ToCharArray()[0]))
                {
                    markerValid  = true;
                    game.Marker2 = marker.ToCharArray()[0];
                }
                else
                {
                    Console.WriteLine("Invalid marker, try again with another character");
                }
            }

            bool continuePlaying = true;

            while (continuePlaying)
            {
                // Initializing the board
                game.Init();

                Console.WriteLine("The rules are well known. Or you will figure it out along the way :)");

                // Draw the board with current state
                Console.WriteLine(game.DrawBoard());

                // initializing the player's name to none
                string player = null;

                while (!game.Winner() && game.Plays < 9)
                {
                    // setting the current player's name
                    player = game.CurrentPlayer == 1 ? game.PlayerOne : game.PlayerTwo;

                    bool validPick = false;

                    while (!validPick)
                    {
                        Console.WriteLine("It is " + player + "'s turn. Pick a square/box:");

                        string square = Console.ReadLine();

                        if (square.Length == 1 && char.IsDigit(square.ToCharArray()[0]))
                        {
                            int pick = 0;

                            try
                            {
                                pick = int.Parse(square);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }

                            validPick = game.PlaceMarker(pick);
                        }

                        if (!validPick)
                        {
                            Console.WriteLine("This Square can not be selected... Please Try again");
                        }
                    }

                    // Current player market a square -> switch to the other player
                    game.SwitchPlayers();

                    Console.WriteLine(game.DrawBoard());
                }

                if (game.Winner())
                {
                    Console.WriteLine("Congrats! " + player + " wins the game!");
                }
                else
                {
                    Console.WriteLine("Tada! Game Over - It's a Draw");
                }

                Console.WriteLine("Want to Play again? Type (Y/N)");

                string choice = Console.ReadLine();

                if (!(choice.ToUpper() == "Y"))
                {
                    continuePlaying = false;
                }
            }
        }
Пример #9
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, Welcome to TicTacToeGame");
            TicTacToe t         = new TicTacToe();
            bool      playAgain = true;

            while (playAgain)
            {
                char[] board   = t.CreateBoard();
                char   pLetter = t.ChooseLetter();
                char   cLetter = 'X';
                if (pLetter.Equals('X'))
                {
                    cLetter = 'O';
                }
                Console.WriteLine("Player's Letter = " + pLetter);
                Console.WriteLine("Computer's Letter = " + cLetter);
                t.PrintBoard(board);

                Player p = t.FirstPlayToss();

                bool       gameOver = false;
                GameStatus gstatus  = t.CheckCurrentStatus(board, pLetter, cLetter);

                while (!gameOver)
                {
                    if (p == Player.USER)
                    {
                        t.MakePlayerMove(board, pLetter);
                        gstatus = t.CheckCurrentStatus(board, pLetter, cLetter);
                        p       = Player.COMPUTER;
                    }
                    else
                    {
                        t.MakeComputerMove(board, cLetter, pLetter);
                        gstatus = t.CheckCurrentStatus(board, pLetter, cLetter);
                        p       = Player.USER;
                    }

                    if (gstatus != GameStatus.CONTINUE)
                    {
                        gameOver = true;
                    }
                }

                if (gstatus == GameStatus.WON)
                {
                    Player k = t.WhoWon(board, pLetter, cLetter);
                }

                Console.WriteLine("Do you want to play again(Y/N)");
                string a = Console.ReadLine();
                if (a[0] == 'Y')
                {
                    playAgain = true;
                }
                else
                {
                    playAgain = false;
                }
            }
        }
Пример #10
0
 private void ResetBoard()
 {
     var newGame = new TicTacToe("O", "X");
 }
Пример #11
0
        static void Main(string[] args)
        {
            int size = 3;
            var game = new TicTacToe(size, new WinnerSelector(size));
            var fieldConsoleOutput = new FieldConsoleOutput(size, game.Steps);

            Console.WriteLine("Новая игра");

            game.DoStep(new Step(1, 1, TypeStep.X));
            fieldConsoleOutput.Show();

            game.DoStep(new Step(2, 2, TypeStep.O));
            fieldConsoleOutput.Show();

            game.DoStep(new Step(1, 2, TypeStep.X));
            fieldConsoleOutput.Show();

            game.DoStep(new Step(1, 3, TypeStep.O));
            fieldConsoleOutput.Show();

            game.DoStep(new Step(2, 1, TypeStep.X));
            fieldConsoleOutput.Show();

            game.DoStep(new Step(3, 1, TypeStep.O));
            fieldConsoleOutput.Show();

            Console.WriteLine($"Победитель: {game.GetWinner()}");
            Console.WriteLine();

            Console.WriteLine("Новая игра");
            game.StartNewGame();

            game.DoStep(new Step(1, 1, TypeStep.X));
            fieldConsoleOutput.Show();

            game.DoStep(new Step(2, 1, TypeStep.O));
            fieldConsoleOutput.Show();

            game.DoStep(new Step(1, 2, TypeStep.X));
            fieldConsoleOutput.Show();

            game.DoStep(new Step(1, 3, TypeStep.O));
            fieldConsoleOutput.Show();

            game.DoStep(new Step(2, 2, TypeStep.X));
            fieldConsoleOutput.Show();

            game.DoStep(new Step(3, 2, TypeStep.O));
            fieldConsoleOutput.Show();

            Console.WriteLine($"Победитель: {game.GetWinner()}");
            Console.WriteLine();

            game.DoStep(new Step(3, 3, TypeStep.X));
            fieldConsoleOutput.Show();

            Console.WriteLine($"Победитель: {game.GetWinner()}");
            Console.WriteLine();

            Console.ReadKey();
        }
Пример #12
0
        /// <summary>
        /// Adding the positions in array filled by user and cpu and displaying it on board.
        /// </summary>
        /// <param name="boardPositions">The board positions.</param>
        /// <param name="choosingCharacter">The choosing character.</param>
        /// <param name="tossResult">The toss result.</param>
        public static void MarkingPositions(char[] boardPositions, char[] choosingCharacter, int tossResult)
        {
            int checkForWin;
            int index = tossResult;

            //do while loop moves until game is drawn or any player wins.
            do
            {
                //switches input values between cpu and player one by one. Index value starts from toss result.
                switch (index % 2)
                {
                //input by user.
                case 0:
                {
                    Console.WriteLine("Please enter the position between 1 to 9 where you want to fill your character");
                    while (true)
                    {
                        int positionEnteredByUser = Convert.ToInt32(Console.ReadLine());
                        if (positionEnteredByUser <= 9 && positionEnteredByUser >= 1)
                        {
                            //if position in array and board is vacant, character is added.
                            if (boardPositions[positionEnteredByUser] == ' ')
                            {
                                boardPositions[positionEnteredByUser] = choosingCharacter[index % 2];
                                TicTacToe.Board(boardPositions);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("The position is already occupied, please enter position again");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Please enter the correct position to fill");
                        }
                    }
                    break;
                }

                // input done by user.
                case 1:
                {
                    //calling CpuMove method which processes input by cpu and enters in array and displays it on board.
                    TicTacToe.CpuMove(boardPositions, choosingCharacter, index);
                    break;
                }

                default:
                    break;
                }
                // calling CheckingForWinning method to see if game is ending or not.
                checkForWin = TicTacToe.CheckingForWinning(boardPositions);
                //if checkForWin ==0, one of the player has win the game.
                if (checkForWin == 0)
                {
                    if (index % 2 == 0)
                    {
                        Console.WriteLine("You have won the game");
                    }
                    else
                    {
                        Console.WriteLine("Cpu has won the game");
                    }
                    break;
                }
                index++;
                //if checkforWin= -1, than game is neither draw nor won by any player, positions are vacant in board.
            } while (checkForWin == -1);
        }
Пример #13
0
 static void Main(string[] args)
 {
     TicTacToe.PlayGame();
     Console.Read();
 }
Пример #14
0
        static void Main(string[] args)
        {
            TicTacToe t = new TicTacToe();
            t.DisplayTheBoard();
            string inputPos;
            while (!t.GameOver)
            {
                Console.Write("Enter your position (X) To Exit:");
                inputPos = Console.ReadLine();

                if (inputPos.ToLower() == "x")
                {
                    t.GameOver = true;
                    break;
                }
                if (!t.PutMark(inputPos, 'X'))
                    Console.WriteLine(t.Message);
                else
                {
                    t.DisplayTheBoard();
                }

            }
        }