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