Skip to content

Commit 29b48ec

Browse files
committed
Pieces have functionality; Code cleaning is needed
1 parent 6a90e0b commit 29b48ec

File tree

8 files changed

+384
-46
lines changed

8 files changed

+384
-46
lines changed

Board.py

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,80 @@
11
from kivy.app import App
2-
from kivy.uix.floatlayout import FloatLayout
3-
from kivy.core.window import Window
2+
from kivy.uix.screenmanager import Screen
43

54
from position_of_mouse import find_position
5+
from position_of_pieces import position_dic
6+
from chess_coords_to_real_coords import convert_coordinates
7+
from valid_move import is_valid_move
68

7-
class Scatter_Text_widget(FloatLayout):
9+
class Scatter_Text_widget(Screen):
810

11+
def __init__(self, **kwargs):
12+
super(Scatter_Text_widget, self).__init__(**kwargs)
913

1014
def on_touch_down(self, touch):
1115
res = super(Scatter_Text_widget, self).on_touch_down(touch)
16+
1217
if res:
1318
position = find_position()
19+
#Gets the chess coord of where the mouse pressed
1420
pos_chess = position.chess_position(touch.pos)
15-
print(pos_chess)
16-
return res
21+
22+
position_piece = position_dic
23+
24+
tup = [pos_chess,position_piece[str(pos_chess)]]
25+
#Writes the chess coord and the piece that was pressed in a text file
26+
with open('test.txt', 'w') as f:
27+
f.write(str(tup))
1728

1829
def on_touch_up(self, touch):
19-
position = find_position()
20-
pos_chess = position.chess_position(touch.pos)
21-
print(pos_chess)
30+
res = super(Scatter_Text_widget, self).on_touch_up(touch)
31+
32+
if res:
33+
position = find_position()
34+
#Gets the position of the mouse, and translates it into a chess corrd
35+
pos_chess = position.chess_position(touch.pos)
36+
37+
position_piece = position_dic
38+
conversion = convert_coordinates
39+
40+
with open('test.txt', 'r') as f:
41+
lisr= f.read()
42+
f.close()
43+
#This is where the mouse was pressed, in a chess coord
44+
chess_position_numerical = str(str(lisr[2]) + str(lisr[3]))
45+
46+
#This gets the piece that was moved, and stores it in string format
47+
piece_that_moved = ""
48+
index = 8
49+
while index != len(lisr) -2:
50+
piece_that_moved += str(lisr[index])
51+
index += 1
52+
53+
54+
55+
#Checks to see if the square is currently occupied
56+
st = is_valid_move()
57+
yes = st.main(chess_position_numerical, position_piece, pos_chess, piece_that_moved)
58+
59+
if yes == "True":
60+
self.ids[piece_that_moved].pos = (conversion.to_number()[pos_chess][0], conversion.to_number()[pos_chess][1])
61+
62+
63+
position_dic[str(chess_position_numerical)] = 'None'
64+
position_dic[str(pos_chess)] = str(piece_that_moved)
65+
66+
elif yes == "True, Captured":
67+
piece_occupied = str(position_piece[pos_chess])
68+
self.ids[piece_occupied].pos = (1000,1000)
69+
self.ids[piece_that_moved].pos = (conversion.to_number()[pos_chess][0], conversion.to_number()[pos_chess][1])
70+
71+
72+
position_dic[str(chess_position_numerical)] = 'None'
73+
position_dic[str(pos_chess)] = str(piece_that_moved)
74+
else: #if yes == "FALSE"
75+
self.ids[piece_that_moved].pos = (conversion.to_number()[chess_position_numerical][0], conversion.to_number()[chess_position_numerical][1])
76+
77+
2278

2379
#Builds the App
2480
class window(App):

chess_coords_to_real_coords.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class convert_coordinates():
2+
def to_number():
3+
convert_coordinates = {'a1': (0,0), 'b1': (102.5,0), 'c1': (205,0), 'd1': (307.5,0), 'e1': (410,0), 'f1': (512.5,0), 'g1': (615,0), 'h1': (717.5,0),
4+
'a2': (0,100), 'b2': (102.5,100), 'c2': (205,100), 'd2': (307.5,100), 'e2': (410,100), 'f2': (512.5,100), 'g2': (615,100), 'h2': (717.5,100),
5+
'a3': (0,200), 'b3': (102.5,200), 'c3': (205,200), 'd3': (307.5,200), 'e3': (410,200), 'f3': (512.5,200), 'g3': (615,200), 'h3': (717.5,200),
6+
'a4': (0,300), 'b4': (102.5,300), 'c4': (205,300), 'd4': (307.5,300), 'e4': (410,300), 'f4': (512.5,300), 'g4': (615,300), 'h4': (717.5,300),
7+
'a5': (0,400), 'b5': (102.5,400), 'c5': (205,400), 'd5': (307.5,400), 'e5': (410,400), 'f5': (512.5,400), 'g5': (615,400), 'h5': (717.5,400),
8+
'a6': (0,500), 'b6': (102.5,500), 'c6': (205,500), 'd6': (307.5,500), 'e6': (410,500), 'f6': (512.5,500), 'g6': (615,500), 'h6': (717.5,500),
9+
'a7': (0,600), 'b7': (102.5,600), 'c7': (205,600), 'd7': (307.5,600), 'e7': (410,600), 'f7': (512.5,600), 'g7': (615,600), 'h7': (717.5,600),
10+
'a8': (0,700), 'b8': (102.5,700), 'c8': (205,700), 'd8': (307.5,700), 'e8': (410,700), 'f8': (512.5,700), 'g8': (615,700), 'h8': (717.5,700)
11+
}
12+
13+
return convert_coordinates

difference_for_letter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dictionar_of_letters_to_numbers = {
2+
'a': 1, 'b': 2, 'c': 3, 'd':4, 'e': 5, 'f': 6, 'g':7, 'h': 8
3+
}

main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def setup():
3030
TODO LIST
3131
3232
33-
MAKE THE BUTTONS have a 'hard landing' IN EACH SQUARE AFTER THEY PLAYED
33+
MAKE THE DICTIONARY UPDATE
34+
35+
3436
MAKE THE LABELS DISSAPEAR WHEN CAPTURED
3537
3638
@@ -40,8 +42,8 @@ def setup():
4042
4143
4244
43-
2.5 CODE A TOUCH-BASED SYSTEM
44-
2.75 MAKE THE DRAGGING SYSTEM HAVE THE PIECES MOVE A LAYER UP FROM THE BOARD
45+
46+
2.75 MAKE THE DRAGGING SYSTEM HAVE THE PIECES MOVE A LAYER UP FROM THE BOARD - AND GET IN SYNC WITH MOUSE
4547
3. CODE ALL OF THE MOVES FOR THE PIECES (HARD; MAKE CODE STILL NEAT)
4648
4. CREATE A SYSTEM OF TURN-BASED GAMEPLAY (MEDIUM; NEED TO KEEP CODE CLEAN)
4749
5. CREATE A SYSTEM TO SHOW THE USER WHERE TO PLACE: AND A CHECK-BASED SYSTEM

position_of_pieces.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
position_dic = { 'a1': "Left White Rook", 'b1': "Left White Knight", 'c1': "Left White Bishop", 'd1': "White Queen", 'e1': "White King", 'f1': "Right White Bishop", 'g1': "Right White Knight", 'h1': "Right White Rook",
3+
'a2': "White Pawn1", 'b2': "White Pawn2", 'c2': "White Pawn3", 'd2': "White Pawn4", 'e2': "White Pawn5", 'f2': "White Pawn6", 'g2': "White Pawn7", 'h2': "White Pawn8",
4+
'a3': "None", 'b3': "None", 'c3': "None", 'd3': "None", 'e3': "None", 'f3': "None", 'g3': "None", 'h3': "None",
5+
'a4': "None", 'b4': "None", 'c4': "None", 'd4': "None", 'e4': "None", 'f4': "None", 'g4': "None", 'h4': "None",
6+
'a5': "None", 'b5': "None", 'c5': "None", 'd5': "None", 'e5': "None", 'f5': "None", 'g5': "None", 'h5': "None",
7+
'a6': "None", 'b6': "None", 'c6': "None", 'd6': "None", 'e6': "None", 'f6': "None", 'g6': "None", 'h6': "None",
8+
9+
'a8': "Left Black Rook", 'b8': "Left Black Knight", 'c8': "Left Black Bishop", 'd8': "Black Queen", 'e8': "Black King", 'f8': "Right Black Bishop", 'g8': "Right Black Knight", 'h8': "Right Black Rook",
10+
'a7': "Black Pawn1", 'b7': "Black Pawn2", 'c7': "Black Pawn3", 'd7': "Black Pawn4", 'e7': "Black Pawn5", 'f7': "Black Pawn6", 'g7': "Black Pawn7", 'h7': "Black Pawn8"
11+
}

test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
['b5', 'Left White Knight']

valid_move.py

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
from difference_for_letter import dictionar_of_letters_to_numbers
2+
3+
class is_valid_move():
4+
5+
def is_square_occupied(self, chess_position_numerical, position_piece, pos_chess, piece_that_moved):
6+
if position_piece[pos_chess] != 'None':
7+
piece_occupied = str(position_piece[pos_chess])
8+
if str(piece_occupied)[0] == 'L':
9+
color_of_occupied = str(piece_occupied[5])
10+
elif str(piece_occupied)[0] == 'R':
11+
color_of_occupied = str(piece_occupied[6])
12+
else:
13+
color_of_occupied = str(piece_occupied[0])
14+
15+
if str(piece_that_moved)[0] == 'L':
16+
color_of_moved = str(piece_that_moved[5])
17+
elif str(piece_that_moved)[0] == 'R':
18+
color_of_moved = str(piece_that_moved[6])
19+
else:
20+
color_of_moved = str(piece_that_moved[0])
21+
22+
if color_of_moved == color_of_occupied:
23+
return "False"
24+
25+
else:
26+
piece_only = ""
27+
index = len(str(piece_that_moved)) - 1
28+
while str(piece_that_moved)[index] != " ":
29+
piece_only += str(str(piece_that_moved)[index])
30+
index -= 1
31+
32+
if str(piece_only)[0] == str(1) or str(piece_only)[0] == str(2) or str(piece_only)[0] == str(3) or str(piece_only)[0] == str(4) or str(piece_only)[0] == str(5) or str(piece_only)[0] == str(6) or str(piece_only)[0] == str(7) or str(piece_only)[0] == str(8):
33+
piece_only = piece_only[1:]
34+
piece_only = piece_only[::-1]
35+
s = is_valid_move()
36+
result = "not"
37+
if piece_only == "Rook":
38+
result = s.rook( chess_position_numerical, pos_chess)
39+
elif piece_only == "Bishop":
40+
result = s.Bishop( chess_position_numerical, pos_chess)
41+
elif piece_only == "Queen":
42+
result = s.Queen( chess_position_numerical, pos_chess)
43+
elif piece_only == "King":
44+
result = s.King( chess_position_numerical, pos_chess)
45+
elif piece_only == 'Knight':
46+
result = s.knight(chess_position_numerical, pos_chess)
47+
elif piece_only == "Pawn":
48+
result = s.pawn_capture(chess_position_numerical, pos_chess)
49+
50+
51+
if str(result) == "True":
52+
53+
return "True, Captured"
54+
else:
55+
return "False"
56+
57+
else:
58+
piece_only = ""
59+
index = len(str(piece_that_moved)) - 1
60+
while str(piece_that_moved)[index] != " ":
61+
piece_only += str(str(piece_that_moved)[index])
62+
index -= 1
63+
64+
if str(piece_only)[0] == str(1) or str(piece_only)[0] == str(2) or str(piece_only)[0] == str(3) or str(piece_only)[0] == str(4) or str(piece_only)[0] == str(5) or str(piece_only)[0] == str(6) or str(piece_only)[0] == str(7) or str(piece_only)[0] == str(8):
65+
piece_only = piece_only[1:]
66+
piece_only = piece_only[::-1]
67+
s = is_valid_move()
68+
result = "not"
69+
if piece_only == "Rook":
70+
result = s.rook( chess_position_numerical, pos_chess)
71+
elif piece_only == "Bishop":
72+
result = s.Bishop( chess_position_numerical, pos_chess)
73+
elif piece_only == "Queen":
74+
result = s.Queen( chess_position_numerical, pos_chess)
75+
elif piece_only == "King":
76+
result = s.King( chess_position_numerical, pos_chess)
77+
elif piece_only == 'Knight':
78+
result = s.knight(chess_position_numerical, pos_chess)
79+
elif piece_only == "Pawn":
80+
result = s.pawn(chess_position_numerical, pos_chess)
81+
82+
if str(result) == "True":
83+
84+
return "True"
85+
else:
86+
return "False"
87+
88+
89+
def rook(self, position_piece, pos_chess):
90+
'''
91+
NUMBER STAY SAME
92+
OR
93+
LETTER STAY SAME
94+
'''
95+
96+
if str(position_piece)[0] == str(pos_chess)[0]:
97+
98+
return True
99+
elif str(position_piece)[1] == str(pos_chess)[1]:
100+
101+
return True
102+
else:
103+
104+
return False
105+
106+
def knight(self, position_piece, pos_chess):
107+
'''
108+
want abs(letter_number - letter_number2) + abs(number - number)
109+
THen either number or letter has to varry by exactly ONE
110+
'''
111+
letter_number1 = int(dictionar_of_letters_to_numbers[str(position_piece)[0]])
112+
letter_number2 = int(dictionar_of_letters_to_numbers[str(pos_chess)[0]])
113+
114+
number1 = int(str(position_piece)[1])
115+
number2 = int(str(pos_chess)[1])
116+
117+
difference_between = abs(letter_number1 - letter_number2) + abs(number1 - number2)
118+
119+
if difference_between == 3:
120+
if abs(letter_number1 - letter_number2) == 1:
121+
return True
122+
elif abs(number1 - number2) == 1:
123+
return True
124+
else:
125+
return False
126+
127+
else:
128+
return False
129+
130+
def Bishop(self, position_piece, pos_chess):
131+
'''
132+
NUMBER DID NOT STAY THE SAME
133+
GET DIFFerecne
134+
CALCULATE DIFFerecne for LETTER
135+
CHECK TO SEE IF THAT MAKES SENSE
136+
'''
137+
if str(position_piece)[1] != str(pos_chess)[1]:
138+
139+
difference_of_number = int(str(position_piece)[1]) - int(str(pos_chess)[1])
140+
first_letter = dictionar_of_letters_to_numbers[str(position_piece)[0]]
141+
second_letter = dictionar_of_letters_to_numbers[str(pos_chess)[0]]
142+
if abs(first_letter - second_letter) == abs(difference_of_number):
143+
return True
144+
else:
145+
146+
return False
147+
else:
148+
149+
return False
150+
151+
152+
def Queen(self, position_piece, pos_chess):
153+
'''
154+
CHECK Bishop AND Rook
155+
IF ONE IS TRUE;
156+
THEN QUEEN IS TRUE
157+
'''
158+
s = is_valid_move()
159+
rook_result = s.rook(position_piece, pos_chess)
160+
Bishop_result = s.Bishop(position_piece, pos_chess)
161+
162+
if rook_result == True or Bishop_result == True:
163+
return True
164+
165+
else:
166+
return False
167+
168+
def King(self, position_piece, pos_chess):
169+
'''
170+
+-1 for number then letter stays same
171+
+-1 for letter then number stays same
172+
'''
173+
174+
if int(position_piece[1]) + 1 == int(pos_chess[1]) or int(position_piece[1]) -1 == int(pos_chess[1]):
175+
if str(position_piece)[0] == str(pos_chess)[0]:
176+
return True
177+
else:
178+
return False
179+
180+
elif int(dictionar_of_letters_to_numbers[str(position_piece)[0]]) + 1 == int(dictionar_of_letters_to_numbers[str(pos_chess)[0]]) or int(dictionar_of_letters_to_numbers[str(position_piece)[0]]) -1 == int(dictionar_of_letters_to_numbers[str(pos_chess)[0]]):
181+
if str(position_piece)[1] == str(pos_chess)[1]:
182+
return True
183+
else:
184+
return False
185+
186+
else:
187+
return False
188+
189+
def pawn(self, position_piece, pos_chess):
190+
'''
191+
HAS to MOVE UP BY ONE
192+
UNLESS CAPTURE THEN ADD one to letter and number
193+
'''
194+
if int(str(position_piece)[1]) + 1 == int(str(pos_chess)[1]):
195+
return True
196+
else:
197+
return False
198+
199+
def pawn_capture(self, position_piece, pos_chess):
200+
'''
201+
+ 1 for number
202+
+- one for letter
203+
'''
204+
if int(str(position_piece)[1]) + 1 == int(str(pos_chess)[1]):
205+
if int(dictionar_of_letters_to_numbers[str(position_piece)[0]]) + 1 == int(dictionar_of_letters_to_numbers[str(pos_chess)[0]]) or int(dictionar_of_letters_to_numbers[str(position_piece)[0]]) - 1 == int(dictionar_of_letters_to_numbers[str(pos_chess)[0]]):
206+
return True
207+
else:
208+
return False
209+
else:
210+
return False
211+
212+
def is_path_blocked(self, position_piece, pos_chess):
213+
pass
214+
215+
def special_cases(self, position_piece, pos_chess):
216+
pass
217+
218+
def main(self, chess_position_numerical, position_piece, pos_chess, piece_that_moved):
219+
s = is_valid_move()
220+
status = s.is_square_occupied(chess_position_numerical, position_piece, pos_chess, piece_that_moved)
221+
return status

0 commit comments

Comments
 (0)