From ff2cde4f32de791c737d7b0a5598a5cdd42a1c8c Mon Sep 17 00:00:00 2001 From: Hong-Phuc Bui <hong-phuc.bui@htwsaar.de> Date: Tue, 13 May 2025 16:08:17 +0200 Subject: [PATCH] ue 4 --- ub-3/tictactoe.test.py | 1 /dev/null | 80 -------------------- ub-3/tictaktoe.py | 120 ++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 80 deletions(-) diff --git a/ub-3/tictactoe.test.py b/ub-3/tictactoe.test.py new file mode 100644 index 0000000..9c37cf9 --- /dev/null +++ b/ub-3/tictactoe.test.py @@ -0,0 +1 @@ +import diff --git a/ub-3/tictaktoe.py b/ub-3/tictaktoe.py new file mode 100644 index 0000000..c809e3b --- /dev/null +++ b/ub-3/tictaktoe.py @@ -0,0 +1,120 @@ +EMPTY = 0 +PL1 = 1 +PL2 = 2 + +tafel = [ + [EMPTY, EMPTY, EMPTY], + [EMPTY, EMPTY, EMPTY], + [EMPTY, EMPTY, EMPTY], +] + +COLUMNS = 3 +ROWS = 3 + +def print_tafel(board): + symbols = ("*", "X", "O") + for zeile in board: + for c in zeile: + print(f"{symbols[c]} ",end="") + print() + +def input_ok(tafel, r, c): + is_in_tafel = (ROWS > r >=0) and (COLUMNS > c >= 0) + if is_in_tafel: + beleg = tafel[r][c] + return beleg == EMPTY + else: + return False + + +def user_input(user,tafel): + print(f"Spieler {user} ist dran") + print("Geben Sie die Zeile und Spalten ein") + user_input_ok = False + while not user_input_ok: + r = int(input("Zeile: ")) - 1 + c = int(input("Spalte: ")) - 1 + user_input_ok = input_ok(tafel, r, c) + if user_input_ok: + tafel[r][c] = user + else: + print("Eingabe nicht in Ordnung") + + +def pruefe_comination(p, board): + r = p[0] + c = p[1] + combination = p[2] + player = board[r][c] + if combination == "hvd": + h = (board[r][0] == player) and (board[r][1] == player) and (board[r][2] == player) + v = (board[0][c] == player) and (board[1][c] == player) and (board[2][c] == player) + d = (board[0][0] == player) and (board[1][1] == player) and (board[2][2] == player) + return h or v or d + elif combination == "v": + return (board[0][c] == player) and (board[1][c] == player) and (board[2][c] == player) + elif combination == "vd": + v = (board[0][c] == player) and (board[1][c] == player) and (board[2][c] == player) + d = (board[0][0] == player) and (board[1][1] == player) and (board[2][2] == player) + return v or d + elif combination == "h": + return (board[r][0] == player) and (board[r][1] == player) and (board[r][2] == player) + else: + raise Error(f"combination {combination} ungültig" ) + + +def has_spieler_gewonnen(player, board): + position = [ + (0,0, "hvd"), (0,1, "v"), (0,2,"vd"), (1,0, "h"), (2,0,"h") + ] + for p in position: + r = p[0] + c = p[1] + if board[r][c] == player: + return pruefe_comination(p, board) + else: + return False + + + +def spiel_bewerten(tafel): + """ + return: Ein Tupel + 1. Element: True = Spiel beendet + False = Spiel geht weiter + 2. Element: 0 = unentschieden + 1 = 1. Spieler gewonnen + 2 = 2. Spieler gewonnen + """ + if has_spieler_gewonnen(PL1, tafel): + return (True, PL1) + elif has_spieler_gewonnen(PL2, tafel): + return (True, PL2) + + empty_cells = ROWS * COLUMNS + for row in tafel: + for colum in row: + if colum != EMPTY: + empty_cells = empty_cells - 1 + return (empty_cells == 0, 0) + + +print_tafel(tafel) +aktuelle_spieler = 1 + +(spiel_ende, gewinner) = (False, 0) + +while not spiel_ende : + (spiel_ende, gewinner) = spiel_bewerten(tafel) + user_input(aktuelle_spieler, tafel) + print_tafel(tafel) + if aktuelle_spieler == 1: + aktuelle_spieler = 2 + else: + aktuelle_spieler = 1 + +print("Spiel beendet!") + + + + diff --git a/ub-3/tik-tak-toe.py b/ub-3/tik-tak-toe.py deleted file mode 100644 index ec019f5..0000000 --- a/ub-3/tik-tak-toe.py +++ /dev/null @@ -1,80 +0,0 @@ -EMPTY = 0 -PL1 = 1 -PL2 = 2 - -tafel = [ - [EMPTY, EMPTY, EMPTY], - [EMPTY, EMPTY, EMPTY], - [EMPTY, EMPTY, EMPTY], -] - -COLUMNS = 3 -ROWS = 3 - -def print_tafel(board): - symbols = ("*", "X", "O") - for zeile in board: - for c in zeile: - print(f"{symbols[c]} ",end="") - print() - -def input_ok(tafel, r, c): - is_in_tafel = (ROWS > r >=0) and (COLUMNS > c >= 0) - if is_in_tafel: - beleg = tafel[r][c] - return beleg == EMPTY - else: - return False - - -def user_input(user,tafel): - print(f"Spieler {user} ist dran") - print("Geben Sie die Zeile und Spalten ein") - user_input_ok = False - while not user_input_ok: - r = int(input("Zeile: ")) - 1 - c = int(input("Spalte: ")) - 1 - user_input_ok = input_ok(tafel, r, c) - if user_input_ok: - tafel[r][c] = user - else: - print("Eingabe nicht in Ordnung") - - -def spiel_bewerten(tafel): - """ - return: Ein Tupel - 1. Element: True = Spiel beendet - False = Spiel geht weiter - 2. Element: 0 = unentschieden - 1 = 1. Spieler gewonnen - 2 = 2. Spieler gewonnen - """ - - empty_cells = ROWS * COLUMNS - for row in tafel: - for colum in row: - if colum != EMPTY: - empty_cells = empty_cells - 1 - return (empty_cells == 0, 0) - - -print_tafel(tafel) -aktuelle_spieler = 1 - -(spiel_ende, gewinner) = (False, 0) - -while not spiel_ende : - (spiel_ende, gewinner) = spiel_bewerten(tafel) - user_input(aktuelle_spieler, tafel) - print_tafel(tafel) - if aktuelle_spieler == 1: - aktuelle_spieler = 2 - else: - aktuelle_spieler = 1 - -print("Spiel beendet!") - - - - -- Gitblit v1.10.0-SNAPSHOT