コード例 #1
0
        static void Main(string[] args)
        {
            var     ticTacToeBoard = new string[3, 3];
            IPlayer player1;
            IPlayer player2;

            var gameEngine     = new GameEngine();
            var gameVisualizer = new GameVisualizer();
            var gameManager    = new GameManager(gameVisualizer, gameEngine);

            gameVisualizer.printInstructions();

            // Loops until the player has chosen an option
            while (true)
            {
                Console.WriteLine("Choose playing mode - 2PLAYER or AI?");
                var mode = Console.ReadLine().ToUpper();
                if (mode.Equals("2PLAYER"))
                {
                    player1 = new RealPlayer();
                    player2 = new RealPlayer();
                    break;
                }
                else if (mode.Equals("AI"))
                {
                    player1 = new RealPlayer();
                    player2 = new AIPlayer();
                    break;
                }
                Console.WriteLine();
                Console.WriteLine("That is not a valid option");
            }

            gameManager.PlayGame(ticTacToeBoard, player1, player2);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: glenn222/TicTacToe
        [STAThread] //STA-Single Threaded Apartment, refers to the threading model used by the current main thread.
        static void Main(string[] args)
        {
            string[,] TicTacToeBoard = new string[3, 3];
            // Use an interface to encourage polymorphism - ability of an object to take on many forms
            IPlayer player1;
            IPlayer player2;

            VictoryValidator gameEngine     = new VictoryValidator();
            GameVisualizer   gameVisualizer = new GameVisualizer();
            // Inject dependencies
            GameManager gameManager = new GameManager(gameVisualizer, gameEngine);

            //GameWindow gameWindow = new GameWindow();
            //gameWindow.ShowDialog();

            while (true)
            {
                Console.WriteLine("Choose playing mode - 2 Players or Computer(AI)?");
                var mode = Console.ReadLine().ToUpper();

                // Polymorphism - Two objects are of the same type, but they can be interchangable and defined by the user
                if (mode.Equals("2 PLAYERS"))
                {
                    player1 = new RealPlayer();
                    player2 = new RealPlayer();
                    break;
                }
                else if (mode.Equals("AI"))
                {
                    player1 = new RealPlayer();
                    player2 = new AIPlayer();
                    break;
                }
            }

            //Play game just plays game, not about who the players are, so its better to pass in the players to play the game.
            gameManager.PlayGame(TicTacToeBoard, player1, player2);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: glenn222/TicTacToe
        static void Main(string[] args)
        {
            string[,] TicTacToeBoard = new string[3, 3];
            // Use an interface to encourage polymorphism - ability of an object to take on many forms
            IPlayer player1;
            IPlayer player2;

            VictoryValidator gameEngine = new VictoryValidator();
            GameVisualizer gameVisualizer = new GameVisualizer();
            // Inject dependencies
            GameManager gameManager = new GameManager(gameVisualizer, gameEngine);

            //GameWindow gameWindow = new GameWindow();
            //gameWindow.ShowDialog();

            while (true)
            {
                Console.WriteLine("Choose playing mode - 2 Players or Computer(AI)?");
                var mode = Console.ReadLine().ToUpper();

                // Polymorphism - Two objects are of the same type, but they can be interchangable and defined by the user
                if (mode.Equals("2 PLAYERS"))
                {
                    player1 = new RealPlayer();
                    player2 = new RealPlayer();
                    break;
                }
                else if (mode.Equals("AI"))
                {
                    player1 = new RealPlayer();
                    player2 = new AIPlayer();
                    break;
                }
            }

            //Play game just plays game, not about who the players are, so its better to pass in the players to play the game.
            gameManager.PlayGame(TicTacToeBoard, player1, player2);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: orzecheudezet/CSharpWSINF
        static void Main(string[] args)
        {
            char playOrNot = ' ';

            Board plansza = new Board();

            Console.Write("Enter the first player nick: ");
            string nick1 = Console.ReadLine();

            Console.Write("Enter the secound player nick: ");
            string     nick2  = Console.ReadLine();
            RealPlayer gracz1 = new RealPlayer(nick1, plansza);
            RealPlayer gracz2 = new RealPlayer(nick2, plansza);

            while (playOrNot != 'n')
            {
                playOrNot = ' ';

                Console.Clear();
                Random rand = new Random((int)DateTime.Now.Ticks);
                int    los  = rand.Next(2) + 1;
                plansza.InitializeBoard();
                gracz1.ShowScore();
                gracz2.ShowScore();
                Console.WriteLine();


                if (los == 1)
                {
                    Console.WriteLine(gracz1.Nick + " starts a game!");
                    gracz1.SignChoose();
                    if (gracz1.Sign == 'O')
                    {
                        gracz2.Sign = 'X';
                    }
                    if (gracz1.Sign == 'X')
                    {
                        gracz2.Sign = 'O';
                    }
                }
                else
                {
                    Console.WriteLine(gracz2.Nick + " starts a game!");
                    gracz2.SignChoose();
                    if (gracz2.Sign == 'O')
                    {
                        gracz1.Sign = 'X';
                    }
                    if (gracz2.Sign == 'X')
                    {
                        gracz1.Sign = 'O';
                    }
                }



                Console.Clear();

                if (los == 1)
                {
                    while (true)
                    {
                        plansza.PrintBoard();

                        while (gracz1.IfAdded != true)
                        {
                            gracz1.Move();
                        }
                        gracz1.IfAdded = false;
                        gracz1.CheckIfWin();
                        gracz2.CheckIfWin();
                        Console.Clear();

                        if (gracz1.IfWon == true || gracz2.IfWon == true || plansza.IfFull() == true)
                        {
                            break;
                        }


                        plansza.PrintBoard();

                        while (gracz2.IfAdded != true)
                        {
                            gracz2.Move();
                        }
                        gracz2.IfAdded = false;

                        gracz1.CheckIfWin();
                        gracz2.CheckIfWin();
                        Console.Clear();

                        if (gracz1.IfWon == true || gracz2.IfWon == true || plansza.IfFull() == true)
                        {
                            break;
                        }
                    }
                }
                if (los == 2)
                {
                    while (plansza.IfFull() != true)
                    {
                        plansza.PrintBoard();

                        while (gracz2.IfAdded != true)
                        {
                            gracz2.Move();
                        }
                        gracz2.IfAdded = false;

                        gracz1.CheckIfWin();
                        gracz2.CheckIfWin();
                        Console.Clear();

                        if (gracz1.IfWon == true || gracz2.IfWon == true || plansza.IfFull() == true)
                        {
                            break;
                        }

                        plansza.PrintBoard();

                        while (gracz1.IfAdded != true)
                        {
                            gracz1.Move();
                        }
                        gracz1.IfAdded = false;

                        gracz1.CheckIfWin();
                        gracz2.CheckIfWin();
                        Console.Clear();

                        if (gracz1.IfWon == true || gracz2.IfWon == true || plansza.IfFull() == true)
                        {
                            break;
                        }
                    }
                }


                if (gracz1.IfWon == true)
                {
                    Console.WriteLine("Player: " + gracz1.Nick + "( " + gracz1.Sign + " )" + " is a winner!");
                    gracz1.ScoreIncrement();
                }

                else if (gracz2.IfWon == true)
                {
                    Console.WriteLine("Player: " + gracz2.Nick + "( " + gracz2.Sign + " )" + " is a winner!");
                    gracz2.ScoreIncrement();
                }
                else
                {
                    Console.WriteLine("It's a draw!");
                }

                plansza.PrintBoard();
                gracz1.RemoveSign();
                gracz2.RemoveSign();
                Console.WriteLine("Do you want to play again?");



                while (playOrNot != 'y' & playOrNot != 'n')
                {
                    Console.WriteLine("Press y on your keyboard if you want playe again or press n if you want to exit.");
                    playOrNot = Console.ReadKey().KeyChar;
                }


                //Console.ReadKey();
            }
        }