implementing game logic
This commit is contained in:
parent
663457d4bc
commit
2862e73e19
12
server.py
12
server.py
@ -12,12 +12,18 @@ def main():
|
|||||||
|
|
||||||
random.seed()
|
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("p1", 1)
|
||||||
wiz_game.the_game.add_player("p2", 2)
|
wiz_game.the_game.add_player("p2", 2)
|
||||||
wiz_game.the_game.add_player("p3", 3)
|
wiz_game.the_game.add_player("p3", 3)
|
||||||
#wiz_game.the_game.add_player("p4", 4)
|
wiz_game.the_game.add_player("p4", 4)
|
||||||
wiz_game.the_game.deal_cards(12)
|
wiz_game.the_game.init_game()
|
||||||
|
|
||||||
|
|
||||||
api.run(port=19203, address="127.0.0.1")
|
api.run(port=19203, address="127.0.0.1")
|
||||||
exit(0)
|
exit(0)
|
||||||
|
@ -9,7 +9,10 @@
|
|||||||
<br>
|
<br>
|
||||||
<strong>Player {{player}} view</strong><br>
|
<strong>Player {{player}} view</strong><br>
|
||||||
<strong> Table </strong><br>
|
<strong> Table </strong><br>
|
||||||
{{played_cards}}
|
{% for key,value in played_cards.items() %}
|
||||||
|
|| {{key.name}}: {{value}}
|
||||||
|
{% endfor %}
|
||||||
|
||
|
||||||
<br>
|
<br>
|
||||||
<strong> Your cards: </strong>
|
<strong> Your cards: </strong>
|
||||||
<ul>
|
<ul>
|
||||||
@ -21,7 +24,9 @@
|
|||||||
{% for card in cards %}
|
{% for card in cards %}
|
||||||
<li>
|
<li>
|
||||||
<strong>>card {{loop.index}} is: {{card}} </strong><br>
|
<strong>>card {{loop.index}} is: {{card}} </strong><br>
|
||||||
<a href="/player/{{player}}/play/{{loop.index0}}">play card {{loop.index}}<{{card}}></a>
|
{% if playerActive %}
|
||||||
|
<a href="/player/{{player}}/play/{{loop.index0}}">play card {{loop.index}}<{{card}}></a>
|
||||||
|
{% endif %}
|
||||||
</li>
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<!--
|
<!--
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
<meta name="description" content="pyramid web application">
|
<meta name="description" content="pyramid web application">
|
||||||
<meta name="author" content="Pylons Project">
|
<meta name="author" content="Pylons Project">
|
||||||
|
|
||||||
<title>MovieDB Service</title>
|
<title>Free Online Gamblingc</title>
|
||||||
|
|
||||||
<!-- Bootstrap core CSS -->
|
<!-- Bootstrap core CSS -->
|
||||||
<link href="//oss.maxcdn.com/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
|
<link href="//oss.maxcdn.com/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
@ -5,7 +5,7 @@ from wiz_game import the_game
|
|||||||
|
|
||||||
@api.route("/control/")
|
@api.route("/control/")
|
||||||
def index(req, resp):
|
def index(req, resp):
|
||||||
print(req.params)
|
|
||||||
resp.content = api.template('home/control.html', deck=the_game.card_deck,
|
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())
|
trump_card=the_game.trump_card, trump_color=the_game.get_trump_color())
|
||||||
|
|
||||||
|
@ -4,21 +4,25 @@ from wiz_game import the_game
|
|||||||
|
|
||||||
|
|
||||||
@api.route("/player/{player}")
|
@api.route("/player/{player}")
|
||||||
def index(req, resp, player: str):
|
def show(req, resp, player: str):
|
||||||
p = int(player)
|
p = int(player)
|
||||||
|
name = the_game.players[p].name
|
||||||
tcard = the_game.trump_card
|
tcard = the_game.trump_card
|
||||||
tcolor = the_game.get_trump_color()
|
tcolor = the_game.get_trump_color()
|
||||||
table = the_game.table
|
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)
|
trump_card=tcard, trump_color=tcolor, played_cards=table.played_cards)
|
||||||
|
|
||||||
|
|
||||||
@api.route("/player/{player}/play/{card}")
|
@api.route("/player/{player}/play/{card}")
|
||||||
def play(req, resp, player: str, card: str):
|
def play(req, resp, player: str, card: str):
|
||||||
p = int(player)
|
p = int(player)
|
||||||
pl = the_game.players[p]
|
c = int(card)
|
||||||
c = pl.play_card(int(card))
|
#pl = the_game.players[p]
|
||||||
the_game.table.play_card(p, c)
|
#c = pl.play_card(int(card))
|
||||||
|
#the_game.table.play_card(p, c)
|
||||||
|
the_game.play_card(p, c)
|
||||||
url = '/player/'+player
|
url = '/player/'+player
|
||||||
api.redirect(resp, url, status_code=303)
|
api.redirect(resp, url, status_code=303)
|
||||||
|
|
||||||
|
106
wiz_game.py
106
wiz_game.py
@ -22,42 +22,50 @@ class Card:
|
|||||||
return str(self)
|
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:
|
class Player:
|
||||||
def __init__(self, name, player_id):
|
def __init__(self, name, player_id):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.id = player_id
|
self.id = player_id
|
||||||
self.cards = []
|
self.cards = []
|
||||||
#self.played_card = None
|
|
||||||
|
|
||||||
def set_cards(self, cards):
|
def set_cards(self, cards):
|
||||||
self.cards = sorted(cards, key=attrgetter('color', 'value'))
|
self.cards = sorted(cards, key=attrgetter('color', 'value'))
|
||||||
|
|
||||||
def show_cards(self):
|
#def show_cards(self):
|
||||||
print(self.cards)
|
# print(self.cards)
|
||||||
|
|
||||||
def play_card(self, card_idx):
|
def play_card(self, card_idx):
|
||||||
c = self.cards[card_idx]
|
c = self.cards[card_idx]
|
||||||
self.cards.remove(c)
|
self.cards.remove(c)
|
||||||
return 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:
|
class WizGame:
|
||||||
@ -66,8 +74,12 @@ class WizGame:
|
|||||||
self.card_deck = []
|
self.card_deck = []
|
||||||
self.trump_card = None
|
self.trump_card = None
|
||||||
self.table = Table()
|
self.table = Table()
|
||||||
|
self.start_player = None
|
||||||
|
self.active_player = None
|
||||||
|
self.current_round = 1
|
||||||
|
|
||||||
def create_deck(self):
|
def create_deck(self):
|
||||||
|
self.card_deck = []
|
||||||
for color in ["b", "r", "g", "y"]:
|
for color in ["b", "r", "g", "y"]:
|
||||||
for val in range(1, 14):
|
for val in range(1, 14):
|
||||||
self.card_deck.append(Card(color, val))
|
self.card_deck.append(Card(color, val))
|
||||||
@ -75,8 +87,7 @@ class WizGame:
|
|||||||
self.card_deck.append(Card('-', 'Z'))
|
self.card_deck.append(Card('-', 'Z'))
|
||||||
for _ in range(1, 5):
|
for _ in range(1, 5):
|
||||||
self.card_deck.append(Card('-', 'N'))
|
self.card_deck.append(Card('-', 'N'))
|
||||||
print("carddeck:")
|
#print(self.card_deck)
|
||||||
print(self.card_deck)
|
|
||||||
|
|
||||||
def get_trump_color(self):
|
def get_trump_color(self):
|
||||||
card = self.trump_card
|
card = self.trump_card
|
||||||
@ -95,23 +106,64 @@ class WizGame:
|
|||||||
cs = list(chunks(self.card_deck, cards_per_player))
|
cs = list(chunks(self.card_deck, cards_per_player))
|
||||||
for idx, p in enumerate(self.players):
|
for idx, p in enumerate(self.players):
|
||||||
p.set_cards(cs[idx])
|
p.set_cards(cs[idx])
|
||||||
p.show_cards()
|
#p.show_cards()
|
||||||
if len(cs) > len(self.players):
|
if len(cs) > len(self.players):
|
||||||
cc = cs[len(self.players)]
|
cc = cs[len(self.players)]
|
||||||
c = cc[0]
|
c = cc[0]
|
||||||
#self.set_trump_color(c)
|
|
||||||
self.trump_card = c
|
self.trump_card = c
|
||||||
else:
|
else:
|
||||||
#self.trump_color = None
|
|
||||||
self.trump_card = None
|
self.trump_card = None
|
||||||
|
|
||||||
def add_player(self, name: str, player_id):
|
def add_player(self, name: str, player_id):
|
||||||
if len(self.players) < 6:
|
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)
|
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()
|
the_game = WizGame()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user