示例#1
0
        public static List <Board> GetSmart(int gameCount)
        {
            var boards         = new List <Board>();
            var startingPlayer = 1;



            for (int x = 0; x < gameCount; x++)
            {
                Board board = new Board(startingPlayer);
                //Console.WriteLine("-- Getting Smarter by Playing a New Game --");
                var player = startingPlayer;

                var gameOver = false;
                do
                {
                    if (player == 1)
                    {
                        board.Play(GetNextMove(board, 1));
                    }
                    else
                    {
                        // Computer plays
                        Opponent.MakeMove(boards, board);
                    }
                    if (board.Winner() != null)
                    {
                        //Console.WriteLine(board.WinnerText());
                        gameOver = true;
                    }
                    player = player * -1;
                } while (!gameOver);

                // Save this game;
                if (board.Winner() != null)
                {
                    boards.Add(board);
                    //board.Print();
                    //if (board.Winner() == -1) Console.WriteLine("I'll remember that strategy.");
                    //if (board.Winner() == 1) Console.WriteLine("I won't make that mistake again.");
                }
                startingPlayer = startingPlayer * -1;
            }



            return(boards);
        }
示例#2
0
        static void Main(string[] args)
        {
            board = new Board();

            string line;
            int    choice;

            //menu
            do
            {
                Console.WriteLine("TIC TAC TOE");
                Console.WriteLine("1.  Play against an opponent who moves randomly");
                Console.WriteLine("2.  Play against a smart opponent");
                Console.WriteLine("3.  Quit");

                line   = Console.ReadLine();
                choice = Convert.ToInt32(line);
            } while (choice != 1 && choice != 2 && choice != 3);

            Console.Clear();

            Random rand = new Random();
            bool   playerFirst;

            if (rand.Next(0, 2) == 0)
            {
                //computer goes first
                playerFirst = false;
                Console.WriteLine("The CPU is going first this time.  Press any key to start the game");
            }
            else
            {
                //player goes first
                playerFirst = true;
                Console.WriteLine("You are going first this time.  Press any key to start the game");
            }

            Console.ReadKey();
            Console.Clear();

            cpu = null;

            switch (choice)
            {
            case 1:
                cpu = new RandomOpponent(board);
                break;

            case 2:
                cpu = new SmartOpponent(board);
                break;

            case 3:
                Environment.Exit(0);
                break;
            }

            int movesCount = 0;

            while (movesCount <= board.Tiles.Count && board.NoWinner(out winner))
            {
                if (playerFirst)
                {
                    if (movesCount % 2 == 0)
                    {
                        PlayerMove();
                    }
                    else
                    {
                        CPUMove();
                    }
                }
                else
                {
                    if (movesCount % 2 == 0)
                    {
                        CPUMove();
                    }
                    else
                    {
                        PlayerMove();
                    }
                }

                movesCount++;

                //board.DisplayBoard();
            }

            board.DisplayBoard();

            if (winner == 'P')
            {
                Console.WriteLine("Nice!  You won!");
            }
            else if (winner == 'C')
            {
                Console.WriteLine("Bummer!  You lost!");
            }
            else
            {
                Console.WriteLine("Cat got it!");
            }

            Console.ReadKey();
        }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Tic Tac Toe");
            Console.WriteLine("Play by entering a coordinate (row, col): 1,1; 2,3; 3,1; etc");
            Console.WriteLine();

            var playAgain = true;

            int trainingGames = 1000;

            Console.WriteLine($"Just a second, I am playing {trainingGames} games just to get some strategy down...");
            var boards         = Opponent.GetSmart(trainingGames);
            var startingPlayer = 1;

            do
            {
                bool  gameOver = false;
                Board board    = new Board(startingPlayer);
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("-- New Game --");
                Console.WriteLine();

                var player = startingPlayer;

                do
                {
                    if (player == 1)
                    {
                        board.Print();
                        var moveIsValid = false;
                        int row = -1, col = -1;
                        do
                        {
                            var rowcol = Console.ReadLine();
                            if (rowcol.StartsWith("q"))
                            {
                                Environment.Exit(0);
                            }
                            if (rowcol.Length == 3)
                            {
                                row         = int.Parse(rowcol.Substring(0, 1));
                                col         = int.Parse(rowcol.Substring(2, 1));
                                moveIsValid = board.IsValidMove(row - 1, col - 1);
                            }
                        } while (!moveIsValid);
                        board.Play(row - 1, col - 1, 1);
                    }
                    else
                    {
                        // Computer plays
                        Opponent.MakeMove(boards, board);
                    }
                    if (board.Winner() != null)
                    {
                        Console.WriteLine(board.WinnerText());
                        gameOver = true;
                    }
                    player = player * -1;
                } while (!gameOver);

                // Print final board;
                board.Print();

                // Save this game;
                if (board.Winner() != null)
                {
                    boards.Add(board);
                    if (board.Winner() == -1)
                    {
                        Console.WriteLine("I'll remember that strategy.");
                    }
                    if (board.Winner() == 1)
                    {
                        Console.WriteLine("I won't make that mistake again.");
                    }
                }
                startingPlayer = startingPlayer * -1;
            } while (playAgain);
        }