Exemplo n.º 1
0
        static void Main()
        {
            GameState game = new GameState();

            bool running = true;

            while (running)
            {
                game.PrintGame();

                Console.WriteLine("Enter a move in the form (row col): ");
                string move = Console.ReadLine();


                if (!CheckUserInput(move, game.CurrentBoard.Rows, game.CurrentBoard.Cols))
                {
                    Console.WriteLine("Incorrect format, please try again.");
                    continue;
                }

                int row = Int16.Parse(move.Split(' ')[0]);
                int col = Int16.Parse(move.Split(' ')[1]);

                game.MakeMove(row, col);

                char winner = game.CheckWin();
                if (winner != '\0')
                {
                    game.PrintGame();

                    Console.WriteLine($"{winner} Won The Game!");
                    running = false;
                }

                if (game.CheckDraw())
                {
                    game.PrintGame();

                    Console.WriteLine($"The Game is Drawn!");
                    running = false;
                }
            }
        }
Exemplo n.º 2
0
        static int GamePlay(playerFunc p1func, playerFunc p2func)
        {
            var state   = new GameState();
            var pFuncs  = new playerFunc[] { p1func, p2func };
            var players = new GameCell[] { GameCell.Player1, GameCell.Player2 };

            var currentPlayer = 0;
            var movesCount    = 0;

            while (movesCount < 9)
            {
                var move     = pFuncs[currentPlayer](state);
                var newState = state.MakeMove(move.Item1, move.Item2, players[currentPlayer]);
                if (newState == null)
                {
                    if (movesCount == 0)
                    {
                        Console.WriteLine("Skipping...");
                        currentPlayer ^= 1;
                    }
                    else
                    {
                        Console.WriteLine("Invalid move! Play again.");
                    }
                    continue;
                }

                state = newState;

                if (state.IsWinning(players[currentPlayer]))
                {
                    Console.WriteLine(state);
                    return(currentPlayer + 1);
                }

                currentPlayer ^= 1;
                ++movesCount;
            }

            Console.WriteLine(state);
            return(0);
        }
Exemplo n.º 3
0
        static GameResult BuildTree(
            Dictionary <GameState, GameEdge> tree,
            GameState state,
            GameCell currentPlayer,
            GameCell otherPlayer)
        {
            if (tree.ContainsKey(state))
            {
                return(tree[state].Result);
            }

            if (state.IsWinning(otherPlayer))
            {
                tree[state] = new GameEdge(-1, -1, GameResult.Losing);
                return(GameResult.Losing);
            }

            GameEdge drawEdge = null;
            GameEdge loseEdge = null;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    var newState = state.MakeMove(i, j, currentPlayer);
                    if (newState == null)
                    {
                        continue;
                    }

                    var result = BuildTree(tree, newState, otherPlayer, currentPlayer);
                    if (result == GameResult.Losing)
                    {
                        tree[state] = new GameEdge(i, j, GameResult.Winning);
                        return(GameResult.Winning);
                    }
                    if (drawEdge == null && result == GameResult.Draw)
                    {
                        drawEdge = new GameEdge(i, j, GameResult.Draw);
                    }
                    if (loseEdge == null && result == GameResult.Winning)
                    {
                        loseEdge = new GameEdge(i, j, GameResult.Losing);
                    }
                }
            }

            if (drawEdge != null)
            {
                tree[state] = drawEdge;
                return(GameResult.Draw);
            }

            if (loseEdge != null)
            {
                tree[state] = loseEdge;
                return(GameResult.Losing);
            }

            tree[state] = new GameEdge(-1, -1, GameResult.Draw);
            return(GameResult.Draw);
        }