Skip to content

Commit

Permalink
Fixing GH-11 by adding a humans defeated check which ends the game if…
Browse files Browse the repository at this point in the history
… there are no human players left.
  • Loading branch information
ben-ryder committed Sep 12, 2021
1 parent 7b0e6b2 commit 0b79daa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
20 changes: 19 additions & 1 deletion project/game/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ def __init__(self, control, display, model):
def run(self):
while self.state == "game":

if self.model_link.game_ended():
# Check for human defeat before game over as human defeat also means game over.
if self.model_link.humans_are_defeated():
if HumansDefeatedMessage not in [type(obj) for obj in self.persistent_guis]:
self.humans_defeated_message()
elif self.model_link.game_ended():
# game ended at deletion of game over message (called in GameOverMessage on its "ok")
if GameOverMessage not in [type(obj) for obj in self.persistent_guis]: # stops recalling when active
self.game_over_message()
Expand Down Expand Up @@ -193,6 +197,16 @@ def game_over_message(self):

self.persistent_guis.insert(0, GameOverMessage(self, title, message))

def humans_defeated_message(self):
self.persistent_guis.clear()

title = "All humans have been destroyed!"
message = ["The computer players have defeated ",
"all humans so the game is now over.",
"Better luck next time!"]

self.persistent_guis.insert(0, HumansDefeatedMessage(self, title, message))

def quit_message(self):
self.save()
self.persistent_guis.append(QuitMessage(self, "Are you sure you want to quit?",
Expand Down Expand Up @@ -742,6 +756,10 @@ def handle_click(self):
self.GUI.end_game()


class HumansDefeatedMessage(GameOverMessage):
pass


class NextTurnMessage(Message):
def handle_click(self):
if self.ok_button.check_clicked():
Expand Down
7 changes: 5 additions & 2 deletions project/game/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def next_turn(self):
self.get_current_player().end_turn()

# Getting new human player turn
if not self.is_winner(): # if more than 1 player alive
if not self.is_winner() and not self.humans_are_defeated(): # if more than 1 player alive
self.cycle_player()
else:
self.game_end = True
Expand Down Expand Up @@ -131,7 +131,7 @@ def handle_death(self):
if self.check_death(player):
player.kill()

if self.is_winner(): # here, as otherwise must wait for next_turn call
if self.is_winner() or self.humans_are_defeated(): # here, as otherwise must wait for next_turn call
self.game_end = True

return player.get_name() # assuming only one player died, return first found.
Expand All @@ -143,6 +143,9 @@ def check_death(self, player):
def game_ended(self):
return self.game_end

def humans_are_defeated(self):
return len([player for player in self.players if player.get_control() == "human" and not player.is_dead()]) == 0

def is_winner(self):
return len([player for player in self.players if not player.is_dead()]) == 1

Expand Down

0 comments on commit 0b79daa

Please sign in to comment.