aboutsummaryrefslogtreecommitdiff
path: root/imago/engine/core.py
blob: 2bf7d5a9371b78a2a9e2d411e69133a19a1e6edf (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
67
"""Imago GTP engine"""

from imago.data.enums import DecisionAlgorithms
from imago.engine.createDecisionAlgorithm import create as createDA
from imago.gameLogic.gameState import GameState

DEF_SIZE = 9
DEF_KOMI = 5.5
DEF_ALGORITHM = DecisionAlgorithms.KERAS

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

    def __init__(self, decisionAlgorithmId = DEF_ALGORITHM):
        self.komi = DEF_KOMI
        self.gameState = GameState(DEF_SIZE)
        self.daId = decisionAlgorithmId
        self.da = createDA(self.daId, self.gameState.lastMove)

    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)
        self.da = createDA(self.daId, self.gameState.lastMove)

    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()
        self.da.clearBoard()

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

    def setFixedHandicap(self, stones):
        """Sets handicap stones in fixed vertices."""
        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"""
        if vertex == "pass":
            self.gameState.passForPlayer(color)
            self.da.forceNextMove(vertex)
            return
        row = vertex[0]
        col = vertex[1]
        self.gameState.playMoveForPlayer(row, col, color)
        self.da.forceNextMove(vertex)

    def genmove(self, color):
        """Returns a list representing coordinates of the board in the form (row, col)."""
        coords = self.da.pickMove()
        self.play(color, coords)
        return coords

    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()