adding and checking rules
This commit is contained in:
@@ -32,28 +32,26 @@ rule_engine = RuleEngine()
|
||||
# Load rules from JSON file
|
||||
rule_engine.load_rules_from_json('rules.json')
|
||||
|
||||
#exit()
|
||||
# # After a card is played
|
||||
# card = game_player.play(d)
|
||||
# command = rule_engine.apply_rules(card)
|
||||
# if command and player.command != command:
|
||||
# game_player.pick_up_card()
|
||||
|
||||
round_counter = 0
|
||||
#while True: # main game loop
|
||||
for i in range(50):
|
||||
round_counter += 1
|
||||
print(f'round: {round_counter}')
|
||||
for game_player in players: # loop through each player
|
||||
time.sleep(2)
|
||||
#time.sleep(2)
|
||||
has_played,given_command = game_player.play(d) # play a card
|
||||
if has_played: # if the player has played a card
|
||||
command = rule_engine.check_rule(d.discard_pile[-1])
|
||||
command = rule_engine.check_rule(d.discard_pile[-1], game_player.hand)
|
||||
if given_command != command:
|
||||
print(f'{game_player.name} announced the wrong rule\n')
|
||||
print(f'{game_player.name} made the wrong announcement\n')
|
||||
game_player.draw(d)
|
||||
if given_command == command:
|
||||
print(f'{game_player.name} announced the correct rule\n')
|
||||
print(f'Player {game_player.name} has {game_player.check_hand()} cards left\n')
|
||||
if game_player.check_hand() == 0: # if the player has no cards left
|
||||
print(f'{game_player.name} has won')
|
||||
print(f'rounds played: {round_counter}')
|
||||
exit()
|
||||
else:
|
||||
game_player.draw(d) # if the player has not played a card, draw a card
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import json
|
||||
import random
|
||||
from deck import deck
|
||||
#from rules import Rule, RuleEngine
|
||||
|
||||
|
||||
error_chance = 0.2
|
||||
|
||||
class player:
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
@@ -20,36 +20,37 @@ class player:
|
||||
def discard(self):
|
||||
return self.hand.pop()
|
||||
|
||||
def command(self, card_played):
|
||||
def command(self, suit, value, card_count):
|
||||
rules = json.load(open('rules.json'))
|
||||
commands = []
|
||||
#chance to return the wrong command
|
||||
if random.random() < error_chance:
|
||||
commands.append('wrong command')
|
||||
print(f'generating command for {card_count} cards, {suit} and {value}')
|
||||
for rule in rules:
|
||||
if ('suit' in rule and rule['suit'] == card_played.suit) or ('value' in rule and rule['value'] == card_played.value):
|
||||
# 10% chance to return the wrong command
|
||||
if random.random() < 0.1:
|
||||
return 'wrong command'
|
||||
else:
|
||||
return rule['command']
|
||||
else:
|
||||
if random.random() < 0.1:
|
||||
return 'wrong command'
|
||||
else:
|
||||
return None
|
||||
if 'suit' in rule and rule['suit'] == suit:
|
||||
commands.append(rule['command'])
|
||||
if 'value' in rule and rule['value'] == value:
|
||||
commands.append(rule['command'])
|
||||
if 'card_count' in rule and rule['card_count'] == len(self.hand):
|
||||
commands.append(rule['command'])
|
||||
#print(f'Commands: {commands}')
|
||||
return ' and '.join(commands) if commands else None
|
||||
|
||||
|
||||
def play(self, deck):
|
||||
for card in self.hand:
|
||||
if card.suit == deck.discard_pile[-1].suit or card.value == deck.discard_pile[-1].value:
|
||||
player_card = self.hand.pop(self.hand.index(card))
|
||||
command = self.command(player_card)
|
||||
deck.discard_pile.append(player_card)
|
||||
print(f'{self.name} played {player_card}')
|
||||
if command != None:
|
||||
print(f'Command is {command}\n')
|
||||
return True, command
|
||||
commands = self.command(card.suit, card.value, len(self.hand))
|
||||
if commands is not None:
|
||||
print(f'{self.name} announced: {commands}')
|
||||
return True, commands
|
||||
else:
|
||||
print(f'{self.name} cannot play')
|
||||
return False, None
|
||||
|
||||
def check_hand(self):
|
||||
return len(self.hand)
|
||||
|
||||
+7
-1
@@ -1,6 +1,12 @@
|
||||
[
|
||||
{"suit": "Hearts", "command": "Nah "},
|
||||
{"suit": "Spades", "command":"of Spades "},
|
||||
{"value": 1, "command": "Klondike "},
|
||||
{"value": 7, "command": "Good Morning "},
|
||||
{"value": 8, "command": "bang "},
|
||||
{"value":11, "command": "punch"}
|
||||
{"value": 11, "command": "punch "},
|
||||
{"value": 13, "command": "god save the king "},
|
||||
{"card_count": 2, "command": "swish "},
|
||||
{"card_count": 1, "command": "flick "},
|
||||
{"card_count": 0, "command": "mau "}
|
||||
]
|
||||
@@ -1,16 +1,18 @@
|
||||
import json
|
||||
|
||||
class Rule:
|
||||
def __init__(self, suit=None, value=None, command=None):
|
||||
def __init__(self, suit=None, value=None, command=None, card_count=99):
|
||||
self.suit = suit
|
||||
self.value = value
|
||||
self.command = command
|
||||
self.card_count = card_count
|
||||
self.count = 1
|
||||
|
||||
class RuleEngine:
|
||||
def __init__(self):
|
||||
self.rules = []
|
||||
self.last_value = None
|
||||
self.card_count = 99
|
||||
|
||||
def add_rule(self, rule):
|
||||
self.rules.append(rule)
|
||||
@@ -22,21 +24,12 @@ class RuleEngine:
|
||||
self.add_rule(Rule(**rule_json))
|
||||
print(f'Added rule: {rule_json}')
|
||||
|
||||
def apply_rules(self, card):
|
||||
for rule in self.rules:
|
||||
if rule.suit == card.suit or rule.value == card.value:
|
||||
if self.last_value == card.value:
|
||||
rule.count += 1
|
||||
else:
|
||||
rule.count = 1
|
||||
self.last_value = card.value
|
||||
return rule.command * rule.count
|
||||
self.last_value = card.value
|
||||
return None
|
||||
|
||||
def check_rule(self, card):
|
||||
for rule in self.rules:
|
||||
if rule.suit == card.suit or rule.value == card.value:
|
||||
return rule.command
|
||||
return None
|
||||
def check_rule(self, card, hand):
|
||||
commands = []
|
||||
for game_rule in self.rules:
|
||||
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.card_count != 99 and game_rule.card_count == len(hand)):
|
||||
commands.append(game_rule.command)
|
||||
return ' and '.join(commands) if commands else None
|
||||
|
||||
Reference in New Issue
Block a user