This commit is contained in:
Jonas_Jones 2023-01-24 11:13:46 +01:00
parent 40e3b6cac1
commit 3801ab2697
9 changed files with 1463 additions and 1418 deletions

View file

@ -16,7 +16,7 @@ def create_cards():
list of cards list of cards
''' '''
card_deck = [] card_deck = []
# add every combination of suit and rank to the deck
for suit in SUITS: for suit in SUITS:
for rank in RANKS: for rank in RANKS:
card = {'rank': rank, 'suit': suit} card = {'rank': rank, 'suit': suit}

49
main.py
View file

@ -10,6 +10,7 @@ import game
from game_bot import BOT from game_bot import BOT
from game_player import PLAYER from game_player import PLAYER
# initialize screen
screen = Matrix() screen = Matrix()
NO = ["n", 'N', 'no', 'No', 'NO', 'nO',''] NO = ["n", 'N', 'no', 'No', 'NO', 'nO','']
@ -30,6 +31,7 @@ def main(first_time:bool=True, previous_config = None):
- previous_config: list - previous_config: list
previous config of the game previous config of the game
''' '''
# welcome screen - only shown on first start
if first_time: if first_time:
screen.refresh() screen.refresh()
screen.set_frame(int(Terminal.get_columns() / 2 - 25), screen.set_frame(int(Terminal.get_columns() / 2 - 25),
@ -57,73 +59,108 @@ def main(first_time:bool=True, previous_config = None):
"at least 80x28, the game will pause") "at least 80x28, the game will pause")
screen.set_string_center(int(Terminal.get_lines() /2 + 9), screen.set_string_center(int(Terminal.get_lines() /2 + 9),
"and display an error message until resolved") "and display an error message until resolved")
# ask if user wants to see the rules
if screen_handler.console_input( if screen_handler.console_input(
"Would you like to check out the rules of the game?", "[Y/n]", "Would you like to check out the rules of the game?", "[Y/n]",
screen) in YES: screen) in YES:
screen_handler.game_rules(screen) screen_handler.game_rules(screen)
screen.refresh() screen.refresh()
# ask if user wants to reuse the previous config
# only shown if the game is not started for the first time
if previous_config is not None and screen_handler.console_input( if previous_config is not None and screen_handler.console_input(
"Would you like to reuse the configuration from the last game?", "Would you like to reuse the configuration from the last game?",
"[Y/n]", screen) in YES: "[Y/n]", screen) in YES:
pass pass
else: else:
# else show config screen
previous_config = screen_handler.config_sequence(screen) previous_config = screen_handler.config_sequence(screen)
# previous_config = ['', <Player count>, '<Bot Count>']
screen.refresh() screen.refresh()
screen.print() screen.print()
# game initialization
# create players and bots
players = [] players = []
# create players
# Ask for player names, set cards to empty list
# PLAYER(name, cards)
for i in range(0, int(previous_config[1])): for i in range(0, int(previous_config[1])):
players.append(PLAYER(screen_handler.console_input( players.append(PLAYER(screen_handler.console_input(
f'Name for Player {i + 1}: In der kürze liegt die Würze \ f'Name for Player {i + 1}: In der kürze liegt die Würze \
(In short lies the spice :) )', '', screen=screen), [])) (In short lies the spice :) )', '', screen=screen), []))
# create bots
# set cards to empty list
# id is number of the bot starting from 1
# name is Bot <id>
# BOT(name, id, cards)
for i in range(0, int(previous_config[2])): for i in range(0, int(previous_config[2])):
players.append(BOT(f'Bot {i + 1}', i + 1, [])) players.append(BOT(f'Bot {i + 1}', i + 1, []))
screen.refresh() screen.refresh()
# starting screen with player names
screen_handler.starting_screen(screen, players) screen_handler.starting_screen(screen, players)
sleep(1) sleep(1)
screen.refresh() screen.refresh()
# determine the amount of turns
# 52 cards per card deck
max_turns = int(52 / len(players)) max_turns = int(52 / len(players))
# debug to shorten game # debug to shorten game
# max_turns = 3 # max_turns = 3
# main game loop
for turn in range(1, max_turns): for turn in range(1, max_turns):
SUITS = ['Spades', 'Hearts', 'Diamonds', 'Clubs'] SUITS = ['Spades', 'Hearts', 'Diamonds', 'Clubs']
SUITS_SYM = ["", "", "", ""] SUITS_SYM = ["", "", "", ""]
# determine trumpf color for this turn
# is determined randomly
trumpf_color = SUITS[randint(0, 3)] trumpf_color = SUITS[randint(0, 3)]
# show trumpf screen
screen_handler.trumpf_screen(screen, trumpf_color) screen_handler.trumpf_screen(screen, trumpf_color)
sleep(2) sleep(2)
# create and deal cards
card_deck = game.create_cards() card_deck = game.create_cards()
# deal_cards(card_deck, amount, turn)
# dealt_cards is a tuple of lists
dealt_cards = game.deal_cards(card_deck, int(previous_config[1]) + dealt_cards = game.deal_cards(card_deck, int(previous_config[1]) +
int(previous_config[2]), turn) int(previous_config[2]), turn)
# set cards for each player
for player in players: for player in players:
player.set_cards(dealt_cards[players.index(player)]) player.set_cards(dealt_cards[players.index(player)])
# trick loop
# cards is the list of played cards in the order of the players
cards = [] cards = []
for stiche in range(0, turn): for stiche in range(0, turn):
cards = [] cards = []
# each player plays a card
for player in players: for player in players:
# if player is a bot, use bot interface
if player.is_bot(): if player.is_bot():
cards.append(screen_handler.bot_interface(screen, player, cards.append(screen_handler.bot_interface(screen, player,
turn, stiche + 1)) turn, stiche + 1))
sleep(0.5) sleep(0.5)
# else use player interface
else: else:
cards.append(screen_handler.player_interface( cards.append(screen_handler.player_interface(
screen, player, turn, stiche + 1, trumpf_color, screen, player, turn, stiche + 1, trumpf_color,
SUITS_SYM[SUITS.index(trumpf_color)])) SUITS_SYM[SUITS.index(trumpf_color)]))
print(cards) # both the bot and the player interface return the played card
# determine winner of the trick
winner = game.compare_cards(cards, trumpf_color) winner = game.compare_cards(cards, trumpf_color)
players[winner].add_points(1) players[winner].add_points(1)
# show trick winner
screen_handler.stich_win_screen(screen, players[winner], players, screen_handler.stich_win_screen(screen, players[winner], players,
cards, trumpf_color, SUITS_SYM[SUITS.index(trumpf_color)]) cards, trumpf_color, SUITS_SYM[SUITS.index(trumpf_color)])
@ -147,14 +184,22 @@ def main(first_time:bool=True, previous_config = None):
if __name__ == "__main__": if __name__ == "__main__":
try: try:
# run the game
PREVIOUS_CONFIG = main() PREVIOUS_CONFIG = main()
# main() returns the previous config
# ask if user wants to play again
# repeat until user does not want to play again
while screen_handler.console_input("Would you like to play again?", while screen_handler.console_input("Would you like to play again?",
"[Y/n]", screen) in YES: "[Y/n]", screen) in YES:
screen.refresh() screen.refresh()
# run the game again with the previous config
PREVIOUS_CONFIG = main(first_time=False, PREVIOUS_CONFIG = main(first_time=False,
previous_config=PREVIOUS_CONFIG) previous_config=PREVIOUS_CONFIG)
# if user presses ctrl + c (keyboard interrupt), exit the game
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
# clear the terminal and print a thank you message
Terminal.clear() Terminal.clear()
print("Thank you for playing!") print("Thank you for playing!")