예제 #1
0
    Space GetNextMove(BoardState board, Cell jogador)
    {
        Space?     bestSpace  = null;
        var        openSpaces = board.AvailableCells();
        BoardState newBoard;

        for (int i = 0; i < openSpaces.Length; i++)
        {
            newBoard = board.Clone();
            var newSpace = openSpaces[i];

            newBoard.SetCellValue(newSpace.Position, jogador);

            if (newBoard.State == MatchState.InProgress && newBoard.AvailableCells().Any())
            {
                var next = flip(jogador);

                var tempMove = GetNextMove(newBoard, next);

                newSpace.Rank = tempMove.Rank;
            }
            else
            {
                if (newBoard.State == MatchState.Player1Won && jogadorIA == Cell.Player2)
                {
                    newSpace.Rank--;
                }
                else if (newBoard.State == MatchState.Player2Won && jogadorIA == Cell.Player2)
                {
                    newSpace.Rank++;
                }
                else if (newBoard.State == MatchState.Player2Won && jogadorIA == Cell.Player1)
                {
                    newSpace.Rank--;
                }
                else if (newBoard.State == MatchState.Player1Won && jogadorIA == Cell.Player1)
                {
                    newSpace.Rank++;
                }
            }


            if (bestSpace == null ||
                (jogador == oponente && newSpace.Rank < bestSpace.Value.Rank) ||
                (jogador == jogadorIA && newSpace.Rank > bestSpace.Value.Rank))
            {
                bestSpace = newSpace;
            }
        }

        return(bestSpace.Value);
    }