|
| 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