-
Notifications
You must be signed in to change notification settings - Fork 0
/
Virtual_board.py
executable file
·127 lines (118 loc) · 3.52 KB
/
Virtual_board.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 13 13:15:21 2020
@author: ted
"""
EMPTY = ['0','0']
BQ = ['b','q']
WQ = ['w','q']
BP = ['b','p']
WP = ['w','p']
BK = ['b','k']
WK = ['w','k']
BR = ['b','r']
WR = ['w','r']
BN = ['b','n']
WN = ['w','n']
BB = ['b','b']
WB = ['w','b']
import numpy as np
from move_calc import moves
class VBoard(list):
def __init__(self, empty = False, board_str = None):
if empty: # board of all empty squares. I may take this out later.
self = super(VBoard, self).__init__([[EMPTY for j in range(8)] for i in range(8)])
elif board_str: # generate virtual board from a string
self = super(VBoard, self).__init__(
[
[
[
char for char in piece
]
for piece in row.split('_')
]
for row in board_str.split('=')
]
)
else: # standard starting board
self = super(VBoard, self).__init__(gen_standard_board_array())
def moves(self, coord, color):
return moves(self, coord, color)
def get_next_boards(self, color):
out = []
for i in range(8):
for j in range(8):
occ = self[i][j]
if occ != EMPTY and occ[0] == color:
mvs = self.moves((i,j), color)
for move in mvs:
board_1 = self.execute_move((i,j), move)
out.append([(i,j),move,board_1])
return out
def execute_move(self, c1, c2):
out = VBoard(empty=True)
# make careful duplicate, only copying the two squares
for i in range(8):
if i == c1[0] or i==c2[0]:
for j in range(8):
if (i == c1[0] and j==c1[1]) or (i==c2[0] and j==c2[1]):
out[i][j] = self[i][j].copy()
else:
out[i][j] = self[i][j]
else:
out[i] = self[i]
# make switch
out[c2[0]][c2[1]] = self[c1[0]][c1[1]]
out[c1[0]][c1[1]] = EMPTY
return out
def __str__(self):
out = '-'
for i in range(8):
out += '------+'
out+='\n'
for i in range(8):
s = ''
for j in range(8):
occ = self[i][j]
if occ == EMPTY:
occ=' '
s+= '| '+occ + ' '
else:
s+= '| '+occ[0] + occ[1] + ' '
out +=s + '|\n-'
for i in range(8):
out += '------+'
out+='\n'
return out
def index_to_piece(index):
if index[0] == 1:
out = BP
elif index[0] == 6:
out = WP
elif index[0] == 0:
out0 = {0:BR, 1:BN, 2:BB,3:BQ,
4:BK, 5:BB, 6:BN, 7:BR}
out = out0[index[1]]
elif index[0] == 7:
out0 = {0:WR, 1:WN, 2:WB,3:WQ,
4:WK, 5:WB, 6:WN, 7:WR}
out = out0[index[1]]
else:
out = EMPTY
return out
def gen_standard_board_array():
board = []
for i in range(8):
row = []
for j in range(8):
sq = index_to_piece((i,j))
row.append(sq)
board.append(row)
return board
if __name__ == '__main__':
b1 = VBoard()
print(b1)
b1 = b1.execute_move((1,0),(2,0))
print(b1)
print(b1.moves((1,1),'b'))