working game?
This commit is contained in:
@@ -4,5 +4,5 @@ class card:
|
|||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.suit} of {self.value}"
|
return f"{self.value} of {self.suit}"
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class deck:
|
|||||||
def build(self):
|
def build(self):
|
||||||
for suit in ["Spades", "Hearts", "Diamonds", "Clubs"]:
|
for suit in ["Spades", "Hearts", "Diamonds", "Clubs"]:
|
||||||
for value in range(1, 14):
|
for value in range(1, 14):
|
||||||
self.cards.append( card(value, suit) )
|
self.cards.append( card(suit=suit, value=value) )
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
for card in self.cards:
|
for card in self.cards:
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
from deck import deck
|
from deck import deck
|
||||||
#from card import card
|
#from card import card
|
||||||
from player import player
|
from player import BotPlayer, HumanPlayer
|
||||||
from rules import Rule, RuleEngine
|
from rules import Rule, RuleEngine
|
||||||
|
|
||||||
import time
|
import time
|
||||||
@@ -11,13 +11,24 @@ d.shuffle()
|
|||||||
#d.show()
|
#d.show()
|
||||||
d.first_card()
|
d.first_card()
|
||||||
|
|
||||||
|
player_count = int(input('How many players do you want to play with? '))
|
||||||
people = ['Jake','bot1','bot2','bot3','bot4','bot5']
|
|
||||||
players = []
|
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
|
# Create player objects
|
||||||
for person in people:
|
for person in bots:
|
||||||
game_player = player(person)
|
game_player = BotPlayer(person)
|
||||||
players.append(game_player)
|
players.append(game_player)
|
||||||
|
|
||||||
# Now you can use your player objects
|
# Now you can use your player objects
|
||||||
@@ -38,24 +49,34 @@ round_counter = 0
|
|||||||
for i in range(50):
|
for i in range(50):
|
||||||
round_counter += 1
|
round_counter += 1
|
||||||
print(f'round: {round_counter}')
|
print(f'round: {round_counter}')
|
||||||
for game_player in players: # loop through each player
|
for game_player in players:
|
||||||
#time.sleep(2)
|
if isinstance(game_player, HumanPlayer):
|
||||||
has_played,given_command = game_player.play(d) # play a card
|
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
|
if has_played: # if the player has played a card
|
||||||
command = rule_engine.check_rule(d.discard_pile[-1], game_player.hand)
|
command = rule_engine.check_rule(d.discard_pile[-1], game_player.hand)
|
||||||
|
print(f'expected command: {command}')
|
||||||
|
time.sleep(1)
|
||||||
if given_command != command:
|
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)
|
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')
|
||||||
print(f'Player {game_player.name} has {game_player.check_hand()} cards left\n')
|
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
|
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}')
|
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
|
||||||
|
print(f'{game_player.name} drew a card')
|
||||||
print(f'{game_player.name} has {game_player.check_hand()} cards left\n')
|
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
|
if d.check_empty(): # if the deck is empty
|
||||||
d.shuffle()
|
d.shuffle()
|
||||||
d.first_card()
|
d.first_card()
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import json
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
error_chance = 0.2
|
error_chance = 0.3
|
||||||
|
|
||||||
class player:
|
class player:
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
@@ -20,31 +20,36 @@ class player:
|
|||||||
def discard(self):
|
def discard(self):
|
||||||
return self.hand.pop()
|
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'))
|
rules = json.load(open('rules.json'))
|
||||||
commands = []
|
commands = []
|
||||||
#chance to return the wrong command
|
#chance to return the wrong command
|
||||||
if random.random() < error_chance:
|
if random.random() < error_chance:
|
||||||
commands.append('wrong command')
|
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:
|
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'])
|
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'])
|
commands.append(rule['command'])
|
||||||
if 'card_count' in rule and rule['card_count'] == len(self.hand):
|
if 'card_count' in rule and rule['card_count'] == len(self.hand):
|
||||||
commands.append(rule['command'])
|
commands.append(rule['command'])
|
||||||
#print(f'Commands: {commands}')
|
#print(f'Commands: {commands}')
|
||||||
return ' and '.join(commands) if commands else None
|
return ' and '.join(commands) if commands else 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))
|
||||||
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}')
|
||||||
commands = self.command(card.suit, card.value, len(self.hand))
|
commands = self.command(player_card)
|
||||||
if commands is not None:
|
if commands is not None:
|
||||||
print(f'{self.name} announced: {commands}')
|
print(f'{self.name} announced: {commands}')
|
||||||
return True, commands
|
return True, commands
|
||||||
@@ -52,5 +57,20 @@ class player:
|
|||||||
print(f'{self.name} cannot play')
|
print(f'{self.name} cannot play')
|
||||||
return False, None
|
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
@@ -1,12 +1,12 @@
|
|||||||
[
|
[
|
||||||
{"suit": "Hearts", "command": "Nah "},
|
{"suit": "Hearts", "command": "Nah"},
|
||||||
{"suit": "Spades", "command":"of Spades "},
|
{"suit": "Spades", "command":"Pass me the shovel"},
|
||||||
{"value": 1, "command": "Klondike "},
|
{"value": 1, "command": "Klondike"},
|
||||||
{"value": 7, "command": "Good Morning "},
|
{"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 "},
|
{"value": 13, "command": "god save the king"},
|
||||||
{"card_count": 2, "command": "swish "},
|
{"card_count": 2, "command": "swish"},
|
||||||
{"card_count": 1, "command": "flick "},
|
{"card_count": 1, "command": "flick"},
|
||||||
{"card_count": 0, "command": "mau "}
|
{"card_count": 0, "command": "mau"}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -31,5 +31,5 @@ class RuleEngine:
|
|||||||
(game_rule.value is not None and game_rule.value == card.value) 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)):
|
(game_rule.card_count != 99 and game_rule.card_count == len(hand)):
|
||||||
commands.append(game_rule.command)
|
commands.append(game_rule.command)
|
||||||
return ' and '.join(commands) if commands else None
|
return ' and '.join(commands) if commands else ''
|
||||||
|
|
||||||
Reference in New Issue
Block a user