This commit is contained in:
Jonas_Jones 2022-12-23 03:01:21 +01:00
parent ef144e4bb2
commit 3353689dc7
5 changed files with 393 additions and 106 deletions

View file

@ -1,44 +1,103 @@
'''EPR 07 Aufgabe 3'''
__author__ = "7987847, Werner, 7347119, Fajst, 7735965, Melikidze"
from os import get_terminal_size, name, system
from sys import stdout
class Terminal:
'''
Terminal class
'''
def get_size():
'''
Returns the size of the terminal
output:
- columns: int
number of columns
- lines: int
number of lines
'''
return get_terminal_size().columns, get_terminal_size().lines
def get_lines():
'''
Returns the number of lines of the terminal
output:
- lines: int
number of lines
'''
return get_terminal_size().lines
def get_columns():
'''
Returns the number of columns of the terminal
output:
- columns: int
number of columns
'''
return get_terminal_size().columns
def clear():
'''
Clears the terminal
'''
system('cls' if name in ('nt', 'dos') else 'clear')
def curser_to_pos1(self):
for i in range(self.get_lines() + 2):
def curser_to_pos1():
'''
Moves the curser to the first position
'''
for _ in range(self.get_lines() + 2):
stdout.write("\033[F")
class Matrix:
'''
Matrix class
'''
def __init__(self):
self.columns, self.lines = Terminal.get_size()
self.clear()
def clear(self):
'''
Clears the matrix
'''
self.matrix = []
def refresh(self):
'''
Refreshes the matrix
'''
self.columns, self.lines = Terminal.get_size()
self.clear()
for i in range(self.lines):
self.matrix.append([])
for j in range(self.columns):
for _ in range(self.columns):
self.matrix[i].append(" ")
def set_frame(self, x, y, dx, dy, rounded=True, double=False, title=None,
alligncenter=True):
'''
Sets a frame in the matrix
input:
- x: int
x position of the frame
- y: int
y position of the frame
- dx: int
width of the frame
- dy: int
height of the frame
- rounded: bool
if the frame is rounded
- double: bool
if the frame is double
- title: str
title of the frame
- alligncenter: bool
if the title is alligned to the center
'''
if double:
self.set( x, y, "")
self.set(x + dx, y, "")
@ -82,37 +141,109 @@ class Matrix:
self.set_string(x + 2, y, title)
def set_square(self, x, y, dx, dy, char):
'''
Sets a square in the matrix
input:
- x: int
x position of the square
- y: int
y position of the square
- dx: int
width of the square
- dy: int
height of the square
- char: str
character of the square
'''
for i in range(dx):
for j in range(dy):
self.set(x + i, y + j, char)
def set(self, x, y, value):
'''
Sets a value in the matrix
input:
- x: int
x position of the value
- y: int
y position of the value
- value: str
value
'''
try:
self.matrix[y][x] = value
except IndexError:
pass
def get(self, x, y):
'''
Gets a value in the matrix
input:
- x: int
x position of the value
- y: int
y position of the value
output:
- value: str
value
'''
return self.matrix[y][x]
def print(self):
'''
Prints the matrix
'''
for i in range(self.lines):
for j in range(self.columns):
print(self.matrix[i][j], end = "")
print(end = "" if i < self.lines - 1 else "\r")
def set_string(self, x, y, chars):
'''
Sets a string in the matrix
input:
- x: int
x position of the string
- y: int
y position of the string
- chars: str
string
'''
for i in range(len(chars)):
self.set(x + i, y, chars[i])
def set_string_center(self, y, chars):
'''
Sets a string in the matrix, alligned to the center
input:
- y: int
y position of the string
- chars: str
string
'''
self.set_string(int(Terminal.get_columns() / 2 - len(chars) / 2),
y, chars)
def get_matrix(self):
'''
Gets the matrix
output:
- matrix: list
matrix
'''
return self.matrix
def add_matrix(self, x, y, matrix):
'''
Adds a matrix to the matrix
input:
- x: int
x position of the matrix
- y: int
y position of the matrix
- matrix: list
matrix
'''
for i in range(len(matrix)):
for j in range(len(matrix[i])):
self.set(self, x + j, y + i, matrix[i][j])

29
game.py
View file

@ -1,13 +1,20 @@
'''EPR 07 Aufgabe 3'''
__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']
RANKS = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen',
'King']
SUITS = ['Spades', 'Hearts', 'Diamonds', 'Clubs']
def create_cards():
'''
Creates a deck of cards
output:
- card_deck: list
list of cards
'''
card_deck = []
for suit in SUITS:
@ -20,7 +27,7 @@ def create_cards():
def deal_cards(card_deck:list, players:int, cards_per_player:int):
'''
Deals cards to players
input:
- card_deck: list
list of cards
@ -35,12 +42,14 @@ def deal_cards(card_deck:list, players:int, cards_per_player:int):
temp_cards = []
for player in range(players):
temp_cards.append([])
for card in range(cards_per_player):
temp_cards[player].append(card_deck.pop(randint(0, len(card_deck)-1)))
for _ in range(cards_per_player):
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])
return (temp_cards[0], temp_cards[1], temp_cards[2], temp_cards[3],
temp_cards[4])
def compare_cards(cards_to_compare:list, trumpf_color:str):
@ -64,13 +73,15 @@ def compare_cards(cards_to_compare:list, trumpf_color:str):
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')):
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')):
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)
@ -81,4 +92,4 @@ if __name__ == '__main__':
card_deck = create_cards()
print(card_deck)
print(deal_cards(card_deck, 5, 5))
print(deal_cards(card_deck, 5, 5))

View file

@ -1,6 +1,10 @@
'''EPR 07 Aufgabe 3'''
__author__ = "7987847, Werner, 7347119, Fajst, 7735965, Melikidze"
class BOT:
'''
Bot class
'''
def __init__(self, name, bot_number, cards):
self.name = name
self.cards = cards
@ -17,7 +21,7 @@ class BOT:
card to play
'''
return self.cards.pop(0)
def add_points(self, points):
'''
Adds points to the bot
@ -28,39 +32,7 @@ class BOT:
self.points += points
# Getters
def get_points(self, played_cards):
'''
Calculates the points of the played cards
input:
- played_cards: list
list of cards that have been played already
output:
- points: int
points of the played cards
'''
points = 0
for card in played_cards:
if card['rank'] == 'Jack':
points += 2
elif card['rank'] == 'Queen':
points += 3
elif card['rank'] == 'King':
points += 4
elif card['rank'] == 'Ace':
points += 11
return points
def get_trumpf(self, trumpf_color):
'''
Sets the trumpf color
input:
- trumpf_color: str
trumpf color
'''
self.trumpf = True
self.trumpf_color = trumpf_color
def get_trumpf_color(self):
'''
Returns the trumpf color
@ -114,7 +86,7 @@ class BOT:
if the player is a bot
'''
return True
def get_bot_number(self):
'''
Returns the bot number

View file

@ -1,13 +1,17 @@
'''EPR 07 Aufgabe 3'''
__author__ = "7987847, Werner, 7347119, Fajst, 7735965, Melikidze"
class PLAYER:
'''
Player class
'''
def __init__(self, name, cards):
self.name = name
self.cards = cards
self.points = 0
self.trumpf = False
self.trumpf_color = None
def get_name(self):
'''
Returns the name of the player
@ -25,16 +29,7 @@ class PLAYER:
if the player is a bot
'''
return False
def get_cards(self):
'''
Returns the cards of the player
output:
- cards: list
list of cards of the player
'''
return self.cards
def pop_card(self, card:int):
'''
Removes a card from the players cards
@ -52,7 +47,7 @@ class PLAYER:
list of cards of the player
'''
return self.cards
def add_points(self, points):
'''
Adds points to the player

View file

@ -1,3 +1,4 @@
'''EPR 07 Aufgabe 3'''
__author__ = "7987847, Werner, 7347119, Fajst, 7735965, Melikidze"
from time import sleep
@ -7,71 +8,145 @@ from game_bot import BOT
def console_input(text:str, input_str:str, screen:Matrix, fullscreen=False):
'''
Prints a text and waits for user input
input:
- text: str
text to print
- input_str: str
input string
- screen: Matrix
screen to print on
- fullscreen: bool
if the screen is fullscreen
output:
- input: str
user input
'''
screen.set_string(2, Terminal.get_lines() - 2, text)
screen.set(0, Terminal.get_lines() - 2, "")
screen.set_string(0, Terminal.get_lines() - 3, "╭─────────────────────────── ── ── ─ ─ ─")
screen.set_string(0, Terminal.get_lines() - 3,
"╭─────────────────────────── ── ── ─ ─ ─")
if fullscreen:
screen.set_string(0, Terminal.get_lines() - 3, "├─────────────────────────── ── ── ─ ─ ─")
screen.set_string(0, Terminal.get_lines() - 1, " ─ ─ ─ ── ── ")
screen.set_string(0, Terminal.get_lines() - 3,
"├─────────────────────────── ── ── ─ ─ ─")
screen.set_string(0, Terminal.get_lines() - 1,
" ─ ─ ─ ── ── ")
screen.print()
return input("╰→ " + input_str + " ")
def draw_card(card:dict, screen:Matrix, x:int, y:int):
pass
def game_rules(screen:Matrix):
'''
Prints the game rules
input:
- screen: Matrix
screen to print on
'''
screen.refresh()
screen.set_frame(0, 0, Terminal.get_columns() - 1, Terminal.get_lines() - 1, rounded=True, title="Game Rules")
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 - 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.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()
def config_sequence(screen:Matrix):
'''
Prints the game configuration sequence
input:
- screen: Matrix
screen to print on
output:
- config: list
list of the game configuration
'''
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:")
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']
question_possible_asnwers = ['1 - 5', '']
question_possible_asnwers = ['0 - 5', '']
previous_question = ""
question_no = 0
for i in ['How many players are playing?','How many bots should be playing?']:
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)
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)
if input.isdigit():
if int(question_possible_asnwers[question_no].split(" - ")[0]) <= int(input) <= int(question_possible_asnwers[question_no].split(" - ")[1]):
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[1] = f"{'0' if config[1] == '5' else '1'} -\
{5 - int(config[1])}"
return config
def starting_screen(screen:Matrix, players:list):
'''
Prints the starting screen
input:
- screen: Matrix
screen to print on
- players: list
list of the players
'''
screen.refresh()
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_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:")
for i in range(len(players)):
screen.set_string_center(int(Terminal.get_lines() /2 - 3 + i), players[i].get_name())
screen.set_string_center(int(Terminal.get_lines() /2 - 3 + i),
players[i].get_name())
screen.print()
def add_bot_font(screen:Matrix, bot:int):
'''
Adds the bot font to the screen
input:
- screen: Matrix
screen to print on
- bot: int
bot number
'''
line_1 = "$$$$$$$\ $$\ "
line_2 = "$$ __$$\ $$ | "
line_3 = "$$ | $$ | $$$$$$\ $$$$$$\ "
@ -120,12 +195,32 @@ def add_bot_font(screen:Matrix, bot:int):
screen.set_string_center(9, line_6 + " $$ |")
screen.set_string_center(10, line_7 + " $$ |")
screen.set_string_center(11, line_8 + " \__|")
elif bot == 5:
screen.set_string_center(4, line_1 + "$$$$$$$\ ")
screen.set_string_center(5, line_2 + "$$ ____| ")
screen.set_string_center(6, line_3 + "$$ | ")
screen.set_string_center(7, line_4 + "$$$$$$$\ ")
screen.set_string_center(8, line_5 + "\_____$$\ ")
screen.set_string_center(9, line_6 + "$$\ $$ |")
screen.set_string_center(10, line_7 + "\$$$$$$ |")
screen.set_string_center(11, line_8 + " \______/ ")
screen.print()
def draw_player_cards(screen:Matrix, cards:list, players = None):
'''
Draws the cards of the players
input:
- screen: Matrix
screen to print on
- cards: list
list of the cards
- players: list
list of the players
'''
card1 = "╔═══╗"
card2 = "║ X ║"
card3 = "║ X ║"
@ -150,7 +245,7 @@ def draw_player_cards(screen:Matrix, cards:list, players = None):
card2 = "║ ♦ ║"
elif card.get("suit") == "Clubs":
card2 = "║ ♣ ║"
if card.get("rank") == "Ace":
card3 = "║ A ║"
elif card.get("rank") == "Jack":
@ -163,8 +258,9 @@ def draw_player_cards(screen:Matrix, cards:list, players = None):
card3 = "║10 ║"
else:
card3 = f"{card.get('rank')}"
if players != None:
screen.set_string(3 + x, y - 1, " " + players[i].get_name() + ": " + str(players[i].get_points()) + " points")
if players is not 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)
@ -172,51 +268,133 @@ def draw_player_cards(screen:Matrix, cards:list, players = None):
screen.set_string(3 + x, y + 2, card3)
screen.set_string(3 + x, y + 3, card4)
if players != None:
if players is not None:
x += 16
x += 6
def player_interface(screen:Matrix, player:PLAYER, round_no:int, stich:int, trumpf:str, trumpf_sym:str):
def player_interface(screen:Matrix, player:PLAYER, round_no:int, stich:int,
trumpf:str, trumpf_sym:str):
'''
Draws the interface for the player
input:
- screen: Matrix
screen to print on
- player: PLAYER
player object
- round_no: int
number of the round
- stich: int
number of the stich
- trumpf: str
trumpf of the round
- trumpf_sym: str
symbol of the trumpf
'''
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} 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()}")
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", "[1 - " + str(len(player.get_cards())) + "]", screen, fullscreen=True)
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, stich:int):
'''
Draws the interface for the bot
input:
- screen: Matrix
screen to print on
- bot: BOT
bot object
- round_no: int
number of the round
- stich: int
number of the stich
'''
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}, stich NO. {stich}")
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()
def winner_screen(screen, winner, players:list):
'''
Draws the screen for the winner
input:
- screen: Matrix
screen to print on
- winner: PLAYER
player object
- players: list
list of all players
'''
screen.refresh()
screen.set_frame(int(Terminal.get_columns() / 2 - 25), int(Terminal.get_lines() / 2 - 10), 50, 20, rounded=True, title=f"Player '{winner.get_name()}' won the game!")
screen.set_frame(int(Terminal.get_columns() / 2 - 25),
int(Terminal.get_lines() / 2 - 10), 50, 20, rounded=True,
title=f"Player '{winner.get_name()}' won the game!")
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):
'''
Draws the screen for the trumpf
input:
- screen: Matrix
screen to print on
- trumpf: str
trumpf of the round
'''
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_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):
def stich_win_screen(screen:Matrix, winner, players:list, cards:list,
trumpf:str, trumpf_sym:str):
'''
Draws the screen for the stich winner
input:
- screen: Matrix
screen to print on
- winner: PLAYER
player object
- players: list
list of all players
- cards: list
list of all cards
- trumpf: str
trumpf of the round
- trumpf_sym: str
symbol of the trumpf
'''
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_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(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)
screen.set_string(Terminal.get_columns() - len(trumpf + " ") - 2,
2, "Trumpf: " + trumpf + " " + trumpf_sym)
draw_player_cards(screen, cards, players)
screen.print()
input()