Skip to content
This repository was archived by the owner on Apr 17, 2019. It is now read-only.

[WIP] Players can choose a name and they can replay a party #15

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
33 changes: 22 additions & 11 deletions src/game/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .piece import Piece
from .turn import Turn
from .state import State
from .tools import GRID_SIZE, PIECES_NUMBER
from .tools import GRID_SIZE, PIECES_NUMBER, Players
from .ui import UIRender


Expand All @@ -15,6 +15,11 @@ class Game:

def start(self):
game_state = State()
ui = UIRender()

ui.clear_terminal()
Players.player1_name = ui.prompt_player_name(1, Players.player1_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you mutating static class attributes on the fly ? :o

Players.player2_name = ui.prompt_player_name(2, Players.player2_name)

initial_state = ""
try:
Expand All @@ -28,18 +33,24 @@ def start(self):
if (len(game_state.message) > 0):
game_state.message += """\nValid sample : --state='{"grid" : {"A2": 10,"C1":3,"D1":12},"turn" :{"player" : 1,"selected" : 7}}'"""

ui = UIRender()
while not game_state.check_draw():
if game_state.check_winner():
break
ui.display_game(game_state)
if not game_state.is_selected_piece():
ui.prompt_piece_selection(game_state)
game_state.switch_player()
replay = True
while replay:

while not game_state.check_draw():
if game_state.check_winner():
break
ui.display_game(game_state)
ui.prompt_piece_location(game_state)
if not game_state.is_selected_piece():
ui.prompt_piece_selection(game_state)
game_state.switch_player()
ui.display_game(game_state)
ui.prompt_piece_location(game_state)

ui.display_game(game_state)
replay = ui.prompt_restart()

ui.display_game(game_state)
if replay:
game_state = State()

def parse_state_from_args(self, argv):
if 'quarto.py' in argv[0]:
Expand Down
6 changes: 3 additions & 3 deletions src/game/state.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .turn import Turn
from .piece import Piece
from .tools import GRID_SIZE, PIECES_NUMBER, EMPTY_POSITION, get_coordinates
from .tools import GRID_SIZE, PIECES_NUMBER, EMPTY_POSITION, get_coordinates, Players


class State:
Expand Down Expand Up @@ -69,9 +69,9 @@ def switch_player(self):
def check_winner(self):
if self.check_raws_winning() or self.check_columns_winning() or self.check_diags_winning():
if self.game_turn.player_one_active:
player_name = "Player 1"
player_name = Players.player1_name
else:
player_name = "Player 2"
player_name = Players.player2_name
self.message = player_name + " WINNNNS !!!!!!\n"
return True
return False
Expand Down
3 changes: 2 additions & 1 deletion src/game/tests/test_players.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from ..turn import Turn
from ..ui import UIRender
from ..tools import Players


class TestPlayersMethods(unittest.TestCase):
Expand All @@ -16,7 +17,7 @@ def test_init_game_turn_should_start_without_selected_piece(self):
def test_players_to_string_should_create_the_reference_string(self):
game_turn = Turn()
players_display = UIRender().players_to_string(game_turn)
reference_string = "=> Player 1 <= Player 2 "
reference_string = "=> " + Players.player1_name + " <= " + Players.player2_name + " "
self.assertEqual(players_display, reference_string)


Expand Down
8 changes: 8 additions & 0 deletions src/game/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
EMPTY_POSITION = '.'


class Players:
Copy link
Contributor

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.

Copy link
Collaborator Author

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)

Copy link
Contributor

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.


"""Definition of the users names:"""

player1_name = "Player 1"
player2_name = "Player 2"


def get_coordinates(position):
"""Convert a postion of format 'A3' into coordinates x= 0 and y = 2 in the grid"""
if len(position) != 2:
Expand Down
28 changes: 21 additions & 7 deletions src/game/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .piece import pieces_list_definition
from .turn import Turn
from .state import State
from .tools import GRID_SIZE, PIECES_NUMBER
from .tools import GRID_SIZE, PIECES_NUMBER, Players


class UIRender:
Expand Down Expand Up @@ -40,6 +40,23 @@ def prompt_piece_location(self, game_state):
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still possible to add an empty name with space

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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 another party ? (o/n) ") == 'o'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/party/game/

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))
Expand All @@ -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"

Expand Down Expand Up @@ -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):
Expand All @@ -102,7 +116,7 @@ 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):
Expand All @@ -111,8 +125,8 @@ def selected_piece_to_string(self, piece_number, game_turn):
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)
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):
Expand Down