-
Notifications
You must be signed in to change notification settings - Fork 2
[WIP] Players can choose a name and they can replay a party #15
base: master
Are you sure you want to change the base?
Changes from 4 commits
e3f3a22
cc6cd9c
bb3ecff
bd31689
1cd6496
c7ec3e3
158b658
f1df813
dc53721
3686bc0
8354051
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ class UIRender: | |
|
||
"""Definition of the user interface and the interactions:""" | ||
|
||
def prompt_piece_selection(self, game_state): | ||
def prompt_piece_selection(self, game_state, players): | ||
while True: | ||
try: | ||
piece = int(input("Choose the next piece of the opponent : ")) | ||
|
@@ -22,24 +22,41 @@ def prompt_piece_selection(self, game_state): | |
game_state.message = "You must choose a number available in the list" | ||
except ValueError: | ||
game_state.message = "You have to type number between 1 and " + str(PIECES_NUMBER) | ||
self.display_game(game_state) | ||
self.display_game(game_state, players) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's strange to not integrate the players into the game_state, why have you choice this architecture ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. State represent the state of the current round game. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can still integrate player into the game state even if they don't change. This way, you manipulate one state only and it's easier to reason about and test |
||
except KeyboardInterrupt: | ||
print("\nGame aborted") | ||
exit() | ||
|
||
def prompt_piece_location(self, game_state): | ||
def prompt_piece_location(self, game_state, players): | ||
while True: | ||
try: | ||
position = input("Choose the position to place your piece : ") | ||
game_state.place_piece(position, game_state.game_turn.selected_piece) | ||
return | ||
except ValueError: | ||
game_state.message = "You have to type a free coordinate using this format : 'A1'" | ||
self.display_game(game_state) | ||
self.display_game(game_state, players) | ||
except KeyboardInterrupt: | ||
print("\nGame aborted") | ||
exit() | ||
|
||
def prompt_player_name(self, player_id, default_name): | ||
try: | ||
name = input("Player " + str(player_id) + ", what is your name (" + default_name + " if empty) ? ") | ||
if len(name) > 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's still possible to add an empty name with space There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's right. I don't see some real reason to forbid "spaces names" if the player has entered it, but I 'll manage for the game visibility There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bonus ;) |
||
return name | ||
return default_name | ||
except KeyboardInterrupt: | ||
print("\nGame aborted") | ||
exit() | ||
|
||
def prompt_restart(self): | ||
try: | ||
return input("Let's play again ? (o/n) ") == 'o' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. y/n |
||
except KeyboardInterrupt: | ||
print("\nGame aborted") | ||
exit() | ||
|
||
def piece_to_string(self, piece_id): | ||
piece_display = str(piece_id) | ||
pieces = list(filter(lambda x: x.id == piece_id, pieces_list_definition)) | ||
|
@@ -62,8 +79,6 @@ def piece_to_string(self, piece_id): | |
|
||
if pieces[0].light_color: | ||
piece_display = "\033[47m" + piece_display | ||
else: | ||
piece_display = "\033[100m" + piece_display | ||
|
||
return ' ' + piece_display + " \033[0m" | ||
|
||
|
@@ -91,7 +106,6 @@ def pieces_to_string(self, remaining_pieces, game_turn): | |
if piece_id >= 10: | ||
display_string += ' ' | ||
display_string += ' . ' | ||
display_string += ' ' | ||
display_string += '\n' | ||
|
||
for piece_id in range(1, PIECES_NUMBER + 1): | ||
|
@@ -102,17 +116,17 @@ def pieces_to_string(self, remaining_pieces, game_turn): | |
if piece_id >= 10: | ||
display_string += ' ' | ||
display_string += ' ' | ||
display_string += ' ' | ||
display_string += ' ' | ||
return display_string | ||
|
||
def selected_piece_to_string(self, piece_number, game_turn): | ||
if game_turn.selected_piece == piece_number: | ||
return "[" + str(piece_number) + "]" | ||
return " " + str(piece_number) + " " | ||
|
||
def players_to_string(self, game_turn): | ||
player_1 = self.selected_player_to_string('Player 1', game_turn.player_one_active) | ||
player_2 = self.selected_player_to_string('Player 2', not game_turn.player_one_active) | ||
def players_to_string(self, game_turn, players): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case you've create a "Player" class, it would be possible to implement a "special" method (str) to achieve this task (cast an object to a string representation). This is the Java equivalent of toString(). More informations here: https://openclassrooms.com/courses/apprenez-a-programmer-en-python/les-methodes-speciales-1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thinked about it, but I prefer to keep all render methods in the UI class |
||
player_1 = self.selected_player_to_string(players.player1_name, game_turn.player_one_active) | ||
player_2 = self.selected_player_to_string(players.player2_name, not game_turn.player_one_active) | ||
return player_1 + " " + player_2 | ||
|
||
def selected_player_to_string(self, player_name, selected): | ||
|
@@ -123,15 +137,15 @@ def selected_player_to_string(self, player_name, selected): | |
def clear_terminal(self): | ||
subprocess.call(["printf", "'\033c'"]) | ||
|
||
def display_game(self, game_state): | ||
def display_game(self, game_state, players): | ||
self.clear_terminal() | ||
print() | ||
print("\033[32;1mWelcome to Quarto-Py\033[0m") | ||
print() | ||
print() | ||
print(self.grid_to_string(game_state.grid)) | ||
print() | ||
print(self.players_to_string(game_state.game_turn)) | ||
print(self.players_to_string(game_state.game_turn, players)) | ||
print() | ||
print(self.pieces_to_string(game_state.remaining_pieces, game_state.game_turn)) | ||
print() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's very strange to have a "Players" class in "tools.py". Moreover, it seems that you use Players class as a property container, i'm not sure that it's the best way to achieve this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was also destined to store players scores (other story)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, all this informations are stored in only one object (a "game" state) to ensure data integrity and synchronicity.