From e98d586445c9854b8ff6f8e783cb09a588b2a001 Mon Sep 17 00:00:00 2001 From: Rh17S15 Date: Mon, 22 Sep 2025 19:43:52 +0200 Subject: [PATCH] final touchups (i.e. removing useless comments) --- playerhandler.py | 3 --- printer.py | 2 -- tests/test_minimax.py | 17 +++++++++++++++++ tests/test_model.py | 5 +++++ tictactoe.py | 13 +------------ 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/playerhandler.py b/playerhandler.py index 2440468..ae2bf82 100644 --- a/playerhandler.py +++ b/playerhandler.py @@ -11,6 +11,3 @@ class Playerhandler: self.player1_char = "X" self.player2_char = "O" self.ai = False - ''' - def getPlayer(self): - return''' \ No newline at end of file diff --git a/printer.py b/printer.py index ab06d8c..707a31e 100644 --- a/printer.py +++ b/printer.py @@ -64,8 +64,6 @@ class Printer: def invalidai(self): print("That was not a valid input. Please try again. ") - # return 0 - # return input("That was not a valid input. Type 1/2 if you want to play against AI or local multiplayer: ") def startsplaying(self, player1, player1_char, player2_char): print(f"Player {1 if player1 else 2} ({player1_char if player1 else player2_char}) will start playing.") diff --git a/tests/test_minimax.py b/tests/test_minimax.py index 4c4fcf9..bb101f3 100644 --- a/tests/test_minimax.py +++ b/tests/test_minimax.py @@ -23,6 +23,23 @@ class TestMinimax(unittest.TestCase): self.assertEqual(x, expected_x) # Check if the x-coordinate of the best move matches the expected value self.assertEqual(y, expected_y) # Check if the y-coordinate of the best move matches the expected value + current_field = [["X", "|", "O", "|", "X"], + ["—", "+", "—", "+", "—"], + [" ", "|", "O", "|", ""], + ["—", "+", "—", "+", "—"], + ["O", "|", " ", "|", "X"]] # Set the current field state + player1_char = 'X' + player2_char = 'O' + + score, x, y = self.minimax.minimax(current_field, True, "X", "O") # Call the minimax method + expected_score = 1 # Adjust the expected score based on the game rules and the field state + expected_x = 2 # Adjust the expected x-coordinate of the best move + expected_y = 4 # Adjust the expected y-coordinate of the best move + + self.assertEqual(score, expected_score) # Check if the score matches the expected value + self.assertEqual(x, expected_x) # Check if the x-coordinate of the best move matches the expected value + self.assertEqual(y, expected_y) # Check if the y-coordinate of the best move matches the expected value + def test_check_win(self) -> None: player1_char = 'X' player2_char = 'O' diff --git a/tests/test_model.py b/tests/test_model.py index 3802ca3..9fd83f2 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -95,3 +95,8 @@ class TestModel(unittest.TestCase): if __name__ == '__main__': unittest.main() + f = open("savestate.py", "r") # Read the content of the savestate file + f.write("save = [['X', '|', ' ', '|', ' '], " + "['—', '+', '—', '+', '—'], [' ', '|', ' ', '|', ' '], " + "['—', '+', '—', '+', '—'], [' ', '|', ' ', '|', ' ']]") + f.close() diff --git a/tictactoe.py b/tictactoe.py index 99b3a30..4481b5e 100644 --- a/tictactoe.py +++ b/tictactoe.py @@ -28,20 +28,17 @@ class Tictactoe: self.model.do_move(best_move[1] // 2, best_move[2] // 2, self.playerhandler.player1, self.playerhandler.player1_char, self.playerhandler.player2_char) - # self.printer.printfield(self.model.field) else: while True: nextmove = self.printer.playermove(self.playerhandler.player1, self.playerhandler.player1_char, self.playerhandler.player2_char) next_move_x, next_move_y = nextmove - # return self.model.check_move(next_move_x, next_move_y, True) if self.model.check_move(next_move_x, next_move_y, True): self.model.do_move(next_move_x, next_move_y, self.playerhandler.player1, self.playerhandler.player1_char, self.playerhandler.player2_char) break - # self.playerhandler.player1 = not self.playerhandler.player1 else: self.printer.occupied() self.printer.printfield(self.model.field) @@ -60,10 +57,8 @@ class Tictactoe: self.printer.printfield(self.model.field) self.printer.endmessage(win[0], win[1], self.playerhandler.ai) exit() - # self.turn() def check_win(self, ifield, player1_char, player2_char): - # nothing 0, 0; draw 1, 1; win player1 1, 0; win player2 0, 1 for j in range(0, len(ifield), 2): if ifield[j][0] == ifield[j][2] == ifield[j][4] == player1_char: return 1, 0 @@ -87,7 +82,6 @@ class Tictactoe: else: return 0, 0 - # verbose = True def main(self): inp = None @@ -99,7 +93,7 @@ class Tictactoe: break if inp == "y": - self.model.loadsavestate() # self.tictactoe.checksavestateplayer() + self.model.loadsavestate() spacecount = 0 for j in range(0, len(self.model.field)): for k in range(0, len(self.model.field)): @@ -108,11 +102,8 @@ class Tictactoe: if spacecount % 2 == 0: self.playerhandler.player1 = False break - # self.printer.checkai() - # inp = self.printer.invalidsavestate() - # inp = self.printer.checkai() while True: checkai = self.printer.checkai() if checkai == 1: @@ -123,9 +114,7 @@ class Tictactoe: elif checkai == 3: self.printer.invalidai() - # checkai = self.printer.invalidai() - # printer.printfield(self.model.field) self.printer.startsplaying(self.playerhandler.player1, self.playerhandler.player1_char, self.playerhandler.player2_char) self.turn()