11from Data_Conversion .position_of_pieces import teams_turn
22from Data_Conversion .position_of_pieces import position_dic
3+
4+ #Imports each of the pieces' specific files
35from Checking_for_valid_move .bishop import Bishop
4- from Checking_for_valid_move .is_square_occupied import is_square_occupied
56from Checking_for_valid_move .king import King
67from Checking_for_valid_move .knight import knight
78from Checking_for_valid_move .pawn import pawn_movement
89from Checking_for_valid_move .Queen import Queen
910from Checking_for_valid_move .rook import rook
1011
1112from 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
1415class 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