-
Notifications
You must be signed in to change notification settings - Fork 1
/
boardControl.py
208 lines (174 loc) · 6.6 KB
/
boardControl.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
'''
Allows for replaying of chess games given the input of every move.
'''
from array import *
from chess_move import ChessMove
from time import sleep
'''
assign variables and create objects
'''
move = ChessMove()
current_position = [0,0]
directionX = ""
directionY = ""
#turn = 1 # evens are black moves, odds are white move
'''
Move to one space at a time.
'''
#define pieces
pieces = {
"bpawn":1,"brook":2,'bknight':3,'bbishop':4,'bqueen':5,'bking':6,
'wpawn':7,'wrook':8,'wknight':9,'wbishop':10,'wqueen':11,'wking':12
}
#create reference for board letters and numbers so actual numbers on the board(1-8) work with array(0-7)
reference = {
'a' : 0 , 'b' : 1 , 'c' : 2 , 'd': 3 , 'e': 4 , 'f': 5 , 'g': 6 , 'h': 7,
'1': 0 , '2': 1 ,'3': 2 ,'4': 3 ,'5': 4 ,'6' : 5 ,'7': 6 ,'8': 7
}
'''
create board array
letters go left to right starting from a
numbers go down staring from 1
'''
board = [[8,9,10,11,12,10,9,8], \
[7,7,7,7,7,7,7,7],\
[0,0,0,0,0,0,0,0],\
[0,0,0,0,0,0,0,0],\
[0,0,0,0,0,0,0,0],\
[0,0,0,0,0,0,0,0],\
[1,1,1,1,1,1,1,1],\
[2,3,4,5,6,4,3,2]]
def take_piece(current, move_position):
'''
figure out of moving to a space will take a piece, telling the chess_move program to initiate the algorithm that moves pieces off of the board
'''
print("take piece", current, board[move_position[1]][move_position[0]])
if current > 6 and board[move_position[1]][move_position[0]] < 7 and current != 0 and board[move_position[1]][move_position[0]] != 0:
return True
elif current < 7 and board[move_position[1]][move_position[0]] > 6 and current != 0 and board[move_position[1]][move_position[0]] != 0:
return True
else:
return False
def print_board():
'''
prints the board out
'''
for r in board:
for c in r:
print(c,end = " ")
print()
def find_knight(temp):
'''
figure out if a piece is a knight or not, telling the chess_move program to initiate the algorithm that moves pieces on the lines
'''
if temp == 3 or temp == 9:
return True
else:
return False
def calculate_moves(x, y):
'''
calculate the number of steps needed to move to a certain square
'''
global directionX
global directionY
global current_position
if x > 0:
directionX = "positive"
current_position[0] += x
else:
directionX = "negative"
current_position[0] += x # negative number
if y > 0:
directionY = "positive"
current_position[1] += y
else:
directionY = "negative"
current_position[1] += y # negative number
def piece_color(piece):
'''
determine color of a piece, white will return True
'''
if piece > 6:
return True
else:
return False
def find_turn(numTurn):
'''
determine whose turn it is, white will return True
'''
if numTurn%2 == 1:
return True
else:
return False
print("e2e4: move pawn to e4 \nkc:kingside castle \nqc:queenside castle \nhome:return to a1")
def move_pieces(moveTo, turn):
#moveTo = input("INPUT: ") #intake ending position and split into letter and number
#moveTo = moveset[turn - 1] #auto run game
print(moveTo, turn)
# print(moveTo)
TEMP = 0
KNIGHT = False
if moveTo == "qc" and find_turn(turn):
deltaX = 4 - current_position[0]
deltaY = 0 - current_position[1]
calculate_moves(deltaX, deltaY)
board[0][4] = 0
board[0][2] = 12
board[0][0] = 0
board[0][3] = 8
move.move_steppers_uneven(abs(deltaX), abs(deltaY), directionX, directionY, "off", False)
move.queenside_castle(find_turn(turn))
elif moveTo == "kc" and find_turn(turn):
deltaX = 4 - current_position[0]
deltaY = 0 - current_position[1]
calculate_moves(deltaX, deltaY)
board[0][4] = 0
board[0][6] = 12
board[0][7] = 0
board[0][5] = 8
move.move_steppers_uneven(abs(deltaX), abs(deltaY), directionX, directionY, "off", False)
move.kingside_castle(find_turn(turn))
elif moveTo == "qc" and not find_turn(turn):
deltaX = 4 - current_position[0]
deltaY = 7 - current_position[1]
calculate_moves(deltaX, deltaY)
board[7][4] = 0
board[7][2] = 6
board[7][0] = 0
board[7][3] = 2
move.move_steppers_uneven(abs(deltaX), abs(deltaY), directionX, directionY, "off", False)
move.queenside_castle(find_turn(turn))
#move.move_steps_uneven(0,60,"positive", "positive") # temporary workaround to topping out
elif moveTo == "kc" and not find_turn(turn):
deltaX = 4 - current_position[0]
deltaY = 7 - current_position[1]
calculate_moves(deltaX, deltaY)
board[7][4] = 0
board[7][6] = 6
board[7][7] = 0
board[7][5] = 2
move.move_steppers_uneven(abs(deltaX), abs(deltaY), directionX, directionY, "off", False)
move.kingside_castle(find_turn(turn))
#move.move_steps_uneven(0,60,"positive", "positive") # temporary workaround to topping out
elif moveTo == "home":
move.return_origin()
else:
deltaX = moveTo[0] - current_position[0]
deltaY = moveTo[1] - current_position[1]
#print(deltaX, deltaY)
calculate_moves(deltaX, deltaY)
TEMP = board[current_position[1]][current_position[0]]
board[current_position[1]][current_position[0]] = 0
KNIGHT = find_knight(TEMP)
#print(current_position, move_position, abs(deltaX), abs(deltaY), directionX, directionY, "off", board[current_position[1]][current_position[0]])
move.move_steppers_uneven(abs(deltaX), abs(deltaY), directionX, directionY, "off", False)
deltaX2 = moveTo[2] - current_position[0]
deltaY2 = moveTo[3] - current_position[1]
#print(deltaX2, deltaY2)
calculate_moves(deltaX2, deltaY2)
if take_piece(TEMP,current_position):
move.take_piece(abs(deltaX2), abs(deltaY2), directionX, directionY, current_position)
#print(current_position, move_position, abs(deltaX2), abs(deltaY2), directionX, directionY, "on", KNIGHT, board[current_position[1]][current_position[0]])
move.move_steppers_uneven(abs(deltaX2), abs(deltaY2), directionX, directionY, "on", KNIGHT)
board[current_position[1]][current_position[0]] = TEMP # moving pieces in virtual chessboard # keeping track of whose turn it is
#print_board()