コード例 #1
0
ファイル: Program.cs プロジェクト: rhinohq/TicTacToe
        private static void PlayGame()
        {
            TTTGame         game   = new TTTGame();
            TTTModel        model  = new TTTModel();
            List <TTTModel> models = new List <TTTModel>();
            char            player = 'x';

            ShowBoard(game.Board);

            do
            {
                TTTCoord move;

                do
                {
                    Console.WriteLine("Vertical coordinate:");
                    string vert = Console.ReadLine();

                    Console.WriteLine("Horizontal coordinate:");
                    string hori = Console.ReadLine();

                    move = new TTTCoord(Convert.ToInt32(vert) - 1, Convert.ToInt32(hori) - 1);
                } while (!game.CheckMove(player, move));

                model  = game.PlayMove(player, move);
                player = game.Turn;

                models.Add(model);

                ShowBoard(game.Board);

                Console.WriteLine();
            } while (model.Winnner == default);

            char result = model.Winnner;

            if (result != default)
            {
                Console.WriteLine(result + " wins! " + game.MovesLeft + " moves left.");
            }
            else
            {
                Console.WriteLine("No one wins.");
            }

            Console.WriteLine("\nSaving game to file...\n");

            TSVRecorder tsvRecorder = new TSVRecorder();

            tsvRecorder.SaveGameToCSV(models.ToArray());

            Console.WriteLine("Game saved.");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: rhinohq/TicTacToe
        private static void PlayRandomGames(int numberOfGames)
        {
            char   startPlayer = 'x';
            Random random      = new Random();

            for (int i = 0; i < numberOfGames; i++)
            {
                TTTGame         game   = new TTTGame();
                TTTModel        model  = new TTTModel();
                List <TTTModel> models = new List <TTTModel>();
                char            player = startPlayer;

                do
                {
                    TTTCoord move;

                    do
                    {
                        move = new TTTCoord(random.Next(3), random.Next(3));
                    } while (!game.CheckMove(player, move));

                    model  = game.PlayMove(player, move);
                    player = game.Turn;

                    models.Add(model);
                } while (model.Winnner == default && game.MovesLeft > 0);

                ShowBoard(game.Board);

                char result = model.Winnner;

                if (result != default)
                {
                    Console.WriteLine(result + " wins! " + game.MovesLeft + " moves left.");
                }
                else
                {
                    Console.WriteLine("No one wins.");
                }

                Console.WriteLine("\nSaving game to file...\n");

                TSVRecorder tsvRecorder = new TSVRecorder();
                tsvRecorder.SaveGameToCSV(models.ToArray());

                Console.WriteLine($"Game {i + 1} saved.");

                startPlayer = game.OppositePlayer(player);
            }
        }