First Commit

This commit is contained in:
2025-09-16 15:00:16 +02:00
commit c8980f785f
188 changed files with 43407 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
from minimax import aiO, aiX, Terminal, Value
# from player import player
def display_board(board):
print('-------------')
for row in [board[i:i + 3] for i in range(0, 9, 3)]:
print(f'| {row[0]} | {row[1]} | {row[2]} |')
print('-------------')
def main():
results = []
while True:
board = [" " for _ in range(9)]
# board = ['O', 'X', 'O','O', 'X', 'X',' ', ' ', 'X']
display_board(board)
while True:
board[aiX(board)] = "X"
display_board(board)
if Terminal(board):
break
board[aiO(board)] = "O"
display_board(board)
if Terminal(board):
break
results.append(Value(board))
if __name__ == '__main__':
main()
+93
View File
@@ -0,0 +1,93 @@
import random
win_states = [
(0, 1, 2), (3, 4, 5), (6, 7, 8), # horizontal
(0, 3, 6), (1, 4, 7), (2, 5, 8), # vertical
(0, 4, 8), (2, 4, 6) # diagonal
]
def Terminal(state: list):
for win_state in win_states:
if all(state[i] == "O" for i in win_state) or all(state[i] == "X" for i in win_state):
return True
if state.count(" ") == 0:
return True
else:
return False
def Value(state):
for win_state in win_states:
if all(state[i] == "O" for i in win_state):
return -1
elif all(state[i] == "X" for i in win_state):
return 1
if state.count(" ") == 0:
return 0
def Player(state: list):
if state.count("X") == state.count("O"):
return "X"
else:
return "O"
def Actions(state):
indexes = []
for i in range(9):
if state[i] == " ":
indexes.append(i)
return indexes
def Result(state, index, player):
state[index] = player
return state
def Minimax(state):
if Terminal(state):
return Value(state)
if Player(state) == "X":
value = -9999
for a in Actions(state):
value = max(value, Minimax(Result(state.copy(), a, "X")))
return value
elif Player(state) == "O":
value = 9999
for a in Actions(state):
value = min(value, Minimax(Result(state.copy(), a, "O")))
return value
def aiO(state):
best_score = float('inf')
best_move = None
for move in Actions(state):
state[move] = 'O'
score = Minimax(state)
state[move] = ' '
if score < best_score:
best_score = score
best_move = move
return best_move
def aiX(state):
best_score = float('-inf')
best_move = None
for move in Actions(state):
state[move] = 'X'
score = Minimax(state)
state[move] = ' '
if score > best_score:
best_score = score
best_move = move
return best_move
def aiRandom(state):
return random.choice(Actions(state))
+10
View File
@@ -0,0 +1,10 @@
from minimax import Actions
def player(state):
actions = Actions(state)
choice = -1
while choice not in actions:
choice = int(input("Pick a spot between 1-9.> ")) - 1
return choice
+68
View File
@@ -0,0 +1,68 @@
from random import randint
"""def player(start_board):
player_choice = input("Pick a place: ")
if start_board.count(player_choice) == 1:
start_board[start_board.index(player_choice)] = "X"
else:
player(start_board)"""
def player(start_board):
com_choice = str(randint(1, 9))
if start_board.count(com_choice) == 1:
start_board[start_board.index(com_choice)] = "X"
else:
player(start_board)
def com(start_board):
com_choice = str(randint(1, 9))
if start_board.count(com_choice) == 1:
start_board[start_board.index(com_choice)] = "O"
else:
com(start_board)
def print_board(s_board):
board = "".join(s_board)
print(board)
def check(s_b):
if s_b[0] == s_b[2] == s_b[4] == 'X' or s_b[6] == s_b[8] == s_b[10] == 'X' or s_b[12] == s_b[14] == s_b[16] == 'X' \
or s_b[0] == s_b[6] == s_b[12] == 'X' or s_b[2] == s_b[8] == s_b[14] == 'X' or s_b[4] == s_b[10] == s_b[16] \
== 'X' or s_b[0] == s_b[8] == s_b[16] == 'X' or s_b[4] == s_b[8] == s_b[12] == 'X':
print("you won")
main()
elif s_b[0] == s_b[2] == s_b[4] == 'O' or s_b[6] == s_b[8] == s_b[10] == 'O' or s_b[12] == s_b[14] == s_b[16] == 'O' \
or s_b[0] == s_b[6] == s_b[12] == 'O' or s_b[2] == s_b[8] == s_b[14] == 'O' or s_b[4] == s_b[10] == s_b[16] \
== 'O' or s_b[0] == s_b[8] == s_b[16] == 'O' or s_b[4] == s_b[8] == s_b[12] == 'O':
print('computer won')
print_board(s_b)
main()
def main():
# main program
start_board = ["1", "|", "2", "|", "3", "\n",
"4", "|", "5", "|", "6", "\n",
"7", "|", "8", "|", "9", "\n", ]
print_board(start_board)
while 1:
try:
player(start_board)
check(start_board)
com(start_board)
check(start_board)
print_board(start_board)
except RecursionError:
print_board(start_board)
print("draw")
main()
if __name__ == "__main__":
main()