added dynamic rule checking
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -6,16 +6,17 @@ from rules import Rule, RuleEngine
|
|||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
d = deck()
|
deck = deck()
|
||||||
d.shuffle()
|
deck.shuffle()
|
||||||
#d.show()
|
#deck.show()
|
||||||
d.first_card()
|
deck.first_card()
|
||||||
|
|
||||||
player_count = int(input('How many players do you want to play with? '))
|
player_count = int(input('How many players do you want to play with? '))
|
||||||
players = []
|
players = []
|
||||||
for i in range(player_count):
|
for i in range(player_count):
|
||||||
player_name = input(f'Enter the name of player {i+1}: ')
|
player_name = input(f'Enter the name of player {i+1}: ')
|
||||||
players.append(HumanPlayer(player_name))
|
players.append(HumanPlayer(player_name))
|
||||||
|
print(f'player {player_name} has been added')
|
||||||
|
|
||||||
bot_count = int(input('How many bots do you want to play against? '))
|
bot_count = int(input('How many bots do you want to play against? '))
|
||||||
bots = []
|
bots = []
|
||||||
@@ -34,9 +35,9 @@ for person in bots:
|
|||||||
# Now you can use your player objects
|
# Now you can use your player objects
|
||||||
for game_player in players:
|
for game_player in players:
|
||||||
print(f'dealing 3 cards to {game_player.name}')
|
print(f'dealing 3 cards to {game_player.name}')
|
||||||
game_player.draw(d)
|
game_player.draw(deck)
|
||||||
game_player.draw(d)
|
game_player.draw(deck)
|
||||||
game_player.draw(d)
|
game_player.draw(deck)
|
||||||
|
|
||||||
# In your main game loop
|
# In your main game loop
|
||||||
rule_engine = RuleEngine()
|
rule_engine = RuleEngine()
|
||||||
@@ -51,20 +52,28 @@ for i in range(50):
|
|||||||
print(f'round: {round_counter}')
|
print(f'round: {round_counter}')
|
||||||
for game_player in players:
|
for game_player in players:
|
||||||
if isinstance(game_player, HumanPlayer):
|
if isinstance(game_player, HumanPlayer):
|
||||||
has_played, given_command = game_player.play(d)
|
has_played, given_command = game_player.play(deck)
|
||||||
else:
|
else:
|
||||||
has_played, given_command = game_player.play(d) # Bot player's turn
|
has_played, given_command = game_player.play(deck) # Bot player's turn
|
||||||
|
|
||||||
if has_played: # if the player has played a card
|
if has_played: # if the player has played a card
|
||||||
command = rule_engine.check_rule(d.discard_pile[-1], game_player.hand)
|
if deck.discard_pile[-2].suit != deck.discard_pile[-1].suit and deck.discard_pile[-2].value != deck.discard_pile[-1].value:
|
||||||
print(f'expected command: {command}')
|
print(f'player {game_player.name} has played a wrong card')
|
||||||
|
game_player.hand.append(deck.discard_pile.pop())
|
||||||
|
game_player.draw(deck)
|
||||||
|
continue
|
||||||
|
command = rule_engine.check_rule(deck.discard_pile[-1], game_player.hand)
|
||||||
|
print(f'{game_player.name} announced: {given_command}')
|
||||||
|
expected_command = ' and '.join(command)
|
||||||
|
print(f'expected command: {expected_command}')
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
if given_command != command:
|
given_command_set = set(given_command.split(' and '))
|
||||||
|
if given_command_set != command:
|
||||||
print(f'{game_player.name} made the wrong announcement')
|
print(f'{game_player.name} made the wrong announcement')
|
||||||
#need to put the card back in the hand
|
#need to put the card back in the hand
|
||||||
game_player.hand.append(d.discard_pile.pop())
|
game_player.hand.append(deck.discard_pile.pop())
|
||||||
game_player.draw(d)
|
game_player.draw(deck)
|
||||||
if given_command == command:
|
if given_command_set == command:
|
||||||
print(f'{game_player.name} announced the correct rule')
|
print(f'{game_player.name} announced the correct rule')
|
||||||
print(f'{game_player.name} has {game_player.check_hand()} cards left\n')
|
print(f'{game_player.name} has {game_player.check_hand()} cards left\n')
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
@@ -73,15 +82,15 @@ for i in range(50):
|
|||||||
print(f'rounds played: {round_counter}')
|
print(f'rounds played: {round_counter}')
|
||||||
exit()
|
exit()
|
||||||
else:
|
else:
|
||||||
game_player.draw(d) # if the player has not played a card, draw a card
|
game_player.draw(deck) # if the player has not played a card, draw a card
|
||||||
print(f'{game_player.name} drew a card')
|
print(f'{game_player.name} drew a card')
|
||||||
print(f'{game_player.name} has {game_player.check_hand()} cards left\n')
|
print(f'{game_player.name} has {game_player.check_hand()} cards left\n')
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
if d.check_empty(): # if the deck is empty
|
if deck.check_empty(): # if the deck is empty
|
||||||
d.shuffle()
|
deck.shuffle()
|
||||||
d.first_card()
|
deck.first_card()
|
||||||
#game_player.draw(d)
|
#game_player.draw(deck)
|
||||||
|
|
||||||
print(f'discard pile: {d.discard_pile[-1]}')
|
print(f'discard pile: {deck.discard_pile[-1]}')
|
||||||
print(f'cards in deck: {len(d.cards)}\n')
|
print(f'cards in deck: {len(deck.cards)}\n')
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ class BotPlayer(player):
|
|||||||
if 'card_count' in rule and rule['card_count'] == len(self.hand):
|
if 'card_count' in rule and rule['card_count'] == len(self.hand):
|
||||||
commands.append(rule['command'])
|
commands.append(rule['command'])
|
||||||
#print(f'Commands: {commands}')
|
#print(f'Commands: {commands}')
|
||||||
return ' and '.join(commands) if commands else None
|
return ' and '.join(commands) if commands else ''
|
||||||
|
|
||||||
def play(self, deck):
|
def play(self, deck):
|
||||||
for card in self.hand:
|
for card in self.hand:
|
||||||
if card.suit == deck.discard_pile[-1].suit or card.value == deck.discard_pile[-1].value:
|
if card.suit == deck.discard_pile[-1].suit or card.value == deck.discard_pile[-1].value:
|
||||||
@@ -50,8 +51,8 @@ class BotPlayer(player):
|
|||||||
deck.discard_pile.append(player_card)
|
deck.discard_pile.append(player_card)
|
||||||
print(f'{self.name} played {player_card}')
|
print(f'{self.name} played {player_card}')
|
||||||
commands = self.command(player_card)
|
commands = self.command(player_card)
|
||||||
if commands is not None:
|
#if commands is not None:
|
||||||
print(f'{self.name} announced: {commands}')
|
#print(f'{self.name} announced: {commands}')
|
||||||
return True, commands
|
return True, commands
|
||||||
else:
|
else:
|
||||||
print(f'{self.name} cannot play')
|
print(f'{self.name} cannot play')
|
||||||
@@ -67,10 +68,10 @@ class HumanPlayer(player):
|
|||||||
print(f'Your hand:')
|
print(f'Your hand:')
|
||||||
for i, card in enumerate(self.hand):
|
for i, card in enumerate(self.hand):
|
||||||
print(f'{i}: {card}')
|
print(f'{i}: {card}')
|
||||||
card_index = input('Enter the index of the card you want to play(or type N to pass your go): ')
|
card_index = input('Enter the index of the card you want to play(or type N to pass your go): ').lower()
|
||||||
if card_index == 'N' or card_index == 'n':
|
if card_index == 'n':
|
||||||
return False, None
|
return False, None
|
||||||
card = self.hand.pop(int(card_index))
|
card = self.hand.pop(int(card_index))
|
||||||
deck.discard_pile.append(card)
|
deck.discard_pile.append(card)
|
||||||
command = input('Enter your command: ')
|
command = input('Enter your command: ').lower()
|
||||||
return True, command
|
return True, command
|
||||||
|
|||||||
+4
-4
@@ -1,8 +1,8 @@
|
|||||||
[
|
[
|
||||||
{"suit": "Hearts", "command": "Nah"},
|
{"suit": "Hearts", "command": "nah"},
|
||||||
{"suit": "Spades", "command":"Pass me the shovel"},
|
{"suit": "Spades", "command":"pass me the shovel"},
|
||||||
{"value": 1, "command": "Klondike"},
|
{"value": 1, "command": "klondike"},
|
||||||
{"value": 7, "command": "Good Morning"},
|
{"value": 7, "command": "good morning"},
|
||||||
{"value": 8, "command": "bang"},
|
{"value": 8, "command": "bang"},
|
||||||
{"value": 11, "command": "punch"},
|
{"value": 11, "command": "punch"},
|
||||||
{"value": 13, "command": "god save the king"},
|
{"value": 13, "command": "god save the king"},
|
||||||
|
|||||||
@@ -25,11 +25,11 @@ class RuleEngine:
|
|||||||
print(f'Added rule: {rule_json}')
|
print(f'Added rule: {rule_json}')
|
||||||
|
|
||||||
def check_rule(self, card, hand):
|
def check_rule(self, card, hand):
|
||||||
commands = []
|
commands = set()
|
||||||
for game_rule in self.rules:
|
for game_rule in self.rules:
|
||||||
if (game_rule.suit is not None and game_rule.suit == card.suit) or \
|
if (game_rule.suit is not None and game_rule.suit == card.suit) or \
|
||||||
(game_rule.value is not None and game_rule.value == card.value) or \
|
(game_rule.value is not None and game_rule.value == card.value) or \
|
||||||
(game_rule.card_count != 99 and game_rule.card_count == len(hand)):
|
(game_rule.card_count != 99 and game_rule.card_count == len(hand)):
|
||||||
commands.append(game_rule.command)
|
commands.add(game_rule.command)
|
||||||
return ' and '.join(commands) if commands else ''
|
return commands
|
||||||
|
|
||||||
Reference in New Issue
Block a user