-
Notifications
You must be signed in to change notification settings - Fork 1
/
terratri.py
212 lines (164 loc) · 5.08 KB
/
terratri.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
207
208
209
210
211
212
"""
the rules of terratri
=====================
step notation
-------------
We represent the history as a string of 2 chars (steps) per move.
lowercase = red
uppercase = blue
'n','N' = north
's','S' = south
'e','E' = east
'w','W' = west
'f','F' = fort
'x','X' = pass (second step only)
board notation
--------------
We represent either as a 5 x 5 array (list of lists)
of characters (a "grid"), or as a flattened 25-character
string (a "board").
In both cases, the characters are:
' '= unclaimed square
'.' = red square
'r' = red pawn
'R' = red fort
'E' = red fort + red pawn
'_' = blue square
'b' = blue pawn
'B' = blue fort
'L' = blue fort + blue pawn
"""
import string
kStartBoard =\
( ' b '
' '
' '
' '
' r ' )
# board string -> grid
def boardToGrid(board):
rows = [board[off*5:off*5+5] for off in range(5)]
return [list(row) for row in rows]
# grid -> board string
def gridToBoard(grid):
return ''.join(''.join(row) for row in grid)
startGrid = lambda : boardToGrid(kStartBoard)
# steps string -> side char
def whoseTurn(steps):
stepCount = len(steps)
turnCount = stepCount / 2
return 'r' if turnCount % 2 == 0 else 'b'
# side -> grid -> (x, y, hasFort)
def findPawn(pawn, grid):
fortPawn = 'E' if pawn == 'r' else 'L'
for y in range(5):
for x in range(5):
if grid[y][x] in (pawn, fortPawn):
return x, y, grid[y][x] == fortPawn
# steps string -> grid
def after(steps):
grid = startGrid()
for s in steps:
if s == 'n': move('r', 'n', grid)
elif s == 's': move('r', 's', grid)
elif s == 'e': move('r', 'e', grid)
elif s == 'w': move('r', 'w', grid)
elif s == 'f': fortify('r', grid)
elif s == 'x': pass
elif s == 'N': move('b', 'n', grid)
elif s == 'S': move('b', 's', grid)
elif s == 'E': move('b', 'e', grid)
elif s == 'W': move('b', 'w', grid)
elif s == 'F': fortify('b', grid)
elif s == 'X': pass
return grid
# -> (x2, y2)
def relative(dir, x, y):
if dir == 'n': return x, y - 1
if dir == 's': return x, y + 1
if dir == 'e': return x + 1, y
if dir == 'w': return x - 1, y
raise ValueError('relative() expected [nsew], got %r' % dir)
def pawnAndFort(pawn):
return 'E' if pawn == 'r' else 'L'
# edits grid in place
def move(pawn, dir, grid):
x1, y1, hasFort = findPawn(pawn, grid)
x2, y2 = relative(dir, x1, y1)
# move pawn to the new square:
if grid[y2][x2] == pawn.upper():
grid[y2][x2] = pawnAndFort(pawn)
else:
grid[y2][x2] = pawn
# repaint the old square:
if hasFort:
grid[y1][x1] = pawn.upper()
else:
grid[y1][x1] = '.' if pawn == 'r' else '_'
# edits grid in place:
def fortify(pawn, grid):
x, y, hasFort = findPawn(pawn, grid)
assert not hasFort, "?? already a fort at (%s,%s)" % (x, y)
grid[y][x] = pawnAndFort(pawn)
def inBounds(x, y):
return x in range(5) and y in range(5)
def enemiesOf(side):
return ['b','B','L'] if side == 'r' else ['r','R','E']
kRows = 'abcde'
kCols = '54321'
# int -> int -> str ex: (2,4) -> 'c1'
def sq(x, y):
return kRows[x] + kCols[y]
def squareCount(side, grid):
count = 0
want = '.' if side == 'r' else '_'
for y in range(5):
for x in range(5):
if grid[y][x] == want:
count += 1
return count
def opposite(dir):
if dir == 'n': return 's'
if dir == 's': return 'n'
if dir == 'e': return 'w'
if dir == 'w': return 'e'
return None
def validSteps(side, grid, steps):
res = []
fixCase = string.lower if side=='r' else string.upper
lastStep = steps[-1:]
secondStep = lastStep.islower() if side == 'r' else lastStep.isupper()
if secondStep:
res.append((fixCase('x'), 'end'))
enemies = enemiesOf(side)
x, y, hasFort = findPawn(side, grid)
for step in ['n','s','e','w']:
x2, y2 = relative(step, x, y)
# prevent 'pass' turns that leave you on the same square
# (but this is allowed if the territory changed, so we have to check)
if secondStep and step == opposite(lastStep.lower()):
beforeLast = after(steps[:-1])
afterThis = after(steps + step)
if beforeLast == afterThis:
continue
if inBounds(x2, y2) and grid[y2][x2] not in enemies:
res.append((fixCase(step), sq(x2,y2)))
if not hasFort and squareCount(side, grid) >= 5:
res.append((fixCase('f'), sq(x,y)))
return dict(res)
# grid -> Maybe side
def winner(grid):
rCount = 0
bCount = 0
for y in range(5):
for x in range(5):
if grid[y][x] in ('R','E'): rCount += 1
if grid[y][x] in ('B','L'): bCount += 1
if rCount == 5: return 'r'
if bCount == 5: return 'b'
return None
def niceHistory(steps):
moves = [steps[off*4:off*4+4] for off in range(len(steps)/4 + 1)]
if '' in moves: moves.remove('')
return [string.strip('%s %s' % (move[:2], move[2:]))
for move in moves]