class TestUser(unittest.TestCase): def setUp(self): '''Set up a new empty User object, with an empty board of size 10.''' board = Board(10) self.user = User(10, board) def test_AI_guess_registered(self): '''Make sure a random AI guess gets added to the list of guesses.''' coordinate = self.user.AI_guess() self.assertTrue(coordinate in self.user._guesses, \ 'AI guess was not added to list of guesses.') def test_invalid_guess_big_numbers(self): '''Make sure a guess with a coordinate equal to the board size is considered invalid. ''' # [10, 1] is invalid because the board is numbered from zero, so # a board of size 10 only goes up to 9. self.assertTrue(self.user.invalid_guess([10, 1]), \ 'An invalid row coordinate was declared valid.') def test_invalid_guess_already_guessed(self): '''Make sure a coordinate that has already been guessed is invalid.''' self.user._guesses.append([0, 0]) self.assertTrue(self.user.invalid_guess([0, 0]), \ 'A guess made twice was declared valid.')
def __init__(self, size=10): self.size = size board_1 = self.random_board() board_2 = self.random_board() board_2.hid = True self.user = User(board_1, board_2) self.ai = AI(board_2, board_1)
def playeradvance(levelnow): ball = pygame.image.load('wimages/ball.png') player_x = 640 # levelnow[0] player_y = 360 # levelnow[1] player = User(ball, player_x, player_y) player.walls = wall_list sprite_list.add(player) return player
def __init__(self, print_func=print, input_func=input, dealer=None, user=None): """ This is where most of the basic game set up should take place. Namely the creation of the deck as well as the dealer. For now we can also add the player. In: None Exceptions: None Out: None """ # Variables to control both the users starting bank as well as their goal self.starting_bank = 100 self.score_goal = 250 # Override functions so you can create a wrapper around the program self._print = print_func self._input = input_func # Create a new deck of cards self.deck = Deck(deck_count=2) # Add the players that the game cares about self.dealer = dealer or Dealer() self.user = user or User(starting_bank=self.starting_bank) self.round = 0
def difficulty_level(self): valid_easy_responses = {'e', 'easy'} valid_hard_responses = {'h', 'hard'} while True: level_response = self._input( 'Which difficulty? \nEasy: start with 100 chips, and goal is 250 chips \nHard: start with 50 chips, and goal is 500 chips \nPress (e) or (h): ' ) if level_response.lower() in valid_easy_responses: self.user = User(starting_bank=100) self.score_goal = 250 break if level_response.lower() in valid_hard_responses: self.user = User(starting_bank=50) self.score_goal = 500 break self._print('Difficulty must be Easy or Hard')
def player_process(self, id, deck): me = User(id) myQueue = self.turns[id] nextQueue = self.turns[(id + 1) % self.numOfPlayers] self.players.append(me) deck.to_hand(me, self.numOfCardsPerPlayer) me.turnsLeft = 1 while True: # If player's turn if not myQueue.empty(): # Implement game logic here if me.turnsLeft > 0: # if there are still turns left, don't change turns response = input("> ") #"> spit?(f)/slap?(j) " if response == "f": # spit self.game_logic(me, id, deck) elif response == "j": # slap me.slap(deck) # print(self) elif response == "d": # print deck print_deck(deck) elif me.turnsLeft == 0: # if there are no turns left, change turns self.change_turns(nextQueue) # Even if not player's turn, ask for response; only choice is slap else: pass
def test_doubleElim(self): entrant1 = User("entrant1") entrant2 = User("entrant2") entrant3 = User("entrant3") entrant4 = User("entrant4") entrant5 = User("entrant5") entrant6 = User("entrant6") entrant7 = User("entrant7") entrant8 = User("entrant8") tournament = Tournament([entrant1, entrant2, entrant3, entrant4, entrant5, entrant6, entrant7, entrant8], BracketTypes.DOUBLEELIMINATION) self.assertTrue(tournament.bracket.rounds[0].matches[0].entrant1 == entrant1) self.assertTrue(tournament.bracket.rounds[0].matches[0].loserPlaysInMatch == tournament.bracket.rounds[0].matches[1].loserPlaysInMatch) self.assertTrue(tournament.bracket.rounds[0].matches[0].winnerPlaysInMatch == tournament.bracket.rounds[0].matches[1].winnerPlaysInMatch) self.assertTrue(tournament.bracket.rounds[5].matches[0].winnerPlaysInMatch == tournament.bracket.rounds[1].matches[0].loserPlaysInMatch)
def difficulty_level(self): """ Set the difficulty level for game. Out: (boolean) Should start game """ valid_easy_responses = {'e', 'easy'} valid_hard_responses = {'h', 'hard'} while True: self._print( 'On easy difficulty you start with 100 chips and try to get to 250 chips.' ) self._print( 'On hard difficulty you start with 50 chips and try to get to 500 chips.' ) level_response = self._input( 'Which difficulty do you want to play on?') if level_response.lower() in valid_easy_responses: self.starting_bank = 100 self.score_goal = 250 self.user = User(starting_bank=self.starting_bank) return True if level_response.lower() in valid_hard_responses: self.starting_bank = 50 self.score_goal = 500 self.user = User(starting_bank=self.starting_bank) return True if level_response.lower() in self._valid_exit: return False self._print('Difficulty must be easy or hard.')
def setUp(self): '''Set up a new empty User object, with an empty board of size 10.''' board = Board(10) self.user = User(10, board)
from game import Game from player import User from interface import HumanInterface, RuleAIInterface if __name__ == '__main__': human_interface = HumanInterface() ai_interface = RuleAIInterface() users = [ User('A', ['q', 'w', 'e', 'r'], human_interface), User('B', ['a', 's', 'd', 'f'], ai_interface), ] game = Game(users) game.play()
class Game: """ This is the main controller for Black Jack. It handles the creation of the Deck, Dealer, and User. As well as managing the main parts of the game flow. """ # Used to give the user different options for answering prompts _valid_exit = {'exit', 'exit()', 'leave', 'quit', 'q'} _valid_yes = {'y', 'yes', 'yeah', 'ok', 'sure'} _valid_hit = {'h', 'hit', 'deal', 'hit me'} _valid_stay = {'s', 'stay', 'stop'} def __init__(self, print_func=print, input_func=input, dealer=None, user=None): """ This is where most of the basic game set up should take place. Namely the creation of the deck as well as the dealer. For now we can also add the player. In: None Exceptions: None Out: None """ # Variables to control both the users starting bank as well as their goal self.starting_bank = 100 self.score_goal = 250 # Override functions so you can create a wrapper around the program self._print = print_func self._input = input_func # Create a new deck of cards self.deck = Deck(deck_count=2) # Add the players that the game cares about self.dealer = dealer or Dealer() self.user = user or User(starting_bank=self.starting_bank) self.round = 0 def play(self): """ Start the game loop as well as any other set up that the user needs In: None Exceptions: None Out: """ if not self.intro(): exit_game() if not self.difficulty_level(): exit_game() # Run the game loop while True: self.turn() if self.deck.deck_size() / 4 > self.deck.cards_remaining(): self.deck.shuffle() # Goal met and may play more if self.user.get_bank() >= self.score_goal: self.start_endless() break # Game loss met and exit game if self.user.get_bank() <= 0: if self.user.get_max_bank() <= self.starting_bank: self._print( f'You had no net gain over {self.round} rounds.') else: self._print( f'You amassed a max of {self.user.get_max_bank()} points over {self.round} rounds.' ) exit_game() break def intro(self): """ Player sees introduction to Black Jack. Out: (boolean) Should start game """ self._print('Welcome to Black Jack!') self._print( 'On easy difficulty you start with 100 chips and try to get to 250 chips.' ) self._print( 'On hard difficulty you start with 50 chips and try to get to 500 chips.' ) response = self._input('Do you want to play?') if response not in self._valid_yes: self._print('Okay, come again!') return False return True def difficulty_level(self): """ Set the difficulty level for game. Out: (boolean) Should start game """ valid_easy_responses = {'e', 'easy'} valid_hard_responses = {'h', 'hard'} while True: self._print( 'On easy difficulty you start with 100 chips and try to get to 250 chips.' ) self._print( 'On hard difficulty you start with 50 chips and try to get to 500 chips.' ) level_response = self._input( 'Which difficulty do you want to play on?') if level_response.lower() in valid_easy_responses: self.starting_bank = 100 self.score_goal = 250 self.user = User(starting_bank=self.starting_bank) return True if level_response.lower() in valid_hard_responses: self.starting_bank = 50 self.score_goal = 500 self.user = User(starting_bank=self.starting_bank) return True if level_response.lower() in self._valid_exit: return False self._print('Difficulty must be easy or hard.') def increment_round(self): """ Increment the round number In: None Out: None """ self.round += 1 def shuffle_deck(self): """ Shuffles decks of cards In: None Out: None """ self.deck.shuffle() def start_endless(self): """ This mode is made an option once the user passes the point where they have more points the the goal. It allows them to play until they go broke. In: None Out: None """ self._print(f'You have beat the table in {self.round} hands') response = self._input( f'You have {self.user.get_bank()} points. Would you like to continue? (y/n) ' ) if response.lower() in self._valid_yes: while self.user.get_bank() > 0: self.turn() if self.deck.deck_size() / 4 > self.deck.cards_remaining(): self.deck.shuffle() def turn(self): """ Runs through a turn of the game In: None Out: None """ self.increment_round() if not self.place_user_bet(self.user.get_bank()): exit_game() self.deal() self.user_turn() self.dealer_turn() self.calculate_winner() self.reset_hands() def place_user_bet(self, current_bank): """ Shows current bank, requests bet, handles edge cases In: (int) current_bank of player Out: (boolean) Should exit game """ while True: print('\n') self._print(f'Your current bank is {current_bank}') self._print('how much would you like to bet?') player_bet = self._input('Bet: ') # If the player wants to exit they can if player_bet.lower() in self._valid_exit: return False if re.match(r'\s*[0-9]+\s*$', player_bet): player_bet = int(player_bet) if player_bet >= 1 and player_bet <= current_bank: self.user.place_bet(player_bet) return True elif player_bet == 0: self._print('Your bet must be greater than 0') else: self._print('You can\'t bet more points than you have') else: self._print('Please enter an integer') def deal(self): """ Deals 2 cards to the user and dealer In: None Out: None """ for _ in range(0, 2): self.user.hit(self.deck.deal()) self.dealer.hit(self.deck.deal()) def user_turn(self): """ Handles the users decision to either hit and gain a card, or to stay and keep the cards they have. In: None Out: None """ while not self.user.bust(): self.save_game() print('\n') self._print(f'The Dealer shows:\n{repr(self.dealer)}\n') self._print(f'Your hand is:\n{str(self.user)}\n') self._print('Your current score is ', self.user.get_score()) hit_or_stay_input = self._input( 'Would you like to hit or stay? (h/s): ') # If the player wants to exit they can if hit_or_stay_input.lower() in self._valid_exit: exit_game() if hit_or_stay_input.lower() in self._valid_stay: return elif hit_or_stay_input in self._valid_hit: self.user.hit(self.deck.deal()) else: self._print('invalid input') def dealer_turn(self): """ Controls the dealers logic if they need to hit again or keep the cards that they already have In: None Out: None """ while not self.dealer.bust(): if self.dealer.get_score() < 17: self.dealer.hit(self.deck.deal()) else: break def calculate_winner(self): """ ** WIP ** Used to decide if the player or the dealer is the one to win the round In: None Out: Changes the value in the Player's bank """ print('\n') self._print(f'Your last hand is:\n{str(self.user)}') self._print(f'Your score is {self.user.get_score()}') if self.user.bust(): self._print('You have bust') self.user.beat_dealer(False) else: self._print(f'The Dealer\'s hand is:\n{str(self.dealer)}') self._print(f'Dealer has {self.dealer.get_score()} points') if self.user.get_score() == self.dealer.get_score( ) and not self.dealer.bust(): self._print(f'It was a tie\nYou don\'t gain or lose points') return result = self.user.get_score() > self.dealer.get_score( ) or self.dealer.bust() self.user.beat_dealer(result) if self.dealer.bust(): self._print('The Dealer bust') if result: self._print('You beat the Dealer') else: self._print('You lost this hand') def reset_hands(self): """ Resets hands of all players to contain no cards In: None Out: None """ self.user.reset_hand() self.dealer.reset_hand() def save_game(self): """ Calls on the player and the deck to save their respective cards to the csv files for the notebook. In: None Exceptions: No Data To Save Out: .csv files """ self.user.to_csv() self.deck.to_csv()
def test_dealer_instance(): assert User()
def user(): return User()
user.hit(test_input_4) assert user.get_score() == expected @pytest.mark.parametrize("test_input_1, test_input_2, test_input_3, test_input_4, expected", [(Card(10), Card(3), Card(5), Card(7), True), (Card(6), Card(5), Card(9), Card(1), False), (Card(10), Card(1), Card(5), Card(6), True)]) def test_user_bust(test_input_1, test_input_2, test_input_3, test_input_4, expected, user): user.hit(test_input_1) user.hit(test_input_2) user.hit(test_input_3) user.hit(test_input_4) assert user.bust() == expected @pytest.mark.parametrize("test_input_1, test_input_2,expected", [(Card(10), Card(11), True and len(User().hand) == 2), (Card(10), Card(10), False and len(User().hand) == 2)]) def test_user_blackjack(test_input_1, test_input_2,expected, user): user.hit(test_input_1) user.hit(test_input_2) assert user.blackjack() == expected def test_user_reset_hand(user): assert user.reset_hand() == None def test_get_bank(user): user.bank = 100 actual = user.get_bank() assert user.bank == actual def test_get_bet_and_place_bet(user):
def __init__(self, deck): self.deck = deck self.user = User(deck) self.dealer = Dealer(deck) self.mxscore = 21 self.deck.shuffle()
class Game: def __init__(self, size=10): self.size = size board_1 = self.random_board() board_2 = self.random_board() board_2.hid = True self.user = User(board_1, board_2) self.ai = AI(board_2, board_1) def create_ships(self): ships = (4, 3, 3, 2, 2, 2, 1, 1, 1, 1) board = Field(size=self.size) a = 0 for i in ships: while True: a += 1 if a > 2000: return None ship = Ship(Dot(randint(0, self.size), randint(0, self.size)), randint(1, 2), i) try: board.add_ship(ship) break except CannotPlaceShip: pass board.check() return board def random_board(self): board = None while board is None: board = self.create_ships() return board @staticmethod def logo(): print("___________________") print("| SEA BUTTLE |") print("|#################|") print("| Use 1 to 10 |") print("|_____to shoot____|") def loop(self): turns = 0 while True: print("User:"******"#" * 22) print("AI:") self.ai.board.show() if turns % 2 == 0: print("#" * 23) print("Make a turn") repeat = self.user.move() else: print("#" * 23) print("AI makes a turn") repeat = self.ai.move() if repeat: turns -= 1 if self.ai.board.alive == 0: print("#" * 23) print("You win!") break elif self.user.board.alive == 0: print("-" * 23) print("AI wins!") break turns += 1 def start(self): self.logo() self.loop()
class Round: """start a round of blackjack Arguments ---------- deck : shared deck of cards between classes Parameters ---------- user : player class controlled by input dealer : player class controlled by hit rules mxscore : maximum score without busting """ def __init__(self, deck): self.deck = deck self.user = User(deck) self.dealer = Dealer(deck) self.mxscore = 21 self.deck.shuffle() def play(self): def deal(self): self.user.get_card() self.user.get_card() self.dealer.get_card() print("\nDealer has {0} ??? for a visible total of {1} points".format(str(self.dealer), self.dealer.first_ace())) self.dealer.get_card() # Dealer gets 2nd card after first print to keep 2nd card hidden print("You have {0} for a total of {1} points".format(str(self.user), self.user.score())) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") check_blackjack(self) def check_blackjack(self): if self.dealer.score() == 21: print("Dealer has {0} for a total of {1} points".format(str(self.dealer), self.dealer.score())) print("Dealer got a Blackjack! You Lose!") elif self.user.score() == 21: print("You got a Blackjack! You Win!") else: user_hit(self) def user_hit(self): while self.user.score() <= 21 and self.user.hit(): if self.user.score() > 21: print("You have {0} for a total of {1} points. \nYOU BUSTED!".format(str(self.user), self.user.score())) if self.user.score() <= 21: print("You have {0} for a total of {1} points.".format(str(self.user), self.user.score())) if self.user.score() <= 21: print("You stay with {0} for a total of {1} points.".format(str(self.user), self.user.score())) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") dealer_hit(self) def dealer_hit(self): print("Dealer has {0} for a total of {1} points.".format(str(self.dealer), self.dealer.score())) if self.dealer.score() >= 17: print("Dealer stays at {0}".format(self.dealer.score())) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") compare_score(self) while self.dealer.score() < 17: print("Dealer Hits") self.dealer.hit() print("Dealer has {0} for a total of {1} points.".format(str(self.dealer), self.dealer.score())) if 17 <= self.dealer.score() <= 21: print("Dealer stays at {0}".format(self.dealer.score())) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") compare_score(self) elif self.dealer.score() > 21: print("DEALER BUSTED!".format(str(self.dealer), self.dealer.score())) def compare_score(self): if self.user.score <= self.dealer.score: print("The Dealer's {0} beats your {1}. \nYOU LOSE!").format(self.dealer.score(), self.user.score()) if self.user.score > self.dealer.score: print("Your {0} beats the Dealer's {1}. \nYOU WIN!").format(self.user.score(), self.dealer.score()) deal(self)
class Game: """ This is the main controller for Black Jack. It handles the creation of the Deck, Dealer, and User. As well as managing the main parts of the game flow. """ def __init__(self, print_func=print, input_func=input): """ This is where most of the basic game set up should take place. Namely the creation of the deck as well as the dealer. For now we can also add the player. In: None Exceptions: None Out: None """ # Variables to control both the users starting bank as well as their goal self.starting_bank = 100 self.score_goal = 250 self.endless = False # Override functions so you can create a wrapper around the program self._print = print_func self._input = input_func # Create a new deck of cards self.deck = Deck(deck_count=2) # Add the players that the game cares about self.dealer = Dealer() self.user = User(starting_bank=self.starting_bank) self.round = 0 def difficulty_level(self): valid_easy_responses = {'e', 'easy'} valid_hard_responses = {'h', 'hard'} while True: level_response = self._input( 'Which difficulty? \nEasy: start with 100 chips, and goal is 250 chips \nHard: start with 50 chips, and goal is 500 chips \nPress (e) or (h): ' ) if level_response.lower() in valid_easy_responses: self.user = User(starting_bank=100) self.score_goal = 250 break if level_response.lower() in valid_hard_responses: self.user = User(starting_bank=50) self.score_goal = 500 break self._print('Difficulty must be Easy or Hard') def iterate_round(self): """ Increment the round number In: None Out: None """ self.round += 1 def shuffle_deck(self): """ Shuffles decks of cards In: None Out: None """ if self.deck.deck_size() / 4 > self.deck.cards_remaining(): self.deck.shuffle() def place_user_bet(self, value): """ Shows current bank, requests bet, handles edge cases In: None Out: None """ current_bank = self.user.get_bank() if value >= 1 and value <= current_bank: self.user.place_bet(value) def deal(self): """ Deals 2 cards to the user and dealer In: None Out: None """ for _ in range(0, 2): self.user.hit(self.deck.deal()) self.dealer.hit(self.deck.deal()) self.save_game() def user_hand(self): return self.user.hand def dealer_hand(self): return self.dealer.hand def user_hit(self): """ Handles the users decision to either hit and gain a card, or to stay and keep the cards they have. In: None Out: None """ self.user.hit(self.deck.deal()) self.save_game() def dealer_turn(self): """ Controls the dealers logic if they need to hit again or keep the cards that they already have In: None Out: None """ while not self.dealer.bust(): if self.dealer.get_score() < 17: self.dealer.hit(self.deck.deal()) else: break def calculate_winner(self): """ ** WIP ** Used to decide if the player or the dealer is the one to win the round In: None Out: Changes the value in the Player's bank """ if self.user.bust(): self.user.beat_dealer(False) return False else: if self.user.blackjack() and not self.dealer.blackjack(): self.user.beat_dealer(True) return True if not self.user.blackjack() and self.dealer.blackjack(): self.user.beat_dealer(False) return False if self.user.get_score() == self.dealer.get_score( ) and not self.dealer.bust(): return None result = self.user.get_score() > self.dealer.get_score( ) or self.dealer.bust() self.user.beat_dealer(result) return result def reset_hands(self): """ Resets hands of all players to contain no cards In: None Out: None """ self.user.reset_hand() self.dealer.reset_hand() def save_game(self): """ Calls on the player and the deck to save their respective cards to the csv files for the notebook. In: None Exceptions: No Data To Save Out: .csv files """ self.user.to_csv() self.deck.to_csv()