aboutsummaryrefslogtreecommitdiff
path: root/imago/engine/core.py
blob: 09603190f41dfa2120029818c54a4b25cbf9d43f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Imago GTP engine"""

from random import randrange

from imago.data.enums import Player
from imago.gameLogic.gameState import GameState

DEF_SIZE = 19
DEF_KOMI = 5.5

class GameEngine:
    """Plays the game of Go."""

    def __init__(self):
        self.komi = DEF_KOMI
        self.gameState = GameState(DEF_SIZE)

    def setBoardsize(self, newSize):
        """Changes the size of the board.
        Board state, number of stones and move history become arbitrary.
        It is wise to call clear_board after this command.
        """
        self.gameState = GameState(newSize)

    def clearBoard(self):
        """The board is cleared, the number of captured stones reset to zero and the move
        history reset to empty.
        """
        self.gameState.clearBoard()

    def setKomi(self, komi):
        """Sets a new value of komi."""
        self.komi = komi

    def setFixedHandicap(self, stones):
        """Sets handicap stones in fixed vertexes."""
        if stones < 1 or stones > 9:
            raise Exception("Wrong number of handicap stones")
        # TODO: Set handicap stones
        return [[0,0], [0,1]]

    def play(self, color, vertex):
        """Plays in the vertex passed as argument"""
        row = vertex[0]
        col = vertex[1]
        self.gameState.playMoveForPlayer(row, col, color)

    def genmove(self, color):
        """The key of this TFG."""
        validCells = []
        board = self.gameState.getBoard().board
        size = self.gameState.size
        for row in range(size):
            for col in range(size):
                if board[row][col] == Player.EMPTY:
                    validCells.append([row, col])
        randIndex = randrange(0, len(validCells))
        move = validCells[randIndex]
        self.gameState.playMoveForPlayer(move[0], move[1], color)
        return move

    def undo(self):
        """The board configuration and number of captured stones are reset to the state
            before the last move, which is removed from the move history.
        """
        self.gameState.undo()