final touchups (i.e. removing useless comments)
This commit is contained in:
@@ -11,6 +11,3 @@ class Playerhandler:
|
||||
self.player1_char = "X"
|
||||
self.player2_char = "O"
|
||||
self.ai = False
|
||||
'''
|
||||
def getPlayer(self):
|
||||
return'''
|
||||
@@ -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.")
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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()
|
||||
|
||||
13
tictactoe.py
13
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()
|
||||
|
||||
Reference in New Issue
Block a user