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
+21 -20
View File
@@ -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
@@ -19,37 +19,38 @@ 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)