-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.py
206 lines (148 loc) · 5.08 KB
/
engine.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# -*- coding: utf-8 -*-
from builtins import next
from itertools import cycle
from operator import itemgetter
import random
try:
from PyQt4.QtCore import pyqtSignal, QObject, QTimer
except ImportError:
from PyQt5.QtCore import pyqtSignal, QObject, QTimer
class Player(object):
''' Represent a player of any kind
'''
def __init__(self, role, board):
self.role = role
self.board = board
def __str__(self):
return str(self.role)
def headsUp(self):
''' A way to notify a player that it's their turn. A machine would
need such behavior so it knows it's time to make their move.
'''
pass
class HumanPlayer(Player):
''' Represents a human player
'''
def acceptInput(self, position):
''' A human player might require UI, CLI or other means to submit their
move.
'''
self.board.move(position, self)
class MachinePlayer(Player):
''' Represents a machine player
Might be powered by AI or a simple random number generator
'''
pass
class DumbMachinePlayer(MachinePlayer):
''' The Dumb Machine Player is pretty much random
'''
def headsUp(self):
def move():
self.timer.stop()
if self.board.hasAvailablePositions():
self.board.move(
random.choice(self.board.getAvailablePositions()), self
)
self.timer = QTimer()
self.timer.timeout.connect(move)
self.timer.start(500)
class PlayersGroup(object):
''' Groups players together and manages turns
'''
def __init__(self, player_1, player_2):
self.players = [player_1, player_2]
self.playersIterator = cycle(self.players)
self.currentPlayer = None
def current(self):
return self.currentPlayer
def switch(self):
self.currentPlayer = next(self.playersIterator)
class Board(QObject):
''' The board keeps track of available positions and moves
'''
playerMoved = pyqtSignal(int, Player)
WINNING_STATES = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
]
def __init__(self, *args, **kwargs):
super(Board, self).__init__(*args, **kwargs)
self.positions = range(9)
self.state = [''] * len(self.positions)
def hasAvailablePositions(self):
return len([p for p in self.state if p]) != len(self.positions)
def getAvailablePositions(self):
return [
position for position, value in enumerate(self.state) if not value
]
def getWinner(self):
for winningState in self.WINNING_STATES:
# compare winning state to actual state
actualState = [p for p in itemgetter(*winningState)(self.state) if p]
# check if these positions are marked and they belong to the
# same player
if len(actualState) == 3 and len(set(actualState)) == 1:
return actualState[0]
def move(self, position, player):
# prevent accidents
if self.state[position]:
return
self.state[position] = player
self.playerMoved.emit(position, player)
class Engine(QObject):
'''The actual game engine
The game engine is basically a finite state machine that acts as an arbiter
given a board and two players
'''
gameStarted = pyqtSignal()
roundStarted = pyqtSignal()
roundEnded = pyqtSignal()
gameEnded = pyqtSignal()
playerMoved = pyqtSignal(int, Player)
ROLE_X = 'X'
ROLE_0 = '0'
def __init__(self, board, player_1, player_2, *args, **kwargs):
super(Engine, self).__init__(*args, **kwargs)
self.board = board
self.board.playerMoved.connect(self.handlePlayerMove)
self.players = PlayersGroup(player_1, player_2)
self.gameRunning = False
self.awaitingMove = False
def startGame(self):
self.gameStarted.emit()
self.gameRunning = True
if self.board.hasAvailablePositions():
self.players.switch()
self.startRound()
else:
self.endGame()
def startRound(self):
self.roundStarted.emit()
self.awaitingMove = True
self.players.current().headsUp()
def endRound(self):
self.roundEnded.emit()
if self.board.getWinner():
self.endGame()
elif self.gameRunning and self.board.hasAvailablePositions():
self.players.switch()
self.startRound()
else:
self.endGame()
def endGame(self):
self.gameRunning = False
self.gameEnded.emit()
def handlePlayerMove(self, position, player):
self.awaitingMove = False
self.playerMoved.emit(position, player)
self.endRound()
def handleInput(self, position):
# pass input to current player only if they are able to accept it
if self.awaitingMove and getattr(self.players.current(), 'acceptInput', None):
self.players.current().acceptInput(position)