/
game.py
59 lines (52 loc) · 1.49 KB
/
game.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
import copy
import board
import state
def playernum(r):
return (r % 2) + 1
def read_pos(prompt):
p = int(raw_input(prompt))
if p in range(9):
return p
else:
print "Illegal input! Valid range: 0-8."
return read_pos(prompt)
def read_sym(prompt):
p = raw_input(prompt)
if p in ['X','O']:
return p
else:
print "Illegal input! Valid inputs: X, O."
return read_pos(prompt)
def turn(r, board):
print "Player %d's turn:" % playernum(r)
p1 = read_pos('Enter first move [0-8] ')
p2 = read_pos('Enter second move [0-8] ')
newboard = copy.deepcopy(board)
try:
newboard[p1].append(r)
newboard[p2].append(r)
except:
print "Field has already collapsed"
return turn(r, board)
return newboard
def prompt_resolve(r, cycle):
print "Player %d, collapse the following cycle:" % playernum(r+1), cycle
pos = read_pos("Enter a position ")
sym = read_sym("Enter a symbol (X/O) ")
sym_parity = {'X':0 , 'O':1}[sym]
if not sym_parity in [i % 2 for i in board[pos]]:
print 'Illegal choice'
return prompt_resolve(r, cycle)
return pos, sym
def play():
b = board.init()
for r in range(9):
b = turn(r, b)
board.draw(b)
c = state.find_cycle(b)
if c:
pos, symbol = prompt_resolve(r, c)
b = state.collapse(b, pos, symbol)
if state.won(b):
print "Player %d won!" % playernum(r)
break