This commit is contained in:
Stefan Rupp 2020-04-13 22:19:16 +02:00
parent a7d9df1885
commit 0ac030e190
2 changed files with 1 additions and 218 deletions

View File

@ -7,6 +7,7 @@ from app_instance import api
from routes import * from routes import *
import wiz_game import wiz_game
def main(): def main():
print("main started") print("main started")
@ -16,27 +17,6 @@ def main():
api.run(port=19203, address="127.0.0.1") api.run(port=19203, address="127.0.0.1")
exit(0) exit(0)
# w.create_deck()
# w.add_player("p1", 1)
# w.add_player("p2", 2)
# w.add_player("p3", 3)
# w.add_player("p4", 4)
# w.deal_cards(1)
# print("round 2")
# w.deal_cards(2)
# print("round 3")
# w.deal_cards(3)
# print("round 4")
# w.deal_cards(4)
# print("round 12")
# w.deal_cards(12)
# print("round 13")
# w.deal_cards(13)
# print("round 14")
# w.deal_cards(14)
# print("round 15")
# w.deal_cards(15)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -62,50 +62,12 @@ class Player:
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):
# 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
class Table:
def __init__(self):
#self.played_cards = {0: None, 1: None, 2: None, 3: None, 4: None, 5: None}
self.players = list()
self.played_cards = list()
self.num_cards_played = 0
self.trump_card = None
def init(self):
self.players.clear()
self.played_cards.clear()
self.num_cards_played = 0
self.trump_card = None
def add_player(self, p: Player):
self.players.append(p)
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):
t = (player, card)
self.played_cards[player.id] = t
self.num_cards_played += 1
class WizGame: class WizGame:
def __init__(self, player_names: list): def __init__(self, player_names: list):
self.players = [Player(pn, idx) for idx, pn in enumerate(player_names)] self.players = [Player(pn, idx) for idx, pn in enumerate(player_names)]
@ -205,7 +167,6 @@ class WizGame:
def next_trick(self): def next_trick(self):
win = self.get_trick_winner() win = self.get_trick_winner()
winner = win[0] winner = win[0]
#self.start_player = winner.id
starter = winner.id starter = winner.id
self.players_ordered = self.players[starter:] + self.players[:starter] self.players_ordered = self.players[starter:] + self.players[:starter]
self.active_player = 0 self.active_player = 0
@ -216,7 +177,6 @@ class WizGame:
c = p.play_card(card) c = p.play_card(card)
t = (p, c) t = (p, c)
self.played_cards.append(t) self.played_cards.append(t)
#self.next_player()
self.active_player += 1 self.active_player += 1
if self.is_trick_finished(): if self.is_trick_finished():
self.num_tricks_played += 1 self.num_tricks_played += 1
@ -231,14 +191,6 @@ class WizGame:
self.trump_card.color = trump_color self.trump_card.color = trump_color
return True return True
# def next_player(self):
# 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): def get_prev_player(self):
if self.start_player == 0: if self.start_player == 0:
return len(self.players)-1 return len(self.players)-1
@ -246,155 +198,6 @@ class WizGame:
return self.start_player-1 return self.start_player-1
class WizGame_legacy:
def __init__(self):
self.players = []
self.card_deck = []
self.trump_card = None
self.table = Table()
self.start_player = 0
self.active_player = 0
self.current_round = 1
self.num_tricks_played = 0
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))
for _ in range(1, 5):
self.card_deck.append(Card('-', 'Z'))
for _ in range(1, 5):
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:
if card.color:
return card.color
elif card.value == 'Z':
return 'choose'
else:
return None
else:
return None
def deal_cards(self, cards_per_player):
random.shuffle(self.card_deck)
cs = list(chunks(self.card_deck, cards_per_player))
for idx, p in enumerate(self.players):
p.set_cards(cs[idx])
#p.show_cards()
if len(cs) > len(self.players):
cc = cs[len(self.players)]
c = cc[0]
self.trump_card = c
else:
self.trump_card = None
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_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 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 += 1
if self.start_player >= len(self.players):
self.start_player = 0
self.active_player = self.start_player
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.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)
def play_card(self, player, card):
p = self.players[player]
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):
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:
return False
p = ["p1", "p2", "p3", "p4"] p = ["p1", "p2", "p3", "p4"]
the_game = WizGame(p) the_game = WizGame(p)