Skip to content

Commit

Permalink
adjust controller
Browse files Browse the repository at this point in the history
  • Loading branch information
xXUnique31Xx committed Jul 1, 2024
1 parent b103daa commit 24bf6e7
Showing 1 changed file with 46 additions and 43 deletions.
89 changes: 46 additions & 43 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,73 +56,76 @@ def get_menu_choice(self):
sys.exit()

else:
print('Your choice is not valid! Please try again!')
GameView.display_message('Your choice is not valid! Please try again!')
self.get_menu_choice()

def start_game(self):
if not self.load_game:
self.model.set_initial_pieces()
self.view.last_board = self.model.get_copy_board_state()
self.board.set_initial_pieces()
self.view.last_board = self.board.get_copy_board_state()
else:
for _ in range(64):
if self.model.board_state[_] is not None:
self.model.pieces.append(self.model.board_state[_])
if self.board.board_state[_] is not None:
self.board.pieces.append(self.board.board_state[_])

self.model.view.update_board()
self.view.update_board()
self.get_input()

if self.model.ai:
while self.model.check_for_king():
if self.model.currently_playing == 'Black':
if self.board.ai:
while self.board.check_for_king():
if self.board.currently_playing == 'Black':
self.user_ai.move()
else:
self.get_input()
else:
while self.model.check_for_king():
while self.board.check_for_king():
self.get_input()

print(self.model.currently_playing + ' lost because his king died!')
GameView.display_message(self.board.currently_playing + ' lost because his king died!')
self.get_after_game_choice()

@staticmethod
def get_symbol_preference():

while True:
choice = input('Do you want to use symbols? If not, letters will be used instead. (Y/N): ')
choice = GameView.input_prompt('Do you want to use symbols? If not, letters will be used instead. (Y/N): ')
if choice.lower() == 'y' or choice.lower() == 'yes':
return True
elif choice.lower() == 'n' or choice.lower() == 'no':
return False
else:
print('Invalid input! Please answer the question with "yes" or "no"')
GameView.display_message('Invalid input! Please answer the question with "yes" or "no"')

def get_input(self):

choice = input('Please enter your desired Move e.g. "e2e4" (type "s" to save): ').upper()
choice = GameView.input_prompt('Please enter your desired Move e.g. "e2e4" (type "s" to save): ').upper()

if choice == "Q":
self.model.view.clear_console()
self.board.view.clear_console()
sys.exit()

if choice == "S":
pass # Needs some savintg logic stuff
self.save_gamestate()
self.view.clear_console()
self.view.print_menu()
self.get_menu_choice()

if choice == "M":
self.view.clear_console()
self.view.print_menu()

if len(choice) < 4:
print('Invalid Choice')
GameView.display_message('Invalid Choice')
self.get_input()
else:
lines = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
columns = ['1', '2', '3', '4', '5', '6', '7', '8']
start_pos = choice[:2]
goal_pos = choice[-2:]
if start_pos[0] in lines and goal_pos[0] in lines and start_pos[1] in columns and goal_pos[1] in columns:
self.model.move_piece(self.model.correlation[start_pos], self.model.correlation[goal_pos])
self.board.move_piece(self.board.correlation[start_pos], self.board.correlation[goal_pos])
else:
print('Invalid Choice')
GameView.display_message('Invalid Choice')
self.get_input()

@staticmethod
Expand Down Expand Up @@ -163,18 +166,18 @@ def save_gamestate(self):
self.create_directory_if_not_exists()

gamestate = {
'currently_playing': self.model.currently_playing,
'show_symbols': self.model.show_symbols,
'board_state': {str(i): self.piece_to_dict(self.model.board_state[i]) for i in range(64)},
'ai': self.model.ai
'currently_playing': self.board.currently_playing,
'show_symbols': self.board.show_symbols,
'board_state': {str(i): self.piece_to_dict(self.board.board_state[i]) for i in range(64)},
'ai': self.board.ai
}

try:
with open(filename, 'w', encoding='utf-8') as file:
json.dump(gamestate, file, indent=4)
self.view.display_message(f"Game saved as {filename}")
GameView.display_message(f"Game saved as {filename}")
except IOError as e:
self.view.display_message(f"Failed to save game: {e}")
GameView.display_message(f"Failed to save game: {e}")

def load_gamestate(self):
"""Load a game state from a file."""
Expand All @@ -194,43 +197,43 @@ def load_gamestate(self):
with game_save_path.open("r") as file:
GameSave = json.load(file)
# Set the current player and symbol preference
self.model.currently_playing = GameSave['currently_playing']
self.model.show_symbols = GameSave['show_symbols']
self.board.currently_playing = GameSave['currently_playing']
self.board.show_symbols = GameSave['show_symbols']
self.load_game = True
self.user_ai = GameAI(self.model, self.view, "Black", "White")

if 'Ai' in GameSave:
self.ai = True
self.model.ai = True
self.board.ai = True

for i in range(64):
# If the piece is None, the position is empty
if GameSave['board_state'][str(i)]['piece'] == 'None': # Moved wird nicht übernommen
self.model.board_state[i] = None
self.board.board_state[i] = None
else:
# If the piece is not None, create a new piece object
if GameSave['board_state'][str(i)]['piece'] == 'Rooks':
self.model.board_state[i] = Rook(GameSave['board_state'][str(i)]['colour'],
i, self.model)
self.board.board_state[i] = Rook(GameSave['board_state'][str(i)]['colour'],
i, self.board)
if GameSave['board_state'][str(i)]['piece'] == 'Knights':
self.model.board_state[i] = Knight(GameSave['board_state'][str(i)]['colour'],
i, self.model)
self.board.board_state[i] = Knight(GameSave['board_state'][str(i)]['colour'],
i, self.board)
if GameSave['board_state'][str(i)]['piece'] == 'Bishops':
self.model.board_state[i] = Bishop(GameSave['board_state'][str(i)]['colour'],
i, self.model)
self.board.board_state[i] = Bishop(GameSave['board_state'][str(i)]['colour'],
i, self.board)
if GameSave['board_state'][str(i)]['piece'] == 'Queens':
self.model.board_state[i] = Queen(GameSave['board_state'][str(i)]['colour'],
i, self.model)
self.board.board_state[i] = Queen(GameSave['board_state'][str(i)]['colour'],
i, self.board)
if GameSave['board_state'][str(i)]['piece'] == 'Kings':
self.model.board_state[i] = King(GameSave['board_state'][str(i)]['colour'],
i, self.model)
self.board.board_state[i] = King(GameSave['board_state'][str(i)]['colour'],
i, self.board)
if GameSave['board_state'][str(i)]['piece'] == 'Pawns':
self.model.board_state[i] = Pawn(GameSave['board_state'][str(i)]['colour'],
i, self.model)
self.board.board_state[i] = Pawn(GameSave['board_state'][str(i)]['colour'],
i, self.board)

else:
self.view.display_message("There's no Save File for your Game!")
GameView.display_message("There's no Save File for your Game!")
return False

self.view.last_board = self.model.get_copy_board_state()
self.view.last_board = self.board.get_copy_board_state()
return True

0 comments on commit 24bf6e7

Please sign in to comment.