First Commit
This commit is contained in:
Executable
+30
@@ -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()
|
||||
Executable
+93
@@ -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))
|
||||
Executable
+10
@@ -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
|
||||
Reference in New Issue
Block a user