Exemplo n.º 1
0
        public Movement GetBestMove(char[] board, int boardSize, int CurrentPlayer, int alpha, int beta, GameLogic logic)
        {
            Movement BestMove = null;
            int iPossibleMoves = board.Count(b => b == '-');

            Random rand = new Random();
            int i = rand.Next(boardSize);
            int j = rand.Next(boardSize);

            while (iPossibleMoves > 0)
            {
                do
                {
                    if (i < boardSize - 1)
                    {
                        i++;
                    }
                    else if (j < boardSize - 1)
                    {
                        i = 0; j++;
                    }
                    else
                    {
                        i = 0; j = 0;
                    }
                }
                while (board[FromMatrixToIndex(i, j)] != '-');

                Movement NewMove = new Movement(i, j);
                iPossibleMoves--;

                char[] NewBoard = (new string(board)).ToCharArray();

                NewBoard[FromMatrixToIndex(NewMove.iRow, NewMove.iCol)] = 'O';
                var result = logic.GetResult(NewBoard);

                if (result == GameResult.NotFinished)
                {
                    Movement tempMove = GetBestMove(NewBoard, 3, -CurrentPlayer, alpha, beta, logic);
                    NewMove.iRank = tempMove.iRank;
                }
                else
                {
                    if (result == GameResult.NotFinished)
                    {
                        NewMove.iRank = 0;
                    }
                    else
                    {
                        if (result == GameResult.WonByO)
                        {
                            NewMove.iRank = -1;
                        }
                        else
                        {
                            if (result == GameResult.WonByX)
                            {
                                NewMove.iRank = 1;
                            }
                        }
                    }
                }

                if (BestMove == null ||
                    (CurrentPlayer == 1 && NewMove.iRank < BestMove.iRank) ||
                    (CurrentPlayer == -1 && NewMove.iRank > BestMove.iRank))
                {
                    BestMove = NewMove;
                }

                if (CurrentPlayer == 1 && BestMove.iRank < beta)
                {
                    beta = BestMove.iRank;
                }

                if (CurrentPlayer == -1 && BestMove.iRank > alpha)
                {
                    alpha = BestMove.iRank;
                }

                if (alpha > beta)
                {
                    iPossibleMoves = 0;
                }
            }

            return BestMove;
        }
Exemplo n.º 2
0
        public bool Move()
        {
            Movement movement = new Movement();

            movement.Move();

            if (movement.CursorX == 2 & movement.CursorY == 0)
            {
                if (this.Board.Board1[0, 0] == ' ')
                {
                    this.Board.Board1[0, 0] = this.Sign;
                    return(this.IfAdded = true);
                }
                else
                {
                    return(this.IfAdded = false);
                }
            }

            else if (movement.CursorX == 6 & movement.CursorY == 0)
            {
                if (this.Board.Board1[0, 1] == ' ')
                {
                    this.Board.Board1[0, 1] = this.Sign;
                    return(this.IfAdded = true);
                }
                else
                {
                    return(this.IfAdded = false);
                }
            }


            else if (movement.CursorX == 10 & movement.CursorY == 0)
            {
                if (this.Board.Board1[0, 2] == ' ')
                {
                    this.Board.Board1[0, 2] = this.Sign;
                    return(this.IfAdded = true);
                }
                else
                {
                    return(this.IfAdded = false);
                }
            }

            else if (movement.CursorX == 2 & movement.CursorY == 2)
            {
                if (this.Board.Board1[1, 0] == ' ')
                {
                    this.Board.Board1[1, 0] = this.Sign;
                    return(this.IfAdded = true);
                }
                else
                {
                    return(this.IfAdded = false);
                }
            }

            else if (movement.CursorX == 6 & movement.CursorY == 2)
            {
                if (this.Board.Board1[1, 1] == ' ')
                {
                    this.Board.Board1[1, 1] = this.Sign;
                    return(this.IfAdded = true);
                }
                else
                {
                    return(this.IfAdded = false);
                }
            }

            else if (movement.CursorX == 10 & movement.CursorY == 2)
            {
                if (this.Board.Board1[1, 2] == ' ')
                {
                    this.Board.Board1[1, 2] = this.Sign;
                    return(this.IfAdded = true);
                }
                else
                {
                    return(this.IfAdded = false);
                }
            }
            else if (movement.CursorX == 2 & movement.CursorY == 4)
            {
                if (this.Board.Board1[2, 0] == ' ')
                {
                    this.Board.Board1[2, 0] = this.Sign;
                    return(this.IfAdded = true);
                }
                else
                {
                    return(this.IfAdded = false);
                }
            }
            else if (movement.CursorX == 6 & movement.CursorY == 4)
            {
                if (this.Board.Board1[2, 1] == ' ')
                {
                    this.Board.Board1[2, 1] = this.Sign;
                    return(this.IfAdded = true);
                }
                else
                {
                    return(this.IfAdded = false);
                }
            }
            else if (movement.CursorX == 10 & movement.CursorY == 4)
            {
                if (this.Board.Board1[2, 2] == ' ')
                {
                    this.Board.Board1[2, 2] = this.Sign;
                    return(this.IfAdded = true);
                }
                else
                {
                    return(this.IfAdded = false);
                }
            }
            else
            {
                return(this.IfAdded = false);
            }
        }