starting reworking
This commit is contained in:
parent
2862e73e19
commit
c851d42bd1
@ -18,10 +18,10 @@ def main():
|
||||
#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.add_player("p1")
|
||||
wiz_game.the_game.add_player("p2")
|
||||
wiz_game.the_game.add_player("p3")
|
||||
wiz_game.the_game.add_player("p4")
|
||||
wiz_game.the_game.init_game()
|
||||
|
||||
|
||||
|
@ -14,10 +14,19 @@
|
||||
<strong>>{{loop.index}}: {{card}} </strong> |
|
||||
{% endfor %}
|
||||
|
||||
<li>
|
||||
<br>
|
||||
<strong>Trump Card: {{trump_card}}</strong><br>
|
||||
<strong>Trump Color: {{trump_color}}</strong><br>
|
||||
</li>
|
||||
<br>
|
||||
{% if trick_finished %}
|
||||
Winner: {{trick_winner}} with Card {{highest_card}}
|
||||
<a href="/control/next_trick/">start next trick</a>
|
||||
{% endif %}
|
||||
{% if hand_finished %}
|
||||
Winner: {{trick_winner}} with Card {{highest_card}}
|
||||
<a href="/control/next_hand/">start next hand</a>
|
||||
{% endif %}
|
||||
<!--
|
||||
<ul>
|
||||
<li>
|
||||
<strong>Deal cards</strong><br>
|
||||
@ -30,6 +39,7 @@
|
||||
<a href="/control/deal/20">deal 20</a>
|
||||
</li>
|
||||
</ul>
|
||||
-->
|
||||
</p>
|
||||
|
||||
<p class="disclaimer">
|
||||
|
@ -7,10 +7,17 @@
|
||||
<p class="lead">
|
||||
Play a nice game of cards?<br>
|
||||
<br>
|
||||
<strong>Player {{player}} view</strong><br>
|
||||
<strong>Player {{player+1}} view</strong><br>
|
||||
<strong> Table </strong><br>
|
||||
{% for key,value in played_cards.items() %}
|
||||
|| {{key.name}}: {{value}}
|
||||
{% for player,card in played_cards %}
|
||||
||
|
||||
{% if player.id == active_player %}
|
||||
<font color="red">
|
||||
{% endif %}
|
||||
{{player.name}}: {{card}}
|
||||
{% if player.id == active_player %}
|
||||
</font>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
||
|
||||
<br>
|
||||
@ -24,11 +31,18 @@
|
||||
{% for card in cards %}
|
||||
<li>
|
||||
<strong>>card {{loop.index}} is: {{card}} </strong><br>
|
||||
{% if playerActive %}
|
||||
{% if playerActive and not choose_trump_color %}
|
||||
<a href="/player/{{player}}/play/{{loop.index0}}">play card {{loop.index}}<{{card}}></a>
|
||||
{% endif %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
{% if choose_trump_color %}
|
||||
<strong>Choose Trump Color:</strong>
|
||||
<a href="/player/{{player}}/set_trump/r">Red></a>
|
||||
<a href="/player/{{player}}/set_trump/g">Green></a>
|
||||
<a href="/player/{{player}}/set_trump/b">Blue></a>
|
||||
<a href="/player/{{player}}/set_trump/y">Yellow ></a>
|
||||
{% endif %}
|
||||
<!--
|
||||
<li>
|
||||
<strong>Movie by IMDB code</strong><br>
|
||||
|
@ -5,10 +5,41 @@ from wiz_game import the_game
|
||||
|
||||
@api.route("/control/")
|
||||
def index(req, resp):
|
||||
current_round = the_game.current_round
|
||||
hand_finished = the_game.is_hand_finished()
|
||||
trick_finished = the_game.is_trick_finished()
|
||||
|
||||
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())
|
||||
if hand_finished:
|
||||
trick_winner, highest_card = the_game.get_trick_winner()
|
||||
winner = trick_winner.name
|
||||
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(),
|
||||
hand_finished=True, trick_finished=False, trick_winner=winner,
|
||||
highest_card=highest_card
|
||||
)
|
||||
elif trick_finished:
|
||||
trick_winner, highest_card = the_game.get_trick_winner()
|
||||
winner = trick_winner.name
|
||||
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(),
|
||||
round_finished=False, trick_finished=True, trick_winner=winner,
|
||||
highest_card=highest_card
|
||||
)
|
||||
else:
|
||||
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(),
|
||||
round_finished=False, trick_finished=False
|
||||
)
|
||||
|
||||
@api.route("/control/next_hand/")
|
||||
def start_next_hand(req, resp):
|
||||
the_game.next_hand()
|
||||
api.redirect(resp, '/control/', status_code=303)
|
||||
|
||||
@api.route("/control/next_trick/")
|
||||
def start_next_trick(req, resp):
|
||||
the_game.next_trick()
|
||||
api.redirect(resp, '/control/', status_code=303)
|
||||
|
||||
@api.route("/control/deal/{cards}")
|
||||
def deal(req, resp, cards):
|
||||
|
@ -11,7 +11,14 @@ def show(req, resp, player: str):
|
||||
tcolor = the_game.get_trump_color()
|
||||
table = the_game.table
|
||||
isActive = the_game.is_players_turn(p)
|
||||
resp.content = api.template('home/player.html', playerActive=isActive, player=p, cards=the_game.players[p].cards,
|
||||
active_player = the_game.active_player
|
||||
prev_player = the_game.get_prev_player()
|
||||
if tcard.value == 'Z' and p == prev_player and tcolor == '-':
|
||||
choose_trump_color = True
|
||||
else:
|
||||
choose_trump_color = False
|
||||
resp.content = api.template('home/player.html', playerActive=isActive, active_player=active_player, player=p,
|
||||
cards=the_game.players[p].cards, choose_trump_color=choose_trump_color,
|
||||
trump_card=tcard, trump_color=tcolor, played_cards=table.played_cards)
|
||||
|
||||
|
||||
@ -19,10 +26,13 @@ def show(req, resp, player: str):
|
||||
def play(req, resp, player: str, card: str):
|
||||
p = int(player)
|
||||
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)
|
||||
|
||||
@api.route("/player/{player}/set_trump/{trump}")
|
||||
def set_trump_color(req, resp, player, trump):
|
||||
p = int(player)
|
||||
the_game.set_trump_color(trump)
|
||||
url = '/player/'+player
|
||||
api.redirect(resp, url, status_code=303)
|
||||
|
137
wiz_game.py
137
wiz_game.py
@ -22,6 +22,37 @@ class Card:
|
||||
return str(self)
|
||||
|
||||
|
||||
def get_higher_card(card1: Card, card2: Card, trump_color: str):
|
||||
"""
|
||||
compares two cards, assuming card1 was played first and card2 played second.
|
||||
returns: True if card2 beats card1, otherwise False
|
||||
"""
|
||||
if card2.value == 'N':
|
||||
# no way to win in 'N' comes in second
|
||||
return False
|
||||
elif card1.value == 'Z':
|
||||
# no way to win if 'Z' was already played
|
||||
return False
|
||||
elif card1.value == 'N':
|
||||
# any other than 'N' will beat us here, and the special case
|
||||
# where card2 also is 'N' was already handled above
|
||||
return True
|
||||
elif card2.value == 'Z':
|
||||
# we only lose if card1 is also 'Z', and that was handled above,
|
||||
# so this is an easy case also
|
||||
return True
|
||||
else:
|
||||
if card1.color == card2.color:
|
||||
if card1.value > card2.value:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
elif card2.color == trump_color:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
class Player:
|
||||
def __init__(self, name, player_id):
|
||||
self.name = name
|
||||
@ -40,31 +71,38 @@ class Player:
|
||||
return c
|
||||
|
||||
|
||||
|
||||
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.players = list()
|
||||
self.played_cards = list()
|
||||
self.num_cards_played = 0
|
||||
self.trump_card = None
|
||||
|
||||
def init(self):
|
||||
self.played_cards = dict()
|
||||
self.num_players = 0
|
||||
self.players.clear()
|
||||
self.played_cards.clear()
|
||||
self.num_cards_played = 0
|
||||
self.trump_card = None
|
||||
|
||||
def add_player(self, p: Player):
|
||||
self.played_cards[p] = None
|
||||
self.players.append(p)
|
||||
|
||||
def clear_table(self):
|
||||
for key,value in self.played_cards.items():
|
||||
self.played_cards[key] = 0
|
||||
def clear_table(self, start_player):
|
||||
self.played_cards.clear()
|
||||
p1 = self.players[start_player:]
|
||||
p2 = self.players[:start_player]
|
||||
for p in p1:
|
||||
t = (p, None)
|
||||
self.played_cards.append(t)
|
||||
for p in p2:
|
||||
t = (p, None)
|
||||
self.played_cards.append(t)
|
||||
self.num_cards_played = 0
|
||||
|
||||
def play_card(self, player: Player, card: Card):
|
||||
self.played_cards[player] = card
|
||||
t = (player, card)
|
||||
self.played_cards[player.id] = t
|
||||
self.num_cards_played += 1
|
||||
|
||||
|
||||
@ -74,9 +112,10 @@ class WizGame:
|
||||
self.card_deck = []
|
||||
self.trump_card = None
|
||||
self.table = Table()
|
||||
self.start_player = None
|
||||
self.active_player = None
|
||||
self.start_player = 0
|
||||
self.active_player = 0
|
||||
self.current_round = 1
|
||||
self.num_tricks_played = 0
|
||||
|
||||
def create_deck(self):
|
||||
self.card_deck = []
|
||||
@ -89,6 +128,16 @@ class WizGame:
|
||||
self.card_deck.append(Card('-', 'N'))
|
||||
#print(self.card_deck)
|
||||
|
||||
def set_trump_color(self, trump_color):
|
||||
if self.trump_card.value != 'Z':
|
||||
return False
|
||||
else:
|
||||
if trump_color not in ["b", "r", "g", "y"]:
|
||||
return False
|
||||
else:
|
||||
self.trump_card.color = trump_color
|
||||
return True
|
||||
|
||||
def get_trump_color(self):
|
||||
card = self.trump_card
|
||||
if card:
|
||||
@ -114,39 +163,64 @@ class WizGame:
|
||||
else:
|
||||
self.trump_card = None
|
||||
|
||||
def add_player(self, name: str, player_id):
|
||||
def add_player(self, name: str):
|
||||
if len(self.players) < 6:
|
||||
player_id = len(self.players)
|
||||
p = Player(name, player_id)
|
||||
self.players.append(p)
|
||||
self.table.add_player(p)
|
||||
|
||||
def get_active_player(self):
|
||||
return self.players[self.active_player]
|
||||
# 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:
|
||||
def get_trick_winner(self):
|
||||
cards = self.table.played_cards
|
||||
high_player, high_card = cards[0]
|
||||
|
||||
for player, card in cards[1:]:
|
||||
if get_higher_card(high_card, card, self.trump_card.color):
|
||||
high_card = card
|
||||
high_player = player
|
||||
return high_player, high_card
|
||||
|
||||
def is_trick_finished(self):
|
||||
if self.table.num_cards_played < len(self.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:
|
||||
def is_hand_finished(self):
|
||||
h = self.is_trick_finished()
|
||||
if h and self.num_tricks_played == self.current_round:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def next_hand(self):
|
||||
max_hands = len(self.card_deck)/len(self.players)
|
||||
if self.current_round <= max_hands:
|
||||
self.current_round += 1
|
||||
self.start_player = self.start_player+1 if self.start_player < len(self.players) else 0
|
||||
self.start_player += 1
|
||||
if self.start_player >= len(self.players):
|
||||
self.start_player = 0
|
||||
self.active_player = self.start_player
|
||||
self.table.clear_table()
|
||||
self.table.clear_table(self.start_player)
|
||||
self.deal_cards(self.current_round)
|
||||
self.num_tricks_played = 0
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def next_trick(self):
|
||||
self.table.clear_table(self.start_player)
|
||||
self.start_player = self.get_trick_winner().id
|
||||
|
||||
def init_game(self):
|
||||
self.create_deck()
|
||||
self.current_round = 1
|
||||
self.table.clear_table()
|
||||
self.active_player = 0
|
||||
self.start_player = 0
|
||||
self.table.clear_table(self.start_player)
|
||||
self.deal_cards(1)
|
||||
self.table.num_players = len(self.players)
|
||||
|
||||
@ -155,11 +229,26 @@ class WizGame:
|
||||
c = p.play_card(card)
|
||||
self.table.play_card(p, c)
|
||||
self.next_player()
|
||||
if self.is_trick_finished():
|
||||
self.num_tricks_played += 1
|
||||
|
||||
def next_player(self):
|
||||
self.active_player = (self.active_player + 1) if self.active_player < len(self.players) else 0
|
||||
if not self.is_hand_finished():
|
||||
self.active_player += 1
|
||||
if self.active_player >= len(self.players):
|
||||
self.active_player = 0
|
||||
else:
|
||||
self.active_player = None
|
||||
|
||||
def get_prev_player(self):
|
||||
if self.start_player == 0:
|
||||
return len(self.players)-1
|
||||
else:
|
||||
return self.start_player-1
|
||||
|
||||
def is_players_turn(self, player):
|
||||
if self.is_trick_finished():
|
||||
return False;
|
||||
if player == self.active_player:
|
||||
return True
|
||||
else:
|
||||
|
Loading…
Reference in New Issue
Block a user