working game?

This commit is contained in:
Jake Pullen
2024-06-08 09:26:44 +01:00
parent 493c38892b
commit 5bbda755af
6 changed files with 75 additions and 34 deletions
+1 -1
View File
@@ -4,5 +4,5 @@ class card:
self.value = value
def __str__(self):
return f"{self.suit} of {self.value}"
return f"{self.value} of {self.suit}"
+1 -1
View File
@@ -11,7 +11,7 @@ class deck:
def build(self):
for suit in ["Spades", "Hearts", "Diamonds", "Clubs"]:
for value in range(1, 14):
self.cards.append( card(value, suit) )
self.cards.append( card(suit=suit, value=value) )
def show(self):
for card in self.cards:
+32 -11
View File
@@ -1,7 +1,7 @@
from deck import deck
#from card import card
from player import player
from player import BotPlayer, HumanPlayer
from rules import Rule, RuleEngine
import time
@@ -11,13 +11,24 @@ d.shuffle()
#d.show()
d.first_card()
people = ['Jake','bot1','bot2','bot3','bot4','bot5']
player_count = int(input('How many players do you want to play with? '))
players = []
for i in range(player_count):
player_name = input(f'Enter the name of player {i+1}: ')
players.append(HumanPlayer(player_name))
bot_count = int(input('How many bots do you want to play against? '))
bots = []
for i in range(bot_count):
bots.append(f'bot{i+1}')
players = []
# In your main game loop
players = [HumanPlayer('Jake')] # Add other players
# Create player objects
for person in people:
game_player = player(person)
for person in bots:
game_player = BotPlayer(person)
players.append(game_player)
# Now you can use your player objects
@@ -38,24 +49,34 @@ round_counter = 0
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)
has_played,given_command = game_player.play(d) # play a card
for game_player in players:
if isinstance(game_player, HumanPlayer):
has_played, given_command = game_player.play(d)
else:
has_played, given_command = game_player.play(d) # Bot player's turn
if has_played: # if the player has played a card
command = rule_engine.check_rule(d.discard_pile[-1], game_player.hand)
print(f'expected command: {command}')
time.sleep(1)
if given_command != command:
print(f'{game_player.name} made the wrong announcement\n')
print(f'{game_player.name} made the wrong announcement')
#need to put the card back in the hand
game_player.hand.append(d.discard_pile.pop())
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')
print(f'{game_player.name} announced the correct rule')
print(f'{game_player.name} has {game_player.check_hand()} cards left\n')
time.sleep(1)
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
print(f'{game_player.name} drew a card')
print(f'{game_player.name} has {game_player.check_hand()} cards left\n')
time.sleep(1)
if d.check_empty(): # if the deck is empty
d.shuffle()
d.first_card()
+30 -10
View File
@@ -2,7 +2,7 @@ import json
import random
error_chance = 0.2
error_chance = 0.3
class player:
def __init__(self, name):
@@ -20,31 +20,36 @@ class player:
def discard(self):
return self.hand.pop()
def command(self, suit, value, card_count):
def check_hand(self):
return len(self.hand)
class BotPlayer(player):
def __init__(self, name):
super().__init__(name)
def command(self, card):
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 {len(self.hand)} cards, {suit} and {value}')
#print(f'generating command for {len(self.hand)} cards, {card.suit} and {card.value}')
for rule in rules:
if 'suit' in rule and rule['suit'] == suit:
if 'suit' in rule and rule['suit'] == card.suit:
commands.append(rule['command'])
if 'value' in rule and rule['value'] == value:
if 'value' in rule and rule['value'] == card.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))
deck.discard_pile.append(player_card)
print(f'{self.name} played {player_card}')
commands = self.command(card.suit, card.value, len(self.hand))
commands = self.command(player_card)
if commands is not None:
print(f'{self.name} announced: {commands}')
return True, commands
@@ -52,5 +57,20 @@ class player:
print(f'{self.name} cannot play')
return False, None
def check_hand(self):
return len(self.hand)
class HumanPlayer(player):
def __init__(self, name):
super().__init__(name)
def play(self, deck):
print(f'Top Card: {deck.discard_pile[-1]}')
print(f'Your hand:')
for i, card in enumerate(self.hand):
print(f'{i}: {card}')
card_index = input('Enter the index of the card you want to play(or type N to pass your go): ')
if card_index == 'N' or card_index == 'n':
return False, None
card = self.hand.pop(int(card_index))
deck.discard_pile.append(card)
command = input('Enter your command: ')
return True, command
+10 -10
View File
@@ -1,12 +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": 13, "command": "god save the king "},
{"card_count": 2, "command": "swish "},
{"card_count": 1, "command": "flick "},
{"card_count": 0, "command": "mau "}
{"suit": "Hearts", "command": "Nah"},
{"suit": "Spades", "command":"Pass me the shovel"},
{"value": 1, "command": "Klondike"},
{"value": 7, "command": "Good Morning"},
{"value": 8, "command": "bang"},
{"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 -1
View File
@@ -31,5 +31,5 @@ class RuleEngine:
(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
return ' and '.join(commands) if commands else ''