diff --git a/server.py b/server.py
index 1549830..84bcb63 100755
--- a/server.py
+++ b/server.py
@@ -12,12 +12,18 @@ def main():
random.seed()
- wiz_game.the_game.create_deck()
+ #wiz_game.the_game.create_deck()
+ #wiz_game.the_game.add_player("p1", 1)
+ #wiz_game.the_game.add_player("p2", 2)
+ #wiz_game.the_game.add_player("p3", 3)
+ #wiz_game.the_game.add_player("p4", 4)
+ #wiz_game.the_game.deal_cards(12)
wiz_game.the_game.add_player("p1", 1)
wiz_game.the_game.add_player("p2", 2)
wiz_game.the_game.add_player("p3", 3)
- #wiz_game.the_game.add_player("p4", 4)
- wiz_game.the_game.deal_cards(12)
+ wiz_game.the_game.add_player("p4", 4)
+ wiz_game.the_game.init_game()
+
api.run(port=19203, address="127.0.0.1")
exit(0)
diff --git a/templates/home/player.html b/templates/home/player.html
index ce81961..73acbc2 100644
--- a/templates/home/player.html
+++ b/templates/home/player.html
@@ -9,7 +9,10 @@
Player {{player}} view
Table
- {{played_cards}}
+ {% for key,value in played_cards.items() %}
+ || {{key.name}}: {{value}}
+ {% endfor %}
+ ||
Your cards:
@@ -21,7 +24,9 @@
{% for card in cards %}
-
>card {{loop.index}} is: {{card}}
- play card {{loop.index}}<{{card}}>
+ {% if playerActive %}
+ play card {{loop.index}}<{{card}}>
+ {% endif %}
{% endfor %}
diff --git a/views/control.py b/views/control.py
index 32e4c1e..22555c5 100644
--- a/views/control.py
+++ b/views/control.py
@@ -5,7 +5,7 @@ from wiz_game import the_game
@api.route("/control/")
def index(req, resp):
- print(req.params)
+
resp.content = api.template('home/control.html', deck=the_game.card_deck,
trump_card=the_game.trump_card, trump_color=the_game.get_trump_color())
diff --git a/views/player.py b/views/player.py
index ee1b824..e66eb70 100644
--- a/views/player.py
+++ b/views/player.py
@@ -4,21 +4,25 @@ from wiz_game import the_game
@api.route("/player/{player}")
-def index(req, resp, player: str):
+def show(req, resp, player: str):
p = int(player)
+ name = the_game.players[p].name
tcard = the_game.trump_card
tcolor = the_game.get_trump_color()
table = the_game.table
- resp.content = api.template('home/player.html', player=player, cards=the_game.players[p].cards,
+ isActive = the_game.is_players_turn(p)
+ resp.content = api.template('home/player.html', playerActive=isActive, player=p, cards=the_game.players[p].cards,
trump_card=tcard, trump_color=tcolor, played_cards=table.played_cards)
@api.route("/player/{player}/play/{card}")
def play(req, resp, player: str, card: str):
p = int(player)
- pl = the_game.players[p]
- c = pl.play_card(int(card))
- the_game.table.play_card(p, c)
+ c = int(card)
+ #pl = the_game.players[p]
+ #c = pl.play_card(int(card))
+ #the_game.table.play_card(p, c)
+ the_game.play_card(p, c)
url = '/player/'+player
api.redirect(resp, url, status_code=303)
diff --git a/wiz_game.py b/wiz_game.py
index 88a0e5b..48bfd6f 100644
--- a/wiz_game.py
+++ b/wiz_game.py
@@ -22,42 +22,50 @@ class Card:
return str(self)
-class Table:
- def __init__(self):
- self.played_cards = {0: None, 1: None, 2: None, 3: None, 4: None, 5: None}
- self.num_players = 0
- self.trump_card = None
-
- def clear_table(self):
- self.played_cards = {0: None, 1: None, 2: None, 3: None, 4: None, 5: None}
-
- def play_card(self, player: int, card: Card):
- self.played_cards[player] = card
-
class Player:
def __init__(self, name, player_id):
self.name = name
self.id = player_id
self.cards = []
- #self.played_card = None
def set_cards(self, cards):
self.cards = sorted(cards, key=attrgetter('color', 'value'))
- def show_cards(self):
- print(self.cards)
+ #def show_cards(self):
+ # print(self.cards)
def play_card(self, card_idx):
c = self.cards[card_idx]
self.cards.remove(c)
return c
- #def play_card(self, card: Card):
- # self.cards.remove(card)
- # self.played_card = card
- #def reset_played_card(self):
- # self.played_card = None
+
+class Table:
+ def __init__(self):
+ #self.played_cards = {0: None, 1: None, 2: None, 3: None, 4: None, 5: None}
+ self.played_cards = dict()
+ self.num_players = 0
+ self.num_cards_played = 0
+ self.trump_card = None
+
+ def init(self):
+ self.played_cards = dict()
+ self.num_players = 0
+ self.num_cards_played = 0
+ self.trump_card = None
+
+ def add_player(self, p: Player):
+ self.played_cards[p] = None
+
+ def clear_table(self):
+ for key,value in self.played_cards.items():
+ self.played_cards[key] = 0
+ self.num_cards_played = 0
+
+ def play_card(self, player: Player, card: Card):
+ self.played_cards[player] = card
+ self.num_cards_played += 1
class WizGame:
@@ -66,8 +74,12 @@ class WizGame:
self.card_deck = []
self.trump_card = None
self.table = Table()
+ self.start_player = None
+ self.active_player = None
+ self.current_round = 1
def create_deck(self):
+ self.card_deck = []
for color in ["b", "r", "g", "y"]:
for val in range(1, 14):
self.card_deck.append(Card(color, val))
@@ -75,8 +87,7 @@ class WizGame:
self.card_deck.append(Card('-', 'Z'))
for _ in range(1, 5):
self.card_deck.append(Card('-', 'N'))
- print("carddeck:")
- print(self.card_deck)
+ #print(self.card_deck)
def get_trump_color(self):
card = self.trump_card
@@ -95,23 +106,64 @@ class WizGame:
cs = list(chunks(self.card_deck, cards_per_player))
for idx, p in enumerate(self.players):
p.set_cards(cs[idx])
- p.show_cards()
+ #p.show_cards()
if len(cs) > len(self.players):
cc = cs[len(self.players)]
c = cc[0]
- #self.set_trump_color(c)
self.trump_card = c
else:
- #self.trump_color = None
self.trump_card = None
def add_player(self, name: str, player_id):
if len(self.players) < 6:
- self.players.append((Player(name, player_id)))
+ p = Player(name, player_id)
+ self.players.append(p)
+ self.table.add_player(p)
- def start_game(self):
+ def get_active_player(self):
+ return self.players[self.active_player]
+
+ def is_round_finished(self):
+ if self.table.num_cards_played < self.table.num_players:
+ return False
+ else:
+ return True
+
+ def next_round(self):
+ max_rounds = len(self.card_deck)/len(self.players)
+ if self.current_round <= max_rounds:
+ self.current_round += 1
+ self.start_player = self.start_player+1 if self.start_player < len(self.players) else 0
+ self.active_player = self.start_player
+ self.table.clear_table()
+ self.deal_cards(self.current_round)
+ return True
+ else:
+ return False
+
+ def init_game(self):
+ self.create_deck()
+ self.current_round = 1
+ self.table.clear_table()
+ self.active_player = 0
+ self.start_player = 0
+ self.deal_cards(1)
self.table.num_players = len(self.players)
+ def play_card(self, player, card):
+ p = self.players[player]
+ c = p.play_card(card)
+ self.table.play_card(p, c)
+ self.next_player()
+
+ def next_player(self):
+ self.active_player = (self.active_player + 1) if self.active_player < len(self.players) else 0
+
+ def is_players_turn(self, player):
+ if player == self.active_player:
+ return True
+ else:
+ return False
the_game = WizGame()