|
@@ -1,7 +1,8 @@
|
|
|
-import json;
|
|
|
|
|
-import random;
|
|
|
|
|
-from player import Player, Color;
|
|
|
|
|
|
|
+import random
|
|
|
|
|
+from typing import List, Dict
|
|
|
|
|
+
|
|
|
from command import Command, Option
|
|
from command import Command, Option
|
|
|
|
|
+from player import Player, Color
|
|
|
|
|
|
|
|
suits = ["Harten", "Schoppen", "Klaver", "Ruiten"]
|
|
suits = ["Harten", "Schoppen", "Klaver", "Ruiten"]
|
|
|
denoms = ["Aas", "Heer", "Vrouw", "Boer", "10", "9", "8", "7", "6", "5", "4", "3", "2"]
|
|
denoms = ["Aas", "Heer", "Vrouw", "Boer", "10", "9", "8", "7", "6", "5", "4", "3", "2"]
|
|
@@ -10,17 +11,22 @@ denoms = ["Aas", "Heer", "Vrouw", "Boer", "10", "9", "8", "7", "6", "5", "4", "3
|
|
|
class Game(object):
|
|
class Game(object):
|
|
|
"""description of class"""
|
|
"""description of class"""
|
|
|
|
|
|
|
|
- def __init__(self):
|
|
|
|
|
- self.__init([
|
|
|
|
|
- Player(Color.RED, "Rood"),
|
|
|
|
|
- Player(Color.BLUE, "Blauw"),
|
|
|
|
|
- Player(Color.YELLOW, "Geel"),
|
|
|
|
|
- Player(Color.GREEN, "Groen")])
|
|
|
|
|
|
|
+ players: Dict[Color, Player]
|
|
|
|
|
+ current_player: Player
|
|
|
|
|
+ stock: List[str]
|
|
|
|
|
+ number_of_cards: int
|
|
|
|
|
+
|
|
|
|
|
+ def __init__(self, players=None):
|
|
|
|
|
+ if players is None:
|
|
|
|
|
+ players = [
|
|
|
|
|
+ Player(Color.RED, "Rood"),
|
|
|
|
|
+ Player(Color.BLUE, "Blauw"),
|
|
|
|
|
+ Player(Color.YELLOW, "Geel"),
|
|
|
|
|
+ Player(Color.GREEN, "Groen")]
|
|
|
|
|
|
|
|
- def __init__(self, players):
|
|
|
|
|
self.stock = []
|
|
self.stock = []
|
|
|
random.seed()
|
|
random.seed()
|
|
|
- self.numberOfCards = 0
|
|
|
|
|
|
|
+ self.number_of_cards = 0
|
|
|
|
|
|
|
|
self.players = dict((player.color, player) for player in players)
|
|
self.players = dict((player.color, player) for player in players)
|
|
|
|
|
|
|
@@ -28,75 +34,71 @@ class Game(object):
|
|
|
player.options = [Option(Command.DEAL, None, "Delen")]
|
|
player.options = [Option(Command.DEAL, None, "Delen")]
|
|
|
player.message = "Wie begint er met delen?"
|
|
player.message = "Wie begint er met delen?"
|
|
|
|
|
|
|
|
-
|
|
|
|
|
def shuffle(self):
|
|
def shuffle(self):
|
|
|
- deck = [x + " " + y for x in suits for y in denoms] + ["Joker" for i in range(3)]
|
|
|
|
|
|
|
+ deck = [x + " " + y for x in suits for y in denoms] + ["Joker" for _ in range(3)]
|
|
|
self.stock = deck * 2
|
|
self.stock = deck * 2
|
|
|
|
|
|
|
|
for n in range(len(self.stock)):
|
|
for n in range(len(self.stock)):
|
|
|
- x = random.randint(0, len(self.stock) - n);
|
|
|
|
|
|
|
+ x = random.randint(0, len(self.stock) - n)
|
|
|
value = self.stock[x]
|
|
value = self.stock[x]
|
|
|
self.stock[x] = self.stock[n]
|
|
self.stock[x] = self.stock[n]
|
|
|
self.stock[n] = value
|
|
self.stock[n] = value
|
|
|
|
|
|
|
|
|
|
+ def deal(self, player_color):
|
|
|
|
|
+ player = self.players[player_color]
|
|
|
|
|
|
|
|
- def deal(self, playerColor):
|
|
|
|
|
- player = self.players[playerColor]
|
|
|
|
|
|
|
+ self.current_player = player
|
|
|
|
|
|
|
|
- self.currentPlayer = player
|
|
|
|
|
-
|
|
|
|
|
- if self.numberOfCards <= 2:
|
|
|
|
|
|
|
+ if self.number_of_cards <= 2:
|
|
|
self.shuffle()
|
|
self.shuffle()
|
|
|
- self.numberOfCards = 6
|
|
|
|
|
|
|
+ self.number_of_cards = 6
|
|
|
else:
|
|
else:
|
|
|
- self.numberOfCards -= 1
|
|
|
|
|
|
|
+ self.number_of_cards -= 1
|
|
|
|
|
|
|
|
for player in self.players.values():
|
|
for player in self.players.values():
|
|
|
- player.hand = self.stock[:self.numberOfCards]
|
|
|
|
|
- self.stock = self.stock[self.numberOfCards:]
|
|
|
|
|
|
|
+ player.hand = self.stock[:self.number_of_cards]
|
|
|
|
|
+ self.stock = self.stock[self.number_of_cards:]
|
|
|
player.options = [Option(Command.CHANGECARD, [card], card) for card in player.hand]
|
|
player.options = [Option(Command.CHANGECARD, [card], card) for card in player.hand]
|
|
|
player.message = "Kies een kaart om te wisselen"
|
|
player.message = "Kies een kaart om te wisselen"
|
|
|
- player.selectedCard = None
|
|
|
|
|
- player.cardIsChanged = False
|
|
|
|
|
|
|
+ player.selected_card = None
|
|
|
|
|
+ player.card_is_changed = False
|
|
|
|
|
|
|
|
return self.players
|
|
return self.players
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- def changeCard(self, playerColor, card):
|
|
|
|
|
- player = self.players[playerColor]
|
|
|
|
|
|
|
+ def change_card(self, player_color, card):
|
|
|
|
|
+ player = self.players[player_color]
|
|
|
|
|
|
|
|
player.hand.remove(card)
|
|
player.hand.remove(card)
|
|
|
mate = self.mate(player)
|
|
mate = self.mate(player)
|
|
|
|
|
|
|
|
- if mate.selectedCard is None:
|
|
|
|
|
- player.selectedCard = card
|
|
|
|
|
|
|
+ if mate.selected_card is None:
|
|
|
|
|
+ player.selected_card = card
|
|
|
player.message = "Wacht op je maat"
|
|
player.message = "Wacht op je maat"
|
|
|
player.options = [Option(Command.UNDOCARD, None, "Terug")]
|
|
player.options = [Option(Command.UNDOCARD, None, "Terug")]
|
|
|
return self.players
|
|
return self.players
|
|
|
|
|
|
|
|
- player.hand.append(mate.selectedCard)
|
|
|
|
|
- mate.selectedCard = None
|
|
|
|
|
|
|
+ player.hand.append(mate.selected_card)
|
|
|
|
|
+ mate.selected_card = None
|
|
|
mate.hand.append(card)
|
|
mate.hand.append(card)
|
|
|
- player.cardIsChanged = True
|
|
|
|
|
- mate.cardIsChanged = True
|
|
|
|
|
|
|
+ player.card_is_changed = True
|
|
|
|
|
+ mate.card_is_changed = True
|
|
|
mate.options.clear()
|
|
mate.options.clear()
|
|
|
player.options.clear()
|
|
player.options.clear()
|
|
|
player.message = ""
|
|
player.message = ""
|
|
|
|
|
|
|
|
- if all(player.cardIsChanged for player in self.players.values()):
|
|
|
|
|
- self.nextTurn()
|
|
|
|
|
|
|
+ if all(player.card_is_changed for player in self.players.values()):
|
|
|
|
|
+ self.next_turn()
|
|
|
else:
|
|
else:
|
|
|
player.message = "Wacht op het andere team"
|
|
player.message = "Wacht op het andere team"
|
|
|
mate.message = "wacht op het andere team"
|
|
mate.message = "wacht op het andere team"
|
|
|
|
|
|
|
|
return self.players
|
|
return self.players
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- def playCard(self, playerColor, card):
|
|
|
|
|
- player = self.players[playerColor]
|
|
|
|
|
|
|
+ def play_card(self, player_color, card):
|
|
|
|
|
+ player = self.players[player_color]
|
|
|
|
|
|
|
|
player.hand.remove(card)
|
|
player.hand.remove(card)
|
|
|
- player.selectedCard = card
|
|
|
|
|
|
|
+ player.selected_card = card
|
|
|
player.options = [Option(Command.UNDOCARD, None, "Terug"), Option(Command.READY, None, "Klaar")]
|
|
player.options = [Option(Command.UNDOCARD, None, "Terug"), Option(Command.READY, None, "Klaar")]
|
|
|
player.message = f"Je speelt {card}"
|
|
player.message = f"Je speelt {card}"
|
|
|
|
|
|
|
@@ -106,24 +108,22 @@ class Game(object):
|
|
|
|
|
|
|
|
return self.players
|
|
return self.players
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- def ready(self, playerColor):
|
|
|
|
|
- player = self.players[playerColor]
|
|
|
|
|
|
|
+ def ready(self, player_color):
|
|
|
|
|
+ player = self.players[player_color]
|
|
|
|
|
|
|
|
player.options.clear()
|
|
player.options.clear()
|
|
|
- player.selectedCard = None
|
|
|
|
|
- self.nextTurn()
|
|
|
|
|
|
|
+ player.selected_card = None
|
|
|
|
|
+ self.next_turn()
|
|
|
|
|
|
|
|
return self.players
|
|
return self.players
|
|
|
|
|
|
|
|
|
|
+ def undo_card(self, player_color):
|
|
|
|
|
+ player = self.players[player_color]
|
|
|
|
|
|
|
|
- def undoCard(self, playerColor):
|
|
|
|
|
- player = self.players[playerColor]
|
|
|
|
|
-
|
|
|
|
|
- player.hand.append(player.selectedCard)
|
|
|
|
|
- player.selectedCard = None
|
|
|
|
|
|
|
+ player.hand.append(player.selected_card)
|
|
|
|
|
+ player.selected_card = None
|
|
|
|
|
|
|
|
- if player.cardIsChanged:
|
|
|
|
|
|
|
+ if player.card_is_changed:
|
|
|
self.turn()
|
|
self.turn()
|
|
|
else:
|
|
else:
|
|
|
player.options = [Option(Command.CHANGECARD, [card], card) for card in player.hand]
|
|
player.options = [Option(Command.CHANGECARD, [card], card) for card in player.hand]
|
|
@@ -131,22 +131,21 @@ class Game(object):
|
|
|
|
|
|
|
|
return self.players
|
|
return self.players
|
|
|
|
|
|
|
|
- def playOption(self, player, option):
|
|
|
|
|
|
|
+ def play_option(self, player, option):
|
|
|
if option.command == Command.DEAL:
|
|
if option.command == Command.DEAL:
|
|
|
return self.deal(player.color)
|
|
return self.deal(player.color)
|
|
|
elif option.command == Command.CHANGECARD:
|
|
elif option.command == Command.CHANGECARD:
|
|
|
- return self.changeCard(player.color, option.args[0])
|
|
|
|
|
|
|
+ return self.change_card(player.color, option.args[0])
|
|
|
elif option.command == Command.PLAYCARD:
|
|
elif option.command == Command.PLAYCARD:
|
|
|
- return self.playCard(player.color, option.args[0])
|
|
|
|
|
|
|
+ return self.play_card(player.color, option.args[0])
|
|
|
elif option.command == Command.READY:
|
|
elif option.command == Command.READY:
|
|
|
return self.ready(player.color)
|
|
return self.ready(player.color)
|
|
|
elif option.command == Command.UNDOCARD:
|
|
elif option.command == Command.UNDOCARD:
|
|
|
- return self.undoCard(player.color)
|
|
|
|
|
|
|
+ return self.undo_card(player.color)
|
|
|
else:
|
|
else:
|
|
|
raise Exception(f"Unknown command {option.command}")
|
|
raise Exception(f"Unknown command {option.command}")
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- def nextPlayer(self, player):
|
|
|
|
|
|
|
+ def next_player(self, player):
|
|
|
if player.color == Color.RED:
|
|
if player.color == Color.RED:
|
|
|
return self.players[Color.BLUE]
|
|
return self.players[Color.BLUE]
|
|
|
elif player.color == Color.BLUE:
|
|
elif player.color == Color.BLUE:
|
|
@@ -158,26 +157,23 @@ class Game(object):
|
|
|
else:
|
|
else:
|
|
|
raise Exception(f"Unknown color {player.Color}")
|
|
raise Exception(f"Unknown color {player.Color}")
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- def nextTurn(self):
|
|
|
|
|
- self.currentPlayer = self.nextPlayer(self.currentPlayer)
|
|
|
|
|
|
|
+ def next_turn(self):
|
|
|
|
|
+ self.current_player = self.next_player(self.current_player)
|
|
|
self.turn()
|
|
self.turn()
|
|
|
|
|
|
|
|
-
|
|
|
|
|
def turn(self):
|
|
def turn(self):
|
|
|
- if len(self.currentPlayer.hand) > 0:
|
|
|
|
|
- self.currentPlayer.options = [Option(Command.PLAYCARD, [card], card) for card in self.currentPlayer.hand]
|
|
|
|
|
- self.currentPlayer.message = "Kies een kaart om te spelen"
|
|
|
|
|
- otherMessage = ""
|
|
|
|
|
|
|
+ if len(self.current_player.hand) > 0:
|
|
|
|
|
+ self.current_player.options = [Option(Command.PLAYCARD, [card], card) for card in self.current_player.hand]
|
|
|
|
|
+ self.current_player.message = "Kies een kaart om te spelen"
|
|
|
|
|
+ other_message = ""
|
|
|
else:
|
|
else:
|
|
|
- self.currentPlayer.options = [Option(Command.DEAL, None, "Delen")]
|
|
|
|
|
- self.currentPlayer.message = f"Jij bent aan de beurt om te delen."
|
|
|
|
|
- otherMessage = " om te delen"
|
|
|
|
|
|
|
+ self.current_player.options = [Option(Command.DEAL, None, "Delen")]
|
|
|
|
|
+ self.current_player.message = f"Jij bent aan de beurt om te delen."
|
|
|
|
|
+ other_message = " om te delen"
|
|
|
|
|
|
|
|
for player in self.players.values():
|
|
for player in self.players.values():
|
|
|
- if player.color != self.currentPlayer.color:
|
|
|
|
|
- player.message = f"{self.currentPlayer.name} is aan de beurt" + otherMessage
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if player.color != self.current_player.color:
|
|
|
|
|
+ player.message = f"{self.current_player.name} is aan de beurt" + other_message
|
|
|
|
|
|
|
|
def mate(self, player):
|
|
def mate(self, player):
|
|
|
if player.color == Color.RED:
|
|
if player.color == Color.RED:
|
|
@@ -195,13 +191,13 @@ class Game(object):
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|
|
|
game = Game()
|
|
game = Game()
|
|
|
|
|
|
|
|
- #game.deal(Color.BLUE)
|
|
|
|
|
- #game.changeCard(Color.YELLOW, game.players[Color.YELLOW].hand[4])
|
|
|
|
|
- #game.changeCard(Color.RED, game.players[Color.RED].hand[2])
|
|
|
|
|
- #game.changeCard(Color.GREEN, game.players[Color.GREEN].hand[0])
|
|
|
|
|
- #game.changeCard(Color.BLUE, game.players[Color.BLUE].hand[5])
|
|
|
|
|
- #game.playCard(Color.YELLOW, game.players[Color.YELLOW].hand[1])
|
|
|
|
|
- #game.undoCard(Color.YELLOW)
|
|
|
|
|
|
|
+ # game.deal(Color.BLUE)
|
|
|
|
|
+ # game.changeCard(Color.YELLOW, game.players[Color.YELLOW].hand[4])
|
|
|
|
|
+ # game.changeCard(Color.RED, game.players[Color.RED].hand[2])
|
|
|
|
|
+ # game.changeCard(Color.GREEN, game.players[Color.GREEN].hand[0])
|
|
|
|
|
+ # game.changeCard(Color.BLUE, game.players[Color.BLUE].hand[5])
|
|
|
|
|
+ # game.playCard(Color.YELLOW, game.players[Color.YELLOW].hand[1])
|
|
|
|
|
+ # game.undoCard(Color.YELLOW)
|
|
|
|
|
|
|
|
colors = [Color.BLUE, Color.YELLOW, Color.GREEN, Color.RED]
|
|
colors = [Color.BLUE, Color.YELLOW, Color.GREEN, Color.RED]
|
|
|
|
|
|
|
@@ -210,19 +206,19 @@ if __name__ == "__main__":
|
|
|
color = colors[0]
|
|
color = colors[0]
|
|
|
print(color)
|
|
print(color)
|
|
|
game.deal(color)
|
|
game.deal(color)
|
|
|
- game.changeCard(Color.YELLOW, game.players[Color.YELLOW].hand[deal - 1])
|
|
|
|
|
- game.changeCard(Color.RED, game.players[Color.RED].hand[1])
|
|
|
|
|
- game.changeCard(Color.GREEN, game.players[Color.GREEN].hand[0])
|
|
|
|
|
- game.changeCard(Color.BLUE, game.players[Color.BLUE].hand[-1])
|
|
|
|
|
|
|
+ game.change_card(Color.YELLOW, game.players[Color.YELLOW].hand[deal - 1])
|
|
|
|
|
+ game.change_card(Color.RED, game.players[Color.RED].hand[1])
|
|
|
|
|
+ game.change_card(Color.GREEN, game.players[Color.GREEN].hand[0])
|
|
|
|
|
+ game.change_card(Color.BLUE, game.players[Color.BLUE].hand[-1])
|
|
|
|
|
|
|
|
colors = colors[1:] + [colors[0]]
|
|
colors = colors[1:] + [colors[0]]
|
|
|
|
|
|
|
|
for turn in range(deal):
|
|
for turn in range(deal):
|
|
|
for color in colors:
|
|
for color in colors:
|
|
|
- game.playCard(color, game.players[color].hand[0])
|
|
|
|
|
|
|
+ game.play_card(color, game.players[color].hand[0])
|
|
|
game.ready(color)
|
|
game.ready(color)
|
|
|
|
|
|
|
|
for player in game.players.values():
|
|
for player in game.players.values():
|
|
|
print(player.__dict__, end="\n\n")
|
|
print(player.__dict__, end="\n\n")
|
|
|
|
|
|
|
|
- print(game.stock, end="\n\n")
|
|
|
|
|
|
|
+ print(game.stock, end="\n\n")
|