implementing game logic

This commit is contained in:
Stefan Rupp 2020-04-12 15:27:05 +02:00
parent 663457d4bc
commit 2862e73e19
6 changed files with 106 additions and 39 deletions

View File

@ -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)

View File

@ -9,7 +9,10 @@
<br>
<strong>Player {{player}} view</strong><br>
<strong> Table </strong><br>
{{played_cards}}
{% for key,value in played_cards.items() %}
|| {{key.name}}: {{value}}
{% endfor %}
||
<br>
<strong> Your cards: </strong>
<ul>
@ -21,7 +24,9 @@
{% for card in cards %}
<li>
<strong>>card {{loop.index}} is: {{card}} </strong><br>
{% if playerActive %}
<a href="/player/{{player}}/play/{{loop.index0}}">play card {{loop.index}}&lt;{{card}}&gt;</a>
{% endif %}
</li>
{% endfor %}
<!--

View File

@ -7,7 +7,7 @@
<meta name="description" content="pyramid web application">
<meta name="author" content="Pylons Project">
<title>MovieDB Service</title>
<title>Free Online Gamblingc</title>
<!-- Bootstrap core CSS -->
<link href="//oss.maxcdn.com/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">

View File

@ -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())

View File

@ -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)

View File

@ -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()