This commit is contained in:
Jonas_Jones 2022-12-23 01:55:48 +01:00
parent c8a91d9077
commit ad3f491a81
4 changed files with 126 additions and 55 deletions

34
game.py
View file

@ -1,5 +1,8 @@
__author__ = "7987847, Werner, 7347119, Fajst, 7735965, Melikidze"
from random import randint
from time import sleep
RANKS = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
SUITS = ['Spades', 'Hearts', 'Diamonds', 'Clubs']
@ -33,14 +36,14 @@ def deal_cards(card_deck:list, players:int, cards_per_player:int):
for player in range(players):
temp_cards.append([])
for card in range(cards_per_player):
temp_cards[player].append(card_deck.pop(0))
temp_cards[player].append(card_deck.pop(randint(0, len(card_deck)-1)))
while len(temp_cards) < 5:
temp_cards.append([])
return (temp_cards[0], temp_cards[1], temp_cards[2], temp_cards[3], temp_cards[4])
def compare_cards(cards_to_compare:list):
def compare_cards(cards_to_compare:list, trumpf_color:str):
'''
Compares the given cards and returns the winner
@ -52,14 +55,25 @@ def compare_cards(cards_to_compare:list):
index of the winning card
'''
winner = 0
# TODO: wtf is Trumpffarbe? help
# Trumpffarbe-condition muss auch noch implementiert werden
for i in range(1, len(cards_to_compare)):
if RANKS.index(cards_to_compare[i]['rank']) > RANKS.index(cards_to_compare[winner]['rank']):
winner = i
if RANKS.index(cards_to_compare[i]['rank']) == RANKS.index(cards_to_compare[winner]['rank']):
if SUITS.index(cards_to_compare[i]['suit']) > SUITS.index(cards_to_compare[winner]['suit']):
winner = i
trumpf = []
for i in cards_to_compare:
if i.get('suit') == trumpf_color:
trumpf.append(cards_to_compare.index(i))
if len(trumpf) == 1:
return trumpf[0]
elif len(trumpf) > 1:
winner = 0
for j in trumpf:
if RANKS.index(cards_to_compare[j].get('rank')) > RANKS.index(cards_to_compare[winner].get('rank')):
winner = j
winner = j
else:
winner = 0
for j in cards_to_compare:
if RANKS.index(j.get('rank')) > RANKS.index(cards_to_compare[winner].get('rank')):
winner = cards_to_compare.index(j)
print(winner, j, cards_to_compare)
return winner

View file

@ -69,4 +69,22 @@ class PLAYER:
- points: int
points of the player
'''
return self.points
return self.points
def add_card(self, card):
'''
Adds a card to the players cards
input:
- card: dict
card to add
'''
self.cards.append(card)
def set_cards(self, cards):
'''
Sets the cards of the player
input:
- cards: list
list of cards
'''
self.cards = cards

54
main.py
View file

@ -2,6 +2,7 @@ __author__ = "7987847, Werner, 7347119, Fajst, 7735965, Melikidze"
from cmd_interface import Matrix, Terminal
from time import sleep
from random import randint
import screen_handler
import game
@ -29,7 +30,7 @@ def main(first_time:bool=True, previous_config = None):
screen.set_string_center(int(Terminal.get_lines() /2 + 4), "Your terminal currently has the size:")
screen.set_string_center(int(Terminal.get_lines() /2 + 5), f"{Terminal.get_columns()}x{Terminal.get_lines()}")
screen.set_string_center(int(Terminal.get_lines() /2 + 7), "If your terminal is not fullscreen or")
screen.set_string_center(int(Terminal.get_lines() /2 + 8), "at least 80x24, the game will pause")
screen.set_string_center(int(Terminal.get_lines() /2 + 8), "at least 80x28, the game will pause")
screen.set_string_center(int(Terminal.get_lines() /2 + 9), "and display an error message until resolved")
if screen_handler.console_input("Would you like to check out the rules of the game?", "[Y/n]", screen) in YES:
# TODO: Show rules
@ -43,36 +44,55 @@ def main(first_time:bool=True, previous_config = None):
screen.refresh()
screen.print()
card_deck = game.create_cards()
dealt_cards = game.deal_cards(card_deck, int(previous_config[1]) + int(previous_config[2]), int(previous_config[3]))
players = []
for i in range(0, int(previous_config[1])):
players.append(PLAYER(screen_handler.console_input(f'Name for Player {i + 1}', '', screen=screen), dealt_cards[i]))
players.append(PLAYER(screen_handler.console_input(f'Name for Player {i + 1}: In der kürze liegt die Würze (In short lies the spice :) )', '', screen=screen), []))
for i in range(0, int(previous_config[2])):
players.append(BOT(f'Bot {i + 1}', i + 1, dealt_cards[len(previous_config[1]) + i]))
players.append(BOT(f'Bot {i + 1}', i + 1, []))
screen.refresh()
screen_handler.starting_screen(screen, players)
sleep(5)
sleep(1)
screen.refresh()
round_no = 0
while players[0].get_cards() != []:
round_no += 1
cards = []
max_turns = int(52 / len(players))
max_turns = 3
for turn in range(1, max_turns):
SUITS = ['Spades', 'Hearts', 'Diamonds', 'Clubs']
SUITS_SYM = ["", "", "", ""]
trumpf_color = SUITS[randint(0, 3)]
screen_handler.trumpf_screen(screen, trumpf_color)
sleep(2)
card_deck = game.create_cards()
dealt_cards = game.deal_cards(card_deck, int(previous_config[1]) + int(previous_config[2]), turn)
for player in players:
if player.is_bot():
cards.append(screen_handler.bot_interface(screen, player, round_no))
sleep(0.5)
else:
cards.append(screen_handler.player_interface(screen, player, round_no))
players[game.compare_cards(cards)].add_points(1)
player.set_cards(dealt_cards[players.index(player)])
cards = []
for stiche in range(0, turn):
cards = []
for player in players:
if player.is_bot():
cards.append(screen_handler.bot_interface(screen, player, turn, stiche + 1))
sleep(0.5)
else:
cards.append(screen_handler.player_interface(screen, player, turn, stiche + 1, trumpf_color, SUITS_SYM[SUITS.index(trumpf_color)]))
print(cards)
winner = game.compare_cards(cards, trumpf_color)
players[winner].add_points(1)
screen_handler.stich_win_screen(screen, players[winner], players, cards, trumpf_color, SUITS_SYM[SUITS.index(trumpf_color)])
# determine winner
winner = players[0]

View file

@ -24,12 +24,9 @@ def game_rules(screen:Matrix):
screen.refresh()
screen.set_frame(0, 0, Terminal.get_columns() - 1, Terminal.get_lines() - 1, rounded=True, title="Game Rules")
screen.set_string(2, int(Terminal.get_lines() /2 - 6), "How to play:")
screen.set_string(2, int(Terminal.get_lines() /2 - 4), "1. The player with the lowest trumpf card starts the game")
screen.set_string(2, int(Terminal.get_lines() /2 - 3), "2. The player has to play a card of the same suit as the first card")
screen.set_string(2, int(Terminal.get_lines() /2 - 2), "3. If the player does not have a card of the same suit, he can play any card")
screen.set_string(2, int(Terminal.get_lines() /2 - 1), "4. The player with the highest card of the same suit wins the round")
screen.set_string(2, int(Terminal.get_lines() /2), "5. The player with the most points wins the game")
screen.set_string(2, int(Terminal.get_lines() /2 + 1), "6. The player with the most points wins the game")
screen.set_string(2, int(Terminal.get_lines() /2 - 5), "1. The goal is to play the highest card.")
screen.set_string(2, int(Terminal.get_lines() /2 - 4), "2. The trumpf color outweights all other colors.")
screen.set_string(2, int(Terminal.get_lines() /2 - 3), "3. The player who wins the most amounts of tricks (Stiche) wins the game.")
screen.set_string(2, int(Terminal.get_lines() /2 + 3), "Press ENTER key to continue...")
screen.print()
input()
@ -39,24 +36,26 @@ def config_sequence(screen:Matrix):
screen.refresh()
screen.set_frame(int(Terminal.get_columns() / 2 - 25), int(Terminal.get_lines() / 2 - 10), 50, 20, rounded=True, title="Game Configuration")
screen.set_string_center(int(Terminal.get_lines() /2 - 6), "Please answer the questions below:")
config = ['', '1', '1','1']
question_possible_asnwers = ['1 - 5', '', '']
config = ['', '1', '1']
question_possible_asnwers = ['1 - 5', '']
previous_question = ""
question_no = 0
for i in ['How many players are playing?','How many bots should be playing?', 'How many cards should be drawn per player?']:
input = "-1"
while not int(question_possible_asnwers[question_no].split(" - ")[0]) <= int(input) <= int(question_possible_asnwers[question_no].split(" - ")[1]):
for i in ['How many players are playing?','How many bots should be playing?']:
wrong_input = True
while wrong_input:
screen.set_string(int(Terminal.get_columns() / 2 - 24),int(Terminal.get_lines() /2 - 4 + question_no), " ")
screen.set_string(int(Terminal.get_columns() / 2 - 24),int(Terminal.get_lines() /2 - 2 + question_no), " ")
screen.set_string_center(int(Terminal.get_lines() /2 - 4 + question_no), previous_question + " " + config[question_no])
screen.set_string_center(int(Terminal.get_lines() /2 - 2 + question_no), i)
screen.set_frame(int(Terminal.get_columns() / 2 - 24), int(Terminal.get_lines() / 2 - 3 + question_no), 48, 2,double=True)
input = console_input(i, "[" + question_possible_asnwers[question_no] + "]", screen)
config[question_no] = input
if input.isdigit():
if int(question_possible_asnwers[question_no].split(" - ")[0]) <= int(input) <= int(question_possible_asnwers[question_no].split(" - ")[1]):
wrong_input = False
config[question_no + 1] = input
previous_question = i
question_no += 1
question_possible_asnwers[1] = f"{'0' if config[1] == '5' else '1'} - {5 - int(config[1])}"
question_possible_asnwers[2] = f"1 - {str(int(52 / (int(config[1]) + int(config[2]))))}"
return config
@ -65,11 +64,6 @@ def starting_screen(screen:Matrix, players:list):
screen.set_frame(int(Terminal.get_columns() / 2 - 25), int(Terminal.get_lines() / 2 - 10), 50, 20, rounded=True, title="Preparing...")
screen.set_string_center(int(Terminal.get_lines() /2 - 6), "The game is starting...")
screen.set_string_center(int(Terminal.get_lines() /2 - 4), "Here are the players:")
# screen.set_string_center(int(Terminal.get_lines() /2 - 3), players[0].get_name())
# screen.set_string_center(int(Terminal.get_lines() /2 - 2), players[1].get_name())
# screen.set_string_center(int(Terminal.get_lines() /2 - 1), players[2].get_name())
# screen.set_string_center(int(Terminal.get_lines() /2), players[3].get_name())
# screen.set_string_center(int(Terminal.get_lines() /2 + 1), players[4].get_name())
for i in range(len(players)):
screen.set_string_center(int(Terminal.get_lines() /2 - 3 + i), players[i].get_name())
@ -131,7 +125,7 @@ def add_bot_font(screen:Matrix, bot:int):
screen.print()
def draw_player_cards(screen:Matrix, cards:list):
def draw_player_cards(screen:Matrix, cards:list, players = None):
card1 = "╔═══╗"
card2 = "║ X ║"
card3 = "║ X ║"
@ -169,28 +163,34 @@ def draw_player_cards(screen:Matrix, cards:list):
card3 = "║10 ║"
else:
card3 = f"{card.get('rank')}"
screen.set_string(3 + x, y - 1, " " + str(i))
if players != None:
screen.set_string(3 + x, y - 1, " " + players[i].get_name() + ": " + str(players[i].get_points()) + " points")
else:
screen.set_string(3 + x, y - 1, " " + str(i + 1))
screen.set_string(3 + x, y, card1)
screen.set_string(3 + x, y + 1, card2)
screen.set_string(3 + x, y + 2, card3)
screen.set_string(3 + x, y + 3, card4)
if players != None:
x += 16
x += 6
def player_interface(screen:Matrix, player:PLAYER, round_no:int):
def player_interface(screen:Matrix, player:PLAYER, round_no:int, stich:int, trumpf:str, trumpf_sym:str):
screen.refresh()
screen.set_frame(0, 0, Terminal.get_columns() - 1, Terminal.get_lines() - 1, rounded=True, title=f"Player '{player.get_name()}' is playing in round NO. {round_no}")
screen.set_frame(0, 0, Terminal.get_columns() - 1, Terminal.get_lines() - 1, rounded=True, title=f"Player '{player.get_name()}' is playing in round NO. {round_no} and stich NO. {stich}")
screen.set_string(Terminal.get_columns() - len(trumpf + " ") - 2, 2, "Trumpf: " + trumpf + " " + trumpf_sym)
screen.set_string(2, 2, f"Points of player {player.get_name()}: {player.get_points()}")
draw_player_cards(screen, player.get_cards())
screen.print()
chosen_card = console_input("Choose a card by its number", str(len(player.get_cards())), screen, fullscreen=True)
return player.pop_card(int(chosen_card))
chosen_card = console_input("Choose a card by its number", "[1 - " + str(len(player.get_cards())) + "]", screen, fullscreen=True)
return player.pop_card(int(chosen_card) - 1)
def bot_interface(screen:Matrix, bot:BOT, round_no:int):
def bot_interface(screen:Matrix, bot:BOT, round_no:int, stich:int):
screen.refresh()
screen.set_frame(0, 0, Terminal.get_columns() - 1, Terminal.get_lines() - 1, rounded=True, title=f"Bot {bot.get_name()} is playing in round NO. {round_no}")
screen.set_frame(0, 0, Terminal.get_columns() - 1, Terminal.get_lines() - 1, rounded=True, title=f"Bot {bot.get_name()} is playing in round NO. {round_no}, stich NO. {stich}")
add_bot_font(screen, bot.get_bot_number())
screen.print()
return bot.play_card()
@ -201,3 +201,22 @@ def winner_screen(screen, winner, players:list):
screen.set_string_center(4, "Congratulations!")
screen.set_string_center(5, f"Player '{winner.get_name()}' won the game!")
screen.print()
def trumpf_screen(screen:Matrix, trumpf:str):
screen.refresh()
screen.set_frame(int(Terminal.get_columns() / 2 - 25), int(Terminal.get_lines() / 2 - 10), 50, 20, rounded=True, title="Trumpf")
screen.set_string_center( int(Terminal.get_lines() / 2 - 1), "The trumpf is:")
screen.set_string_center( int(Terminal.get_lines() / 2), trumpf)
screen.print()
def stich_win_screen(screen:Matrix, winner, players:list, cards:list, trumpf:str, trumpf_sym:str):
screen.refresh()
screen.set_frame(0, 0, Terminal.get_columns() - 1, Terminal.get_lines() - 1, rounded=True, title=f"Player '{winner.get_name()}' won the trick!")
screen.set_frame(int(Terminal.get_columns() / 2 - 25), 19, 50, 4, double=True)
screen.set_string_center(20, "Congratulations!")
screen.set_string_center(21, f"Player '{winner.get_name()}' won the trick!")
screen.set_string_center(22, "Press ENTER to continue...")
screen.set_string(Terminal.get_columns() - len(trumpf + " ") - 2, 2, "Trumpf: " + trumpf + " " + trumpf_sym)
draw_player_cards(screen, cards, players)
screen.print()
input()