Exemplo n.º 1
0
    def run(self, parsedInput: list, gameBoard: GameBoard) -> None:

        # Save opponent move
        x = int(parsedInput[1])
        y = int(parsedInput[2])
        gameBoard._board[x][y] = OPPONENT

        ## Plan new move
        x, y = AI().getBestMove(gameBoard)
        # Print out the move
        print(f'{x},{y}')
        # Save the move
        gameBoard._board[x][y] = BRAIN
Exemplo n.º 2
0
    def run(self, gameBoard: GameBoard) -> int:

        # Reset board
        gameBoard._board = gameBoard.create(gameBoard._size)
        # Get information
        self.setBoard(gameBoard)

        ## Plan new move
        x, y = AI().getBestMove(gameBoard)
        # Print out the move
        print(f'{x},{y}')
        # Save the move
        gameBoard._board[x][y] = BRAIN
Exemplo n.º 3
0
 def __init__(self, gameBoard=None, x=-1, y=-1, score=0):
     self._coords = [x, y, score]
     if gameBoard:
         self._board = gameBoard.copy()
     else:
         self._board = GameBoard(0)
     self._children = []
Exemplo n.º 4
0
    def run(self, parsedInput: list, gameBoard: GameBoard) -> None:

        x = int(gameBoard._size / 2)
        y = int(gameBoard._size / 2)

        # Print out the move
        print(f'{x},{y}')
        # Save the move
        gameBoard._board[x][y] = BRAIN
Exemplo n.º 5
0
def test_normal_case():

    # Set
    args = ['INFO', 'timeout_turn', '2']
    gameBoard = GameBoard(19)

    # Run
    Info(args, gameBoard)

    # Test
    assert gameBoard._settings['timeout_turn'] == 2
Exemplo n.º 6
0
 def __init__(self):
     self._gameBoard = GameBoard(19)
     self._commands = {
         'START': Start,
         'TURN': Turn,
         'BEGIN': Begin,
         'BOARD': Board,
         'INFO': Info,
         'END': End,
         'ABOUT': About
     }
Exemplo n.º 7
0
def test_ko_key(capsys):

    # Set
    args = ['INFO', 'wrongKey', '2']
    gameBoard = GameBoard(19)

    # Run
    Info(args, gameBoard)

    # Test
    redir = capsys.readouterr()
    assert redir.out == 'ERROR Info command - Invalid key.\n'
Exemplo n.º 8
0
def test_ko_args(capsys):

    # Set
    args = ['INFO', '_']
    gameBoard = GameBoard(19)

    # Run
    Info(args, gameBoard)

    # Test
    redir = capsys.readouterr()
    assert redir.out == 'ERROR Info command - Invalid arguments.\n'
Exemplo n.º 9
0
def test_normal_case(capsys):

    # Set
    args = ['BEGIN']
    gameBoard = GameBoard(20)
    # Run
    Begin(args, gameBoard)
    # Tests
    assert gameBoard._board[10][10] == 1

    redir = capsys.readouterr()
    assert redir.out == '10,10\n'
Exemplo n.º 10
0
    def generate(self, gameBoard: GameBoard, depth: int, player: int,
                 node: Node):

        if depth == 0:
            return

        node._board = gameBoard.copy()
        for i in range(gameBoard._size):
            for j in range(gameBoard._size):
                if gameBoard._board[i][j] is EMPTY and gameBoard.hasNeighbor(
                        i, j):
                    # Simulate move
                    gameBoard._board[i][j] = player
                    # Generate children
                    score = node.eval(gameBoard, [i, j], player)
                    node.setChild(gameBoard.copy(), [i, j, score])
                    # Get to next level
                    nextPlayer = BRAIN if player is OPPONENT else OPPONENT
                    self.generate(gameBoard, depth - 1, nextPlayer,
                                  node._children[-1])
                    # Undo Simulation
                    gameBoard._board[i][j] = EMPTY
Exemplo n.º 11
0
    def eval(self, gameBoard: GameBoard, coords: list, player: int) -> int:

        # Check for victory
        win = gameBoard.isWinning(coords, player)
        if win == BRAIN:
            return inf
        elif win == OPPONENT:
            return 10000000000

        evals = [
            # Vertical
            self.evalDir(gameBoard, [SOUTH, NORTH], coords, player),
            # Horizontal
            self.evalDir(gameBoard, [EAST, WEST], coords, player),
            # Diagonal (Top-Left - Bottom-Right)
            self.evalDir(gameBoard, [NORTHWEST, SOUTHEAST], coords, player),
            # Diagonal (Top-Right - Bottom-Left)
            self.evalDir(gameBoard, [NORTHEAST, SOUTHWEST], coords, player)
        ]

        transpose = list(map(list, zip(*evals)))
        return max(max(res for res in transpose))
Exemplo n.º 12
0
    def setBoard(self, gameBoard: GameBoard) -> list:
        def checkInput(coords: str) -> int:
            cs = coords.split(',')
            if len(cs) != 3:
                return 84
            for c in cs:
                if isInt(c) is False:
                    return 84
            if int(cs[2]) not in [1, 2, 3]:
                return 84
            if int(cs[0]) not in range(gameBoard._size) or int(
                    cs[1]) not in range(gameBoard._size):
                return 84
            return 0

        while True:
            inpt = input()
            if inpt == "DONE":
                break
            elif checkInput(inpt) == 84:
                Log('ERROR', 'Board command - Wrong coordinates format.')
            else:
                cs = inpt.split(',')
                gameBoard._board[int(cs[0])][int(cs[1])] = int(cs[2])
Exemplo n.º 13
0
    def run(self, parsedInput: list, gameBoard: GameBoard) -> None:

        key = parsedInput[1]
        value = parsedInput[2]

        gameBoard._settings[key] = int(value)