adding and checking rules

This commit is contained in:
Jake Pullen
2024-05-12 21:47:25 +01:00
parent 5be036817b
commit 110fb4fc1b
4 changed files with 50 additions and 52 deletions
+11 -18
View File
@@ -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)
@@ -21,22 +23,13 @@ class RuleEngine:
for rule_json in rules_json:
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