initial commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
class card:
|
||||
def __init__(self, suit, value):
|
||||
self.suit = suit
|
||||
self.value = value
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.suit} of {self.value}"
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
from card import card
|
||||
|
||||
import random
|
||||
|
||||
class deck:
|
||||
def __init__(self):
|
||||
self.cards = []
|
||||
self.build()
|
||||
self.discard_pile = []
|
||||
|
||||
def build(self):
|
||||
for suit in ["Spades", "Hearts", "Diamonds", "Clubs"]:
|
||||
for value in range(1, 14):
|
||||
self.cards.append( card(value, suit) )
|
||||
|
||||
def show(self):
|
||||
for card in self.cards:
|
||||
print(card)
|
||||
|
||||
def shuffle(self):
|
||||
if len(self.cards) > 0:
|
||||
print('grabbing the remaining cards from the deck')
|
||||
self.discard_pile.extend(self.cards)
|
||||
print('shuffling the deck')
|
||||
self.cards = self.discard_pile
|
||||
random.shuffle(self.cards)
|
||||
self.discard_pile = []
|
||||
return self.cards
|
||||
|
||||
def check_empty(self):
|
||||
if len(self.cards) == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def first_card(self):
|
||||
# adds the top deck card to the top of the discard pile
|
||||
self.discard_pile.append(self.cards.pop())
|
||||
return self
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
|
||||
from deck import deck
|
||||
#from card import card
|
||||
from player import player
|
||||
from rules import Rule, RuleEngine
|
||||
|
||||
import time
|
||||
|
||||
d = deck()
|
||||
d.shuffle()
|
||||
#d.show()
|
||||
d.first_card()
|
||||
|
||||
|
||||
people = ['Jake','bot1','bot2','bot3','bot4','bot5']
|
||||
players = []
|
||||
|
||||
# Create player objects
|
||||
for person in people:
|
||||
game_player = player(person)
|
||||
players.append(game_player)
|
||||
|
||||
# Now you can use your player objects
|
||||
for game_player in players:
|
||||
print(f'dealing 3 cards to {game_player.name}')
|
||||
game_player.draw(d)
|
||||
game_player.draw(d)
|
||||
game_player.draw(d)
|
||||
|
||||
# In your main game loop
|
||||
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()
|
||||
|
||||
#while True: # main game loop
|
||||
for i in range(50):
|
||||
for game_player in players: # loop through each player
|
||||
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])
|
||||
if given_command != command:
|
||||
print(f'{game_player.name} announced the wrong rule\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')
|
||||
exit()
|
||||
else:
|
||||
game_player.draw(d) # if the player has not played a card, draw a card
|
||||
print(f'{game_player.name} has {game_player.check_hand()} cards left\n')
|
||||
if d.check_empty(): # if the deck is empty
|
||||
d.shuffle()
|
||||
d.first_card()
|
||||
#game_player.draw(d)
|
||||
|
||||
print(f'discard pile: {d.discard_pile[-1]}')
|
||||
print(f'cards in deck: {len(d.cards)}\n')
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import json
|
||||
import random
|
||||
from deck import deck
|
||||
#from rules import Rule, RuleEngine
|
||||
|
||||
|
||||
class player:
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.hand = []
|
||||
|
||||
def draw(self, deck):
|
||||
self.hand.append(deck.cards.pop())
|
||||
return self
|
||||
|
||||
def show_hand(self):
|
||||
for card in self.hand:
|
||||
print(card)
|
||||
|
||||
def discard(self):
|
||||
return self.hand.pop()
|
||||
|
||||
def command(self, card_played):
|
||||
rules = json.load(open('rules.json'))
|
||||
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
|
||||
|
||||
|
||||
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
|
||||
else:
|
||||
print(f'{self.name} cannot play')
|
||||
return False, None
|
||||
|
||||
def check_hand(self):
|
||||
return len(self.hand)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
[
|
||||
{"suit": "Hearts", "command": "Nah"},
|
||||
{"suit":"Spades", "command":"of Spades"},
|
||||
{"value": 8, "command": "bang"},
|
||||
{"value":11, "command": "punch"}
|
||||
]
|
||||
@@ -0,0 +1,42 @@
|
||||
import json
|
||||
|
||||
class Rule:
|
||||
def __init__(self, suit=None, value=None, command=None):
|
||||
self.suit = suit
|
||||
self.value = value
|
||||
self.command = command
|
||||
self.count = 1
|
||||
|
||||
class RuleEngine:
|
||||
def __init__(self):
|
||||
self.rules = []
|
||||
self.last_value = None
|
||||
|
||||
def add_rule(self, rule):
|
||||
self.rules.append(rule)
|
||||
|
||||
def load_rules_from_json(self, json_file):
|
||||
with open(json_file) as f:
|
||||
rules_json = json.load(f)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user