Skip to content

Commit b3adf4c

Browse files
committed
Code Health Update
1 parent e317ea6 commit b3adf4c

File tree

11 files changed

+304
-120
lines changed

11 files changed

+304
-120
lines changed

Board.py

Lines changed: 151 additions & 61 deletions
Large diffs are not rendered by default.

Checking_for_valid_move/Queen.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from Checking_for_valid_move.rook import rook
32
from Checking_for_valid_move.bishop import Bishop
43

Checking_for_valid_move/king.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from Data_Conversion.difference_for_letter import dictionar_of_letters_to_numbers
22
from Data_Conversion.position_of_pieces import position_dic
3+
34
def King(position_piece, pos_chess):
45
'''
56
The rules I used to determine the path of the king:

Data_Conversion/chess_coords_to_real_coords.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
'''
2+
The purpose of this is so when the user places a piece, it goes into the middle of the square, not in the
3+
exact location where the user places it.
4+
'''
5+
16
class convert_coordinates():
27
def to_number():
38
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),
@@ -9,5 +14,5 @@ def to_number():
914
'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),
1015
'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)
1116
}
12-
17+
1318
return convert_coordinates
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
'''
2+
This is to convert letters to numbers when calculations occur
3+
'''
4+
15
dictionar_of_letters_to_numbers = {
26
'a': 1, 'b': 2, 'c': 3, 'd':4, 'e': 5, 'f': 6, 'g':7, 'h': 8
37
}
48

5-
9+
'''
10+
This is to save the corrdinates of where the user pressed, in order
11+
to promote the pawn to the correct piece
12+
'''
613
promotion_piece = []

Data_Conversion/position_of_pieces.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
'''
2+
This is the position of all the pieces, in a dictionary. This data is essential for the gameplay to work
3+
'''
14
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",
25
'a2': "White Pawn1", 'b2': "White Pawn2", 'c2': "White Pawn3", 'd2': "White Pawn4", 'e2': "White Pawn5", 'f2': "White Pawn6", 'g2': "White Pawn7", 'h2': "White Pawn8",
36
'a3': "None", 'b3': "None", 'c3': "None", 'd3': "None", 'e3': "None", 'f3': "None", 'g3': "None", 'h3': "None",
@@ -8,4 +11,8 @@
811
'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",
912
'a7': "Black Pawn1", 'b7': "Black Pawn2", 'c7': "Black Pawn3", 'd7': "Black Pawn4", 'e7': "Black Pawn5", 'f7': "Black Pawn6", 'g7': "Black Pawn7", 'h7': "Black Pawn8"
1013
}
14+
15+
'''
16+
Decalres what team is allowed to move their pieces
17+
'''
1118
teams_turn = "W"

Data_Conversion/saved_input.txt

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

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def setup():
1616
Config.set('graphics', 'resizable', '0')
1717
Config.write()
1818

19-
#Make the top windows bar go away
19+
#Make the top Windows bar go away
2020
Window.borderless = True
2121

2222
#Disable the Multi-Touch

pawn_new_piece.kv

Lines changed: 0 additions & 15 deletions
This file was deleted.

valid_move.py

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,92 @@
11
from Data_Conversion.position_of_pieces import teams_turn
22
from Data_Conversion.position_of_pieces import position_dic
3+
4+
#Imports each of the pieces' specific files
35
from Checking_for_valid_move.bishop import Bishop
4-
from Checking_for_valid_move.is_square_occupied import is_square_occupied
56
from Checking_for_valid_move.king import King
67
from Checking_for_valid_move.knight import knight
78
from Checking_for_valid_move.pawn import pawn_movement
89
from Checking_for_valid_move.Queen import Queen
910
from Checking_for_valid_move.rook import rook
1011

1112
from Checking_for_valid_move.turn_based import move_turn
12-
from Checking_for_valid_move.king_check import end_game
13+
from Checking_for_valid_move.is_square_occupied import is_square_occupied
1314

1415
class is_valid_move():
15-
#Checks to make sure the move was valid, and not breaking any rules of Chess
16+
''''
17+
This function is kinda complicated, and I am sure I could have modularized or organized the code in a more beautiful and readable way, but
18+
essentially this code is checking all of the edge (and normal) cases that the pieces could have been moved to. It checks what piece has been moved (pawn, Queen, etc.), and then checks
19+
against multiple different factors if that move was valid.
20+
'''
1621

1722
def valid(self, chess_position_numerical, position_piece, pos_chess, piece_that_moved):
18-
pawn = pawn_movement(chess_position_numerical)
23+
#The teams_turn variable needed to be global even though I imported it through the file location
1924
global teams_turn
20-
king_check = end_game(teams_turn)
21-
2225

26+
pawn = pawn_movement(chess_position_numerical)
27+
result = ""
2328
piece_only = ""
29+
30+
#Gets the name of the piece only, discarding color and left/right
31+
#Example: "Left Black Rook" would become "Rook"
2432
index = len(str(piece_that_moved)) - 1
2533
while str(piece_that_moved)[index] != " ":
2634
piece_only += str(str(piece_that_moved)[index])
2735
index -= 1
2836

29-
result = ""
30-
37+
#Continues breking up the ID into the piece
3138
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):
3239
piece_only = piece_only[1:]
3340
piece_only = piece_only[::-1]
3441

35-
42+
#analysis of piece based on what piece it was
3643
if piece_only == "Rook":
3744
result = rook( chess_position_numerical, pos_chess)
45+
3846
elif piece_only == "Bishop":
3947
result = Bishop( chess_position_numerical, pos_chess, piece_that_moved)
48+
4049
elif piece_only == "Queen":
4150
result = Queen( chess_position_numerical, pos_chess, piece_that_moved)
51+
4252
elif piece_only == "King":
4353
result = King( chess_position_numerical, pos_chess)
54+
4455
elif piece_only == 'Knight':
4556
result = knight(chess_position_numerical, pos_chess)
4657

4758
if is_square_occupied(chess_position_numerical, position_piece, pos_chess, piece_that_moved) == "True, Captured":
4859
'''
4960
This function gets called when the piece that moved is going to capture a piece
5061
'''
51-
5262
if result == "":
63+
#This means the piece MUST be a pawn
64+
#THe pawn is isolated from the other pieces because it has different moving rules based on if it is capturing a piece or not
5365

5466
if str(position_dic[str(chess_position_numerical)])[0] == 'W':
67+
#Since the piece is white, it checks to see if it has 'landed' onto the top row, Where it can be promoted
5568
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":
69+
#If so, chnage the team that has active play
70+
teams_turn = "B"
71+
72+
#Return that the move was valid
5673
return "New_Piece"
5774
else:
75+
#Since the piece is black it checks the bottom
5876
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":
5977
if move_turn(piece_that_moved, teams_turn) == "True":
60-
#if king_check.in_check() == "True":
6178

62-
if teams_turn == "W":
63-
teams_turn = "B"
64-
else:
65-
teams_turn ="W"
79+
#Changes the active play to White
80+
teams_turn ="W"
81+
82+
#Returns that the move was valid
6683
return "New_Piece"
6784

6885
result = pawn.pawn_capture(chess_position_numerical, pos_chess)
6986

7087
if str(result) == "True":
7188
if move_turn(piece_that_moved, teams_turn) == "True":
72-
#if king_check.in_check() == "True":
89+
#If the move was valid, change the active play and return that the move was valid
7390
if teams_turn == "W":
7491
teams_turn = "B"
7592
else:
@@ -79,55 +96,59 @@ def valid(self, chess_position_numerical, position_piece, pos_chess, piece_that_
7996

8097
elif is_square_occupied(chess_position_numerical, position_piece, pos_chess, piece_that_moved) == "True":
8198
if result == "":
82-
99+
#Same as above, the pawn has to go here
83100
if str(position_dic[str(chess_position_numerical)])[0] == 'W':
84-
85-
print(str(pos_chess))
101+
#Checks to see if the top row is the end location, since the piece was white
86102
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":
87103
if move_turn(piece_that_moved, teams_turn) == "True":
88-
#if king_check.in_check() == "True":
89104

90-
if teams_turn == "W":
91-
teams_turn = "B"
92-
else:
93-
teams_turn ="W"
94-
return "New_Piece"
95-
else:
105+
#Changes the play
106+
teams_turn = "B"
96107

108+
#Returns that the move was Valid
109+
return "New_Piece"
110+
else:
111+
#Since the piece is black, the algorithm checks the bottom row
97112
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":
98113
if move_turn(piece_that_moved, teams_turn) == "True":
99-
#if king_check.in_check() == "True":
100114

101-
if teams_turn == "W":
102-
teams_turn = "B"
103-
else:
104-
teams_turn ="W"
115+
#Changes the team that can move
116+
teams_turn ="W"
117+
118+
#Returns that the move was valid
105119
return "New_piece"
106120

107121
result = pawn.pawn(chess_position_numerical, pos_chess)
108122

109123

110124
if str(result) == "True":
111125
if move_turn(piece_that_moved, teams_turn) == "True":
112-
#if king_check.in_check() == "True":
113-
126+
#If the move was valid, change the play and return that it was valid
114127
if teams_turn == "W":
115128
teams_turn = "B"
116129
else:
117130
teams_turn ="W"
131+
118132
return "True"
119133

120134
elif str(result) == "Castle":
135+
'''
136+
An Edge-Case for Castling, Done mostly in it's own file '''
121137

122138
if move_turn(piece_that_moved, teams_turn) == "True":
139+
#Changes the play and returns that the move was valid
123140
if teams_turn == "W":
124141
teams_turn = "B"
125142
else:
126143
teams_turn ="W"
127144

128145
return "Castle"
146+
129147
def main(self, chess_position_numerical, position_piece, pos_chess, piece_that_moved):
130148
checking = is_valid_move()
149+
131150
#Calls the above function and returns the result to Board.py
151+
#Board.py takes the data and runs some more analysis, and if the move passes that, then the piece will move
152+
#on the GUI for the user, and the dictionary will update locations of all the pieces
132153
status = checking.valid(chess_position_numerical, position_piece, pos_chess, piece_that_moved)
133154
return status

0 commit comments

Comments
 (0)