2025-09-22 19:43:51 +02:00
|
|
|
# first two player tictactoe then one player against ai
|
|
|
|
|
# Ich hab jetzt einfach mal ohne OOP(bzw MVC/MVP) angefangen.
|
|
|
|
|
# Ich denke es ist einfacher es erst so zu machen und dann einzuteilen.
|
2025-09-22 19:43:52 +02:00
|
|
|
import os.path
|
2025-09-22 19:43:52 +02:00
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
if os.path.isfile('./savestate.py'):
|
|
|
|
|
from savestate import save
|
2025-09-22 19:43:51 +02:00
|
|
|
|
|
|
|
|
field = [[" ", "|", " ", "|", " "],
|
|
|
|
|
["—", "+", "—", "+", "—"],
|
|
|
|
|
[" ", "|", " ", "|", " "],
|
|
|
|
|
["—", "+", "—", "+", "—"],
|
|
|
|
|
[" ", "|", " ", "|", " "]]
|
|
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
player1 = True # bool that states which player is currently playing
|
|
|
|
|
player1_char = "X"
|
|
|
|
|
player2_char = "O"
|
2025-09-22 19:43:52 +02:00
|
|
|
draw = False
|
2025-09-22 19:43:52 +02:00
|
|
|
|
2025-09-22 19:43:51 +02:00
|
|
|
|
|
|
|
|
def printer(): # function that prints the field
|
|
|
|
|
|
|
|
|
|
print(" 0 1 2", end="\n")
|
|
|
|
|
for j in range(0, len(field)):
|
|
|
|
|
print("0" if j == 0 else "1" if j == 2 else "2" if j == 4 else " ", end=" ")
|
|
|
|
|
for k in range(0, len(field)):
|
|
|
|
|
print(str(field[j][k]), end=" ")
|
|
|
|
|
print("\n", end="")
|
|
|
|
|
|
|
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
def turn(): # function that checks whose turn it is and where they can place their symbol
|
2025-09-22 19:43:51 +02:00
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
while True:
|
|
|
|
|
next_move_x = int(input(
|
2025-09-22 19:43:52 +02:00
|
|
|
f"Player {1 if player1 else 2} ({player1_char if player1 else player2_char}), please choose the x ("
|
|
|
|
|
f"horizontal) coordinates of your next move: "))
|
2025-09-22 19:43:52 +02:00
|
|
|
if next_move_x in list(range(3)):
|
2025-09-22 19:43:52 +02:00
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
print("Please be sure to input a valid number (0-2). \nPlease try Again.")
|
2025-09-22 19:43:51 +02:00
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
while True:
|
|
|
|
|
next_move_y = int(input(
|
2025-09-22 19:43:52 +02:00
|
|
|
f"Player {1 if player1 else 2} ({player1_char if player1 else player2_char}), please choose the y ("
|
|
|
|
|
f"vertical) coordinates of your next move: "))
|
2025-09-22 19:43:52 +02:00
|
|
|
if next_move_y in list(range(3)):
|
2025-09-22 19:43:52 +02:00
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
print("Please be sure to input a valid number (0-2). \nPlease try Again.")
|
2025-09-22 19:43:51 +02:00
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
# it might be possible to compress the two above loops into one
|
2025-09-22 19:43:51 +02:00
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
return check_move(next_move_x, next_move_y)
|
2025-09-22 19:43:51 +02:00
|
|
|
|
|
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
def check_win():
|
2025-09-22 19:43:52 +02:00
|
|
|
global draw
|
2025-09-22 19:43:52 +02:00
|
|
|
for j in range(0, len(field), 2):
|
|
|
|
|
if field[j][0] == field[j][2] == field[j][4] == (player1_char if player1 else player2_char):
|
|
|
|
|
return True
|
|
|
|
|
elif field[0][j] == field[2][j] == field[4][j] == (player1_char if player1 else player2_char):
|
|
|
|
|
return True
|
|
|
|
|
if field[0][0] == field[2][2] == field[4][4] == (player1_char if player1 else player2_char):
|
|
|
|
|
return True
|
|
|
|
|
elif field[0][4] == field[2][2] == field[4][0] == (player1_char if player1 else player2_char):
|
|
|
|
|
return True
|
2025-09-22 19:43:52 +02:00
|
|
|
if all(field[j][k] != " " for j in range(len(field)) for k in range(len(field))):
|
|
|
|
|
# code to execute if all elements are not equal to " "
|
|
|
|
|
draw = True
|
|
|
|
|
return True
|
|
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
else:
|
|
|
|
|
return False
|
2025-09-22 19:43:52 +02:00
|
|
|
|
|
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
def do_move(x, y):
|
|
|
|
|
global player1 # Tells do_move to look for the global var player1
|
2025-09-22 19:43:52 +02:00
|
|
|
global draw
|
2025-09-22 19:43:52 +02:00
|
|
|
field[y][x] = (player1_char if player1 else player2_char)
|
2025-09-22 19:43:52 +02:00
|
|
|
printer()
|
2025-09-22 19:43:52 +02:00
|
|
|
if not check_win():
|
2025-09-22 19:43:52 +02:00
|
|
|
f = open("savestate.py", "w")
|
|
|
|
|
f.write("save = " + repr(field))
|
|
|
|
|
f.close()
|
2025-09-22 19:43:52 +02:00
|
|
|
player1 = not player1
|
|
|
|
|
turn()
|
2025-09-22 19:43:52 +02:00
|
|
|
elif check_win():
|
2025-09-22 19:43:52 +02:00
|
|
|
if draw:
|
2025-09-22 19:43:52 +02:00
|
|
|
print("Its a draw.")
|
|
|
|
|
else:
|
|
|
|
|
print(f"Player {1 if player1 else 2} won")
|
2025-09-22 19:43:52 +02:00
|
|
|
if os.path.exists("savestate.py"):
|
|
|
|
|
os.remove("savestate.py")
|
2025-09-22 19:43:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_move(x, y):
|
|
|
|
|
# translate x and y coordinates to field coordinates
|
|
|
|
|
x = int(x * 2)
|
|
|
|
|
y = int(y * 2)
|
2025-09-22 19:43:52 +02:00
|
|
|
if not field[y][x] == " ":
|
2025-09-22 19:43:52 +02:00
|
|
|
print("Your given coordinates are already occupied. \nPlease Try again.\n")
|
|
|
|
|
turn()
|
|
|
|
|
else:
|
|
|
|
|
do_move(x, y)
|
|
|
|
|
|
|
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
print("Welcome to Kat&Paul's TicTacToe:")
|
2025-09-22 19:43:52 +02:00
|
|
|
|
|
|
|
|
if os.path.isfile('./savestate.py'):
|
2025-09-22 19:43:52 +02:00
|
|
|
inp = input("An older savestate has been found. Do you want to continue it? (y/n): ")
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
if inp == "n":
|
|
|
|
|
break
|
2025-09-22 19:43:52 +02:00
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
if inp == "y":
|
|
|
|
|
field = save
|
2025-09-22 19:43:52 +02:00
|
|
|
spacecount = 0
|
|
|
|
|
for j in range(0, len(field)):
|
|
|
|
|
for k in range(0, len(field)):
|
|
|
|
|
if field[j][k] == " ":
|
|
|
|
|
spacecount += 1
|
|
|
|
|
# (player1 = True) if spacecount % 2 == 1 else (player1 = False)
|
|
|
|
|
if spacecount % 2 == 0:
|
|
|
|
|
player1 = False
|
|
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
break
|
2025-09-22 19:43:52 +02:00
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
inp = input("That was not a valid input. Type y/n if you want to continue or not: ")
|
2025-09-22 19:43:52 +02:00
|
|
|
|
2025-09-22 19:43:52 +02:00
|
|
|
printer()
|
2025-09-22 19:43:52 +02:00
|
|
|
print(f"Player {1 if player1 else 2} ({player1_char if player1 else player2_char}) will start playing.")
|
2025-09-22 19:43:52 +02:00
|
|
|
turn()
|