-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_code_file.py
49 lines (38 loc) · 1.11 KB
/
player_code_file.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
import time
import random
import sys
grid_size = 8
def read_file(player_color):
with open("shared_file.txt") as f:
lines = f.readlines()
if len(lines) == 0:
return None
if lines[0].strip('\n') == str(player_color):
temp_grid = []
for line in lines[1:]:
temp_grid.append(line.strip('\n').split(" ")[:-1])
return temp_grid
return None
def select_move(grid, player_color):
while True:
x = random.randint(0, 7)
y = random.randint(0, 7)
if grid[x][y] == 'No' or grid[x][y][0] == player_color:
return x, y
def write_move(move):
str_to_write = '0\n' + str(move[0]) + " " + str(move[1])
with open("shared_file.txt", 'w') as f:
f.write(str_to_write)
def main():
player_color = sys.argv[1]
while True:
while True:
# grid = read_file(player_color)
grid = read_file(player_color)
if grid is not None:
break
time.sleep(.01)
move = select_move(grid, player_color)
write_move(move)
if __name__ == "__main__":
main()