Skip to content

Commit e317ea6

Browse files
committed
Added Pawn Promotion
1 parent 6483c71 commit e317ea6

File tree

5 files changed

+152
-40
lines changed

5 files changed

+152
-40
lines changed

Board.py

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from kivy.app import App
22
from kivy.uix.screenmanager import Screen
3-
3+
from Data_Conversion.position_of_pieces import teams_turn
44
from Data_Conversion.position_of_mouse import find_position
55
from Data_Conversion.position_of_pieces import position_dic
66
from Data_Conversion.chess_coords_to_real_coords import convert_coordinates
7-
7+
from kivy.uix.popup import Popup
8+
from kivy.uix.floatlayout import FloatLayout
9+
from kivy.uix.button import Button
810
from valid_move import is_valid_move
11+
from Data_Conversion.difference_for_letter import promotion_piece
912

1013
class Scatter_Text_widget(Screen):
1114

@@ -45,36 +48,36 @@ def on_touch_up(self, touch):
4548

4649
#Gets the position of the mouse, and translates it into a chess corrd
4750
pos_chess = self.position.chess_position(touch.pos)
48-
51+
self.pos_chess = pos_chess
4952
#opens the text file, to get the previous data
5053
with open('Data_Conversion\saved_input.txt', 'r') as f:
5154
list_of_previous_data= f.read()
5255
f.close()
5356

5457
#OLD DATA; for the chess corrdinate
5558
chess_position_numerical = str(str(list_of_previous_data[2]) + str(list_of_previous_data[3]))
56-
59+
self.chess_position_numerical = chess_position_numerical
5760
#This is the piece that was moved, taken from the text file
58-
piece_that_moved = ""
61+
self.piece_that_moved = ""
5962
index = 8
6063
while index != len(list_of_previous_data) -2:
61-
piece_that_moved += str(list_of_previous_data[index])
64+
self.piece_that_moved += str(list_of_previous_data[index])
6265
index += 1
6366

6467
'''
6568
Checks with another python file to see if the move was valid or not, against all rules of Chess
6669
'''
6770
st = is_valid_move()
68-
valid_or_not = st.main(chess_position_numerical, self.position_piece, pos_chess, piece_that_moved)
71+
valid_or_not = st.main(chess_position_numerical, self.position_piece, pos_chess, self.piece_that_moved)
6972

7073
#IF THE MOVE WAS VALID
7174
if valid_or_not == "True":
7275
#Move the piece to the location
73-
self.ids[piece_that_moved].pos = (conversion.to_number()[pos_chess][0], conversion.to_number()[pos_chess][1])
76+
self.ids[self.piece_that_moved].pos = (conversion.to_number()[pos_chess][0], conversion.to_number()[pos_chess][1])
7477

7578
#Updates the Dictionary of all the piece locations
7679
position_dic[str(chess_position_numerical)] = 'None'
77-
position_dic[str(pos_chess)] = str(piece_that_moved)
80+
position_dic[str(pos_chess)] = str(self.piece_that_moved)
7881

7982
elif valid_or_not == "True, Captured":
8083
#If the move was valid, and a capture occured
@@ -83,15 +86,15 @@ def on_touch_up(self, touch):
8386
#Deletes the piece that was captured
8487
self.ids[piece_occupied].pos = (1000,1000)
8588
#Moves the piece in play
86-
self.ids[piece_that_moved].pos = (conversion.to_number()[pos_chess][0], conversion.to_number()[pos_chess][1])
89+
self.ids[self.piece_that_moved].pos = (conversion.to_number()[pos_chess][0], conversion.to_number()[pos_chess][1])
8790

8891
#Updates Dictionary
8992
position_dic[str(chess_position_numerical)] = 'None'
90-
position_dic[str(pos_chess)] = str(piece_that_moved)
93+
position_dic[str(pos_chess)] = str(self.piece_that_moved)
9194

9295
elif valid_or_not == "Castle":
9396
#Move the piece to the location
94-
self.ids[piece_that_moved].pos = (conversion.to_number()[pos_chess][0], conversion.to_number()[pos_chess][1])
97+
self.ids[self.piece_that_moved].pos = (conversion.to_number()[pos_chess][0], conversion.to_number()[pos_chess][1])
9598
if str(pos_chess) == 'g1':
9699
self.ids['Right White Rook'].pos = (conversion.to_number()['f1'][0], conversion.to_number()['f1'][1])
97100
position_dic['h1'] = 'None'
@@ -110,16 +113,74 @@ def on_touch_up(self, touch):
110113
position_dic['d8'] = 'Left Black Rook'
111114
#Updates the Dictionary of all the piece locations
112115
position_dic[str(chess_position_numerical)] = 'None'
113-
position_dic[str(pos_chess)] = str(piece_that_moved)
116+
position_dic[str(pos_chess)] = str(self.piece_that_moved)
114117

115118
elif valid_or_not == "New_Piece":
116-
pass
119+
content = Button(id = 'Queen Promotion', background_normal='Pictures\White_Queen.png', size = (60,60), pos = (410,300))
120+
content1 = Button(id = 'Rook Promotion', background_normal='Pictures\white_rook.png', size = (60,60), pos = (307.5,400))
121+
content2 = Button(id = 'Bishop Promotion', background_normal='Pictures\white_Bishop.png', size = (60,60), pos = (307.5,300))
122+
content3 = Button(id = 'Knight Promotion', background_normal='Pictures\white_horse.png', size = (60,60), pos = (410,400))
123+
float = FloatLayout()
124+
float.add_widget(content)
125+
float.add_widget(content1)
126+
float.add_widget(content2)
127+
float.add_widget(content3)
128+
damn = []
129+
popup = Popup(content=float, size_hint=(None, None), size=(120, 160), auto_dismiss=False, pos_hint={'x': 10.0,
130+
'y':10.0})
131+
sc = Scatter_Text_widget()
132+
content.bind(on_press = self.yup, on_release=popup.dismiss)
133+
content1.bind(on_press = self.yup,on_release=popup.dismiss)
134+
content2.bind(on_press = self.yup,on_release=popup.dismiss)
135+
content3.bind(on_press = self.yup,on_release=popup.dismiss)
136+
popup.open()
137+
138+
del promotion_piece[:]
139+
117140
else:
118141
#If the move was not valid
119142

120143
#Places the piece in the beggining location
121-
self.ids[piece_that_moved].pos = (conversion.to_number()[chess_position_numerical][0], conversion.to_number()[chess_position_numerical][1])
122-
144+
self.ids[self.piece_that_moved].pos = (conversion.to_number()[chess_position_numerical][0], conversion.to_number()[chess_position_numerical][1])
145+
146+
147+
148+
149+
def yup(self, obj):
150+
global teams_turn
151+
conversion = convert_coordinates
152+
153+
154+
promotion_piece.append(obj.pos[0])
155+
promotion_piece.append(obj.pos[1])
156+
157+
self.ids[self.piece_that_moved].pos = (10000,1000)
158+
#If the move was valid, and a capture occured
159+
piece_occupied = str(self.position_piece[self.pos_chess])
160+
print(promotion_piece)
161+
if piece_occupied != "None":
162+
self.ids[piece_occupied].pos = (1000,1000)
163+
if promotion_piece == [410,300]:
164+
self.ids['Whire Queen'].pos = (conversion.to_number()[self.pos_chess][0], conversion.to_number()[self.pos_chess][1])
165+
position_dic[str(self.chess_position_numerical)] = 'None'
166+
position_dic[str(self.pos_chess)] = 'Whire Queen'
167+
elif promotion_piece == [307.5, 400]:
168+
self.ids['Ledt White Rook'].pos = (conversion.to_number()[self.pos_chess][0], conversion.to_number()[self.pos_chess][1])
169+
position_dic[str(self.chess_position_numerical)] = 'None'
170+
position_dic[str(self.pos_chess)] = 'Ledt White Rook'
171+
elif promotion_piece == [307.5, 300]:
172+
self.ids['Ledt White Bishop'].pos = (conversion.to_number()[self.pos_chess][0], conversion.to_number()[self.pos_chess][1])
173+
position_dic[str(self.chess_position_numerical)] = 'None'
174+
position_dic[str(self.pos_chess)] = 'Ledt White Bishop'
175+
elif promotion_piece == [410, 400]:
176+
self.ids['Ledt White Knight'].pos = (conversion.to_number()[self.pos_chess][0], conversion.to_number()[self.pos_chess][1])
177+
position_dic[str(self.chess_position_numerical)] = 'None'
178+
position_dic[str(self.pos_chess)] = 'Ledt White Knight'
179+
180+
if teams_turn == "W":
181+
teams_turn = "B"
182+
else:
183+
teams_turn ="W"
123184
#Builds the App
124185
class window(App):
125186
def build(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
dictionar_of_letters_to_numbers = {
22
'a': 1, 'b': 2, 'c': 3, 'd':4, 'e': 5, 'f': 6, 'g':7, 'h': 8
33
}
4+
5+
6+
promotion_piece = []

Data_Conversion/saved_input.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
['a7', 'Black Pawn1']
1+
['d7', 'Whire Queen']

valid_move.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ def valid(self, chess_position_numerical, position_piece, pos_chess, piece_that_
5656
return "New_Piece"
5757
else:
5858
if str(pos_chess) == "a1" or str(pos_chess) == "b1" or str(pos_chess) == "c1" or str(pos_chess) == "d1" or str(pos_chess) == "e1" or str(pos_chess) == "f1" or str(pos_chess) == "g1" or str(pos_chess) == "h1":
59-
return "New_Piece"
59+
if move_turn(piece_that_moved, teams_turn) == "True":
60+
#if king_check.in_check() == "True":
61+
62+
if teams_turn == "W":
63+
teams_turn = "B"
64+
else:
65+
teams_turn ="W"
66+
return "New_Piece"
6067

6168
result = pawn.pawn_capture(chess_position_numerical, pos_chess)
6269

@@ -77,11 +84,26 @@ def valid(self, chess_position_numerical, position_piece, pos_chess, piece_that_
7784

7885
print(str(pos_chess))
7986
if str(pos_chess) == "a8" or str(pos_chess) == "b8" or str(pos_chess) == "c8" or str(pos_chess) == "d8" or str(pos_chess) == "e8" or str(pos_chess) == "f8" or str(pos_chess) == "g8" or str(pos_chess) == "h8":
80-
return "New_Piece"
87+
if move_turn(piece_that_moved, teams_turn) == "True":
88+
#if king_check.in_check() == "True":
89+
90+
if teams_turn == "W":
91+
teams_turn = "B"
92+
else:
93+
teams_turn ="W"
94+
return "New_Piece"
8195
else:
8296

8397
if str(pos_chess) == "a1" or str(pos_chess) == "b1" or str(pos_chess) == "c1" or str(pos_chess) == "d1" or str(pos_chess) == "e1" or str(pos_chess) == "f1" or str(pos_chess) == "g1" or str(pos_chess) == "h1":
84-
return "New_piece"
98+
if move_turn(piece_that_moved, teams_turn) == "True":
99+
#if king_check.in_check() == "True":
100+
101+
if teams_turn == "W":
102+
teams_turn = "B"
103+
else:
104+
teams_turn ="W"
105+
return "New_piece"
106+
85107
result = pawn.pawn(chess_position_numerical, pos_chess)
86108

87109

window.kv

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,66 @@
1414
source: 'Pictures\chessboard.png'
1515
FloatLayout:
1616

17-
Button:
18-
id: Knight Change
17+
Scatter:
18+
id: Whire Queen
1919
size_hint: None, None
20-
pos: 0,400
2120
size: 100,100
21+
pos: 1000,10000
22+
23+
Label:
24+
25+
canvas.before:
26+
Rectangle:
27+
pos: self.pos
28+
size: self.size
29+
source: 'Pictures\white_Queen.png'
30+
size: 100,100
2231

23-
background_normal: 'Pictures\white_horse.png'
24-
background_down: 'Pictures\white_horse.png'
25-
Button:
26-
id: Rook Change
32+
Scatter:
33+
id: Ledt White Rook
2734
size_hint: None, None
28-
pos: 0,300
2935
size: 100,100
36+
pos: 1000,1000
37+
38+
Label:
39+
40+
canvas.before:
41+
Rectangle:
3042

31-
background_down: 'Pictures\white_rook.png'
32-
background_normal: 'Pictures\white_rook.png'
33-
Button:
34-
id: Bishop Change
43+
pos: self.pos
44+
size: self.size
45+
source: 'Pictures\white_rook.png'
46+
size: 100,100
47+
48+
Scatter:
49+
id: Ledt White Knight
3550
size_hint: None, None
36-
pos: 0,600
3751
size: 100,100
52+
pos: 1000,10000
3853

54+
Label:
3955

40-
background_normal: 'Pictures\white_Bishop.png'
41-
background_down: 'Pictures\white_Bishop.png'
42-
Button:
43-
id: Queen Change
56+
canvas.before:
57+
Rectangle:
58+
pos: self.pos
59+
size: self.size
60+
source: 'Pictures\white_horse.png'
61+
size: 100,100
62+
63+
Scatter:
64+
id: Ledt White Bishop
4465
size_hint: None, None
45-
pos: 0, 500
4666
size: 100,100
67+
pos: 1000,10000
4768

48-
background_down: 'Pictures\white_Queen.png'
49-
background_normal: 'Pictures\white_Queen.png'
69+
Label:
5070

71+
canvas.before:
72+
Rectangle:
73+
pos: self.pos
74+
size: self.size
75+
source: 'Pictures\white_Bishop.png'
76+
size: 100,100
5177

5278
Scatter:
5379
id: Left White Rook

0 commit comments

Comments
 (0)