-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
executable file
·199 lines (175 loc) · 6.65 KB
/
solution.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
#!/usr/bin/python3
""" Example Reversi implementation that reads a JSON board and move from
stdin and writes a JSON board to stdout. """
# Libs
import argparse
import json
import sys
def main():
""" Naively returns the same board it was given """
# Read input
args = parse_arguments()
begin_state = json.load(args.infile)
#import pdb
#pdb.set_trace()
#begin_state[u'board'] to access board array
#begin_state[u'move'][u'player'] to access board move, with options u'column', u'row', and u'player'
# Naively copy the input board
# TODO: Implement move and put the resulting board into end_state
board_dim = 8
player = begin_state[u'move'][u'player']
row = begin_state[u'move'][u'row'] - 1
column = begin_state[u'move'][u'column'] - 1
#set move value to player piece
begin_state[u'board'][row * 8 + column] = player
#parse rest of board to see effect of move
#below
for i in range(row + 1, 8):
if begin_state[u'board'][i * 8 + column] == player:
#change pieces if needed
for j in range(row + 1, 8):
if begin_state[u'board'][j * 8 + column] == player:
break
if begin_state[u'board'][j * 8 + column] != 0:
begin_state[u'board'][j * 8 + column] = player
else:
break
break
#above
for i in range(row - 1, -1, -1):
if begin_state[u'board'][i * 8 + column] == player:
# change pieces if needed
for j in range(row - 1, -1, -1):
if begin_state[u'board'][j * 8 + column] == player:
break
if begin_state[u'board'][j * 8 + column] != 0:
begin_state[u'board'][j * 8 + column] = player
else:
break
break
#right
for i in range(column + 1, 8):
if begin_state[u'board'][row * 8 + i] == player:
#change pieces if needed
for j in range(column + 1, 8):
if begin_state[u'board'][row * 8 + j] == player:
break
if begin_state[u'board'][row * 8 + j] != 0:
begin_state[u'board'][row * 8 + j] = player
else:
break
break
#left
for i in range(column - 1, -1, -1):
if begin_state[u'board'][row * 8 + i] == player:
#change pieces if needed
for j in range(column - 1, -1, -1):
if begin_state[u'board'][row * 8 + j] == player:
break
if begin_state[u'board'][row * 8 + j] != 0:
begin_state[u'board'][row * 8 + j] = player
else:
break
break
#diagonals
#downright diagonal
temp_col = column + 1
temp_row = row + 1
while temp_col < 8 and temp_row < 8:
if begin_state[u'board'][temp_row * 8 + temp_col] == player:
#change pieces if needed
temp_col = column + 1
temp_row = row + 1
while temp_col < 8 and temp_row < 8:
if begin_state[u'board'][temp_row * 8 + temp_col] == player:
break
if begin_state[u'board'][temp_row * 8 + temp_col] != 0:
begin_state[u'board'][temp_row * 8 + temp_col] = player
else:
break
temp_col += 1
temp_row += 1
break
temp_col += 1
temp_row += 1
#downleft diagonal
temp_col = column - 1
temp_row = row + 1
while temp_col >= 0 and temp_row < 8:
if begin_state[u'board'][temp_row * 8 + temp_col] == player:
#change pieces if needed
temp_col = column - 1
temp_row = row + 1
while temp_col >= 0 and temp_row < 8:
if begin_state[u'board'][temp_row * 8 + temp_col] == player:
break
if begin_state[u'board'][temp_row * 8 + temp_col] != 0:
begin_state[u'board'][temp_row * 8 + temp_col] = player
else:
break
temp_col -= 1
temp_row += 1
break
temp_col -= 1
temp_row += 1
#upleft diagonal
temp_col = column - 1
temp_row = row - 1
while temp_col >= 0 and temp_row >= 0:
if begin_state[u'board'][temp_row * 8 + temp_col] == player:
#change pieces if needed
temp_col = column - 1
temp_row = row - 1
while temp_col >= 0 and temp_row >= 0:
if begin_state[u'board'][temp_row * 8 + temp_col] == player:
break
if begin_state[u'board'][temp_row * 8 + temp_col] != 0:
begin_state[u'board'][temp_row * 8 + temp_col] = player
else:
break
temp_col -= 1
temp_row -= 1
break
temp_col -= 1
temp_row -= 1
#upright diagonal
temp_col = column + 1
temp_row = row - 1
while temp_col < 8 and temp_row >= 0:
if begin_state[u'board'][temp_row * 8 + temp_col] == player:
#change pieces if needed
temp_col = column + 1
temp_row = row - 1
while temp_col < 8 and temp_row >= 0:
if begin_state[u'board'][temp_row * 8 + temp_col] == player:
break
if begin_state[u'board'][temp_row * 8 + temp_col] != 0:
begin_state[u'board'][temp_row * 8 + temp_col] = player
else:
break
temp_col += 1
temp_row -= 1
break
temp_col += 1
temp_row -= 1
end_state = {}
end_state["board"] = begin_state["board"]
# Write board to stdout
json.dump(end_state, args.outfile)
def parse_arguments():
""" Configures and parses command-line arguments """
argparser = argparse.ArgumentParser(
description="Naive Reversi implementation")
argparser.add_argument("--infile",
nargs="?",
type=argparse.FileType("r"),
default=sys.stdin,
help="Filename of JSON file containing board and move, default stdin")
argparser.add_argument("--outfile",
nargs="?",
type=argparse.FileType("w"),
default=sys.stdout,
help="Filename of JSON file to write board state to, default stdout")
return argparser.parse_args()
if __name__ == "__main__":
main()