Пример #1
0
        public static bool Game(Board board, bool computerMove)
        {
            Console.Clear();
            Console.WriteLine(board.ToString());
            PossibleState actualstate = new PossibleState(board, new Move[] { });
            Move          m           = new Move(0, 0);

            if (computerMove)
            {
                if (CheckWin(board) == 1)
                {
                    Console.WriteLine("Player wins");
                    return(false);
                }

                bool isMirror = false;
                foreach (var item in possibleStates)
                {
                    if (item.BoardEquals(board, out isMirror))
                    {
                        actualstate = item;
                        break;
                    }
                }
                m = actualstate.GetMove();
                if (isMirror)
                {
                    board[m.From / 3, 2 - m.From % 3] = 0;
                    board[m.To / 3, 2 - m.To % 3]     = 2;
                }
                else
                {
                    board[m.From / 3, m.From % 3] = 0;
                    board[m.To / 3, m.To % 3]     = 2;
                }
            }
            else
            {
                if (CheckWin(board) == 2)
                {
                    Console.WriteLine("Computer wins");
                    return(true);
                }
                Console.Write("From: "); int x = Convert.ToInt32(Console.ReadLine());
                Console.Write("To:   "); int y = Convert.ToInt32(Console.ReadLine());

                // TODO: check if valid move

                board[x / 3, x % 3] = 0;
                board[y / 3, y % 3] = 1;
            }
            if (CheckDraw(board))
            {
                return(false);
            }
            //check pawns amount
            int Bamount = 0;
            int Wamount = 0;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (board[i, j] == 2)
                    {
                        Bamount++;
                    }
                    if (board[i, j] == 1)
                    {
                        Wamount++;
                    }
                }
            }
            if (Bamount == 0)
            {
                return(false);              //computer lost all pawns
            }
            if (Wamount == 0)
            {
                return(true);              //computer wins - player has no pawns
            }
            //
            bool result = Game(board, !computerMove);

            if (result && computerMove)
            {
                //computer won and it is his move
                actualstate.RewardMove(m);
            }
            return(result);
        }
Пример #2
0
        static void Main()
        {
            ///////INIT
            Move m03 = new Move(0, 3);
            Move m04 = new Move(0, 4);
            Move m13 = new Move(1, 3);
            Move m14 = new Move(1, 4);
            Move m15 = new Move(1, 5);
            Move m24 = new Move(2, 4);
            Move m25 = new Move(2, 5);
            Move m36 = new Move(3, 6);
            Move m37 = new Move(3, 7);
            Move m46 = new Move(4, 6);
            Move m47 = new Move(4, 7);
            Move m48 = new Move(4, 8);

            possibleStates = new PossibleState[]
            {
                //2 round
                new PossibleState(new Board(2, 2, 2, 1, 0, 0, 0, 1, 1), new Move[] { m13, m14, m25 }),
                new PossibleState(new Board(2, 2, 2, 0, 1, 0, 1, 0, 1), new Move[] { m03, m04 }),
                //4 round
                new PossibleState(new Board(0, 2, 2, 2, 1, 1, 1, 0, 0), new Move[] { m15, m24 }),
                new PossibleState(new Board(0, 2, 2, 0, 2, 1, 1, 0, 0), new Move[] { m15, m46, m47 }),
                new PossibleState(new Board(2, 2, 0, 1, 0, 1, 0, 0, 1), new Move[] { m13, m14, m15 }),
                new PossibleState(new Board(2, 0, 2, 1, 0, 0, 0, 0, 1), new Move[] { m25 }),
                new PossibleState(new Board(2, 0, 2, 1, 1, 0, 0, 1, 0), new Move[] { m04, m24, m25 }),
                new PossibleState(new Board(0, 2, 2, 1, 2, 0, 0, 0, 1), new Move[] { m13, m25, m47, m48 }),
                new PossibleState(new Board(0, 2, 2, 0, 1, 0, 0, 0, 1), new Move[] { m24, m25 }),
                new PossibleState(new Board(2, 0, 2, 2, 1, 0, 0, 0, 1), new Move[] { m04, m24, m25, m36 }),
                new PossibleState(new Board(2, 0, 2, 2, 0, 1, 0, 1, 0), new Move[] { m36, m37 }),
                new PossibleState(new Board(0, 2, 2, 0, 1, 0, 1, 0, 0), new Move[] { m24, m25 }),
                //6 round
                new PossibleState(new Board(0, 2, 0, 2, 1, 1, 0, 0, 0), new Move[] { m15, m36 }),
                new PossibleState(new Board(2, 0, 0, 2, 1, 0, 0, 0, 0), new Move[] { m04, m36 }),
                new PossibleState(new Board(2, 0, 0, 2, 2, 1, 0, 0, 0), new Move[] { m36, m47 }),
                new PossibleState(new Board(2, 0, 0, 1, 1, 1, 0, 0, 0), new Move[] { m04 }),
                new PossibleState(new Board(0, 2, 0, 1, 2, 0, 0, 0, 0), new Move[] { m13, m47 }),
                new PossibleState(new Board(0, 0, 2, 2, 1, 0, 0, 0, 0), new Move[] { m25, m24, m36 }),
                new PossibleState(new Board(0, 0, 2, 2, 2, 1, 0, 0, 0), new Move[] { m36, m47 })
            };

            //0 puste
            //1 gracz
            //2 komputer
            int gamesAmount        = 0;
            int gamesWonByComputer = 0;

            while (true)
            {
                Board board = new Board(2, 2, 2, 0, 0, 0, 1, 1, 1);
                if (Game(board, false))
                {
                    gamesWonByComputer++;
                }
                gamesAmount++;
                Console.WriteLine(board.ToString());
                Console.WriteLine("Comp. Winrate: {0:00}%", gamesWonByComputer / (float)gamesAmount * 100);
                Console.ReadKey();
            }
        }