Beispiel #1
0
      protected Player CreatePlayerInstance(string playerType, char playerSymbol)
      {
         Player player;

         switch (playerType.ToLower())
         {
            case "human":
               player = new PlayerHuman(playerSymbol, player_promptForMove);
               break;

            case "ai":
               player = new PlayerAI(playerSymbol, 1000);
               break;

            default:
               player = null;
               break;
         }

         return player;
      }
Beispiel #2
0
        static void Main(string[] args)
        {
            PlayerHuman    gA = new PlayerHuman();
            PlayerComputer gB = new PlayerComputer();

            gA.Name   = "User";
            gB.Name   = "Computer";
            gA.Symbol = 'x';
            gB.Symbol = 'o';

            char[,] board = new char[3, 3] {
                { '1', '2', '3' },
                { '4', '5', '6' },
                { '7', '8', '9' }
            };
            char[,] boardCopy = board.Clone() as char[, ];

            // A loop over players moves
            bool gameOver    = false;
            bool movePlayerA = true;

            for (int round = 0; round < board.Length; ++round)
            {
                Console.Clear();
                DrawBoard(board);

                if (movePlayerA)
                {
                    Console.WriteLine("Current player: " + gA.Name);
                    gameOver    = gA.MakeMove(board, boardCopy);
                    movePlayerA = false;
                }
                else
                {
                    Console.WriteLine("Current player: " + gB.Name);
                    gameOver    = gB.MakeMove(board, boardCopy);
                    movePlayerA = true;
                }

                if (gameOver)
                {
                    break;
                }
            }

            // Game ending
            Console.Clear();
            DrawBoard(board);
            Console.Write("Game over! ");
            if (gameOver)
            {
                Console.Write("The winner is ");
                if (movePlayerA)
                {
                    Console.WriteLine(gB.Name);
                }
                else
                {
                    Console.WriteLine(gA.Name);
                }
            }
            else
            {
                Console.WriteLine("A tie.");
            }
            Console.ReadKey();
        }