Пример #1
0
        public override bool GetNextMove(TicTacToeSign sign, out int row, out int column)
        {
            Steps = 0;
            
            row = -1;
            column = -1;

            int rowtmp = -1;
            int columntmp = -1;
            int max = -100;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (board.CorrectMove(i, j))
                    {
                        Steps++;
                        
                        board.SetSign(i, j, sign);

                        if (board.Winner() != TicTacToeResult.None)
                        {
                            row = i;
                            column = j;

                            board.SetSign(i, j, TicTacToeSign.Empty);
                            break;
                        }
                        else
                        {
                            int e = board.FreeDirection(sign) - board.FreeDirection(TicTacToeHelper.Opponent(sign));

                            if (e > max)
                            {
                                max = e;

                                rowtmp = i;
                                columntmp = j;
                            }
                        }

                        board.SetSign(i, j, TicTacToeSign.Empty);
                    }
                }

                if (row != -1)
                    break;
            }

            if (row == -1)
            {
                row = rowtmp;
                column = columntmp;
            }

            return (row != -1);
        }
Пример #2
0
        private int PlayMax(TicTacToeBoard prevBoard, TicTacToeSign sign)
        {
            int max = -1;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (prevBoard.CorrectMove(i, j))
                    {
                        TicTacToeBoard newBoard = new TicTacToeBoard(prevBoard);
                        newBoard.SetSign(i, j, sign);

                        Steps++;

                        TicTacToeResult result = newBoard.Winner();

                        if (result != TicTacToeResult.None)
                        {
                            if (result == TicTacToeResult.Draw)
                            {
                                if (max < 0)
                                {
                                    max = 0;
                                }
                            }
                            else
                            {
                                max = 1;
                            }
                        }
                        else
                        {
                            int max1 = PlayMin(newBoard, TicTacToeHelper.Opponent(sign));

                            if (max1 > max)
                            {
                                max = max1;
                            }
                        }
                    }
                }
            }

            return(max);
        }
Пример #3
0
        public override bool GetNextMove(TicTacToeSign sign, out int row, out int column)
        {
            row    = -1;
            column = -1;

            Steps = 0;
            int max = -1;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (board.CorrectMove(i, j))
                    {
                        TicTacToeBoard newBoard = new TicTacToeBoard(board);
                        newBoard.SetSign(i, j, sign);

                        Steps++;

                        TicTacToeResult result = newBoard.Winner();

                        if (result != TicTacToeResult.None)
                        {
                            if (result == TicTacToeResult.Draw)
                            {
                                if (max < 0)
                                {
                                    max    = 0;
                                    row    = i;
                                    column = j;
                                }
                            }
                            else
                            {
                                max    = 1;
                                row    = i;
                                column = j;
                            }
                        }
                        else
                        {
                            int max1 = PlayMin(newBoard, TicTacToeHelper.Opponent(sign));

                            if (max1 > max)
                            {
                                max    = max1;
                                row    = i;
                                column = j;
                            }
                        }
                    }
                }
            }

            Debug.WriteLine(String.Format("Sor: {0} Oszlop: {1} Érték: {2} Vizsgálatok: {3}", row, column, max, Steps));
            // MessageBox.Show(String.Format("Sor: {0} Oszlop: {1} Érték: {2} Vizsgálatok: {3}", row, column, max, count));

            BestResult = max;

            return(row != -1);
        }
Пример #4
0
        public override bool GetNextMove(TicTacToeSign sign, out int row, out int column)
        {
            Steps = 0;

            row    = -1;
            column = -1;

            // Ha egy adott lépéssel megnyerné a gép a játékot, akkor ezt a lépést kell választani
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Steps++;

                    if (board.GetSign(i, j) == TicTacToeSign.Empty)
                    {
                        board.SetSign(i, j, sign);

                        if (board.Winner() != TicTacToeResult.None)
                        {
                            row    = i;
                            column = j;

                            board.SetSign(i, j, TicTacToeSign.Empty);
                            break;
                        }

                        board.SetSign(i, j, TicTacToeSign.Empty);
                    }
                }

                if (row != -1)
                {
                    break;
                }
            }

            if (row != -1)
            {
                return(true);
            }

            // Ha egy adott lépéssel az ellenfél nyerné meg a játékot, akkor erre a helyre lépve blokkolni kell az ő lépését
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Steps++;

                    if (board.GetSign(i, j) == TicTacToeSign.Empty)
                    {
                        board.SetSign(i, j, TicTacToeHelper.Opponent(sign));

                        if (board.Winner() != TicTacToeResult.None)
                        {
                            row    = i;
                            column = j;

                            board.SetSign(i, j, TicTacToeSign.Empty);
                            break;
                        }

                        board.SetSign(i, j, TicTacToeSign.Empty);
                    }
                }
            }

            if (row != -1)
            {
                return(true);
            }

            // Egyébként táblázat alapján választunk az üres helyek közül
            for (int i = 0; i < bestMoves.Count; i++)
            {
                Steps++;

                if (board.GetSign(bestMoves[i].Row, bestMoves[i].Column) == TicTacToeSign.Empty)
                {
                    row    = bestMoves[i].Row;
                    column = bestMoves[i].Column;

                    break;
                }
            }

            return(row != -1);
        }