-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.py
63 lines (49 loc) · 2.35 KB
/
backup.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
import numpy as np
import re
class Othello:
def __init__(self):
self.BLACK = 1
self.WHITE = 2
self.EMPTY = 0
self.board = np.zeros((8, 8), dtype=int)
self.board[3, 3], self.board[4, 4] = self.BLACK, self.BLACK
self.board[3, 4], self.board[4, 3] = self.WHITE, self.WHITE
self.opp = {self.BLACK: self.WHITE, self.WHITE: self.BLACK}
self.map = {self.BLACK: "●", self.WHITE: "○", self.EMPTY:"-"}
self.rows = [list(range(x*8, x*8+8)) for x in range(8)]
self.cols = [list(range(x, 64, 8)) for x in range(8)]
self.diags = [list(range(x, 64, 9)) for x in range(8)]+[list(range(x*8, 64, 9)) for x in range(1, 8)]
self.diags = self.diags + [list(range(x, 64, 7)) for x in range(8)]+[list(range(x*8+7, 64, 7)) for x in range(1, 8)]
self.diags = [i for i in self.diags if len(i)>=3]
self.checks = self.rows+self.cols+self.diags
def print(self):
print(" ", end="")
print(" ".join([str(x) for x in range(8)]))
for row in range(len(self.board)):
print(row, end=" ")
for item in self.board[row]:
print(self.map[item], end=" ")
print()
def make_move(self, action, turn):
if action in self.legal_moves(turn):
self.board[action//8, action%8]=turn
else: return False
board = self.board.flatten()
for item in self.checks:
li = [str(board[x]) for x in item]
s = "".join(li)
g1 = [(x.start(), x.end()) for x in re.finditer(r"({})({})+{}".format(turn, self.opp[turn], turn), s)]
for tup in g1:
for i in range(*tup):
self.board[item[i]//8, item[i]%8] = turn
def legal_moves(self, turn):
legals = set()
board = self.board.flatten()
for item in self.checks:
li = [str(board[x]) for x in item]
s = "".join(li)
g1 = [x.end()-1 for x in re.finditer(r"({})({})+{}".format(turn, self.opp[turn], self.EMPTY), s)]
g1 = g1+[x.start() for x in re.finditer(r"{}({})+({})".format(self.EMPTY, self.opp[turn], turn), s)]
for i in range(len(g1)):
legals.add(item[g1[i]])
return legals