Last active
June 28, 2023 12:23
-
-
Save ekaitz-zarraga/6e20ffd75f46b6aef0673dcab6fa1ba3 to your computer and use it in GitHub Desktop.
A shitty tictactoe minimax implementation that doesn't actually work yet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_board(): | |
board = {} | |
for i in ["a","b","c"]: | |
for j in ["1","2","3"]: | |
board[i+j] = " " | |
return board | |
def ganador(board): | |
if board["a1"] == board["b1"] == board["c1"] and board["a1"] != " ": | |
return board["a1"] | |
if board["a1"] == board["a2"] == board["a3"] and board["a1"] != " ": | |
return board["a1"] | |
if board["a1"] == board["b2"] == board["c3"] and board["a1"] != " ": | |
return board["a1"] | |
if board["a3"] == board["b2"] == board["c1"] and board["c1"] != " ": | |
return board["c1"] | |
if board["b1"] == board["b2"] == board["b3"] and board["b1"] != " ": | |
return board["b1"] | |
if board["c1"] == board["c2"] == board["c3"] and board["c1"] != " ": | |
return board["c1"] | |
if board["a2"] == board["b2"] == board["c2"] and board["a2"] != " ": | |
return board["a2"] | |
if board["a3"] == board["b3"] == board["c3"] and board["a3"] != " ": | |
return board["a3"] | |
return | |
def terminado(board): | |
return ganador(board) is not None or len(huecos(board)) == 0 | |
def pedir(jug): | |
return input("Jugador " + jug + " introduce coordenada> ") | |
def huecos(board): | |
lhuecos = [] | |
for i in board: | |
if board[i] == " ": | |
lhuecos.append(i) | |
return lhuecos | |
def create_node(board, jug, coords): | |
return { | |
"board": board, | |
"jug": jug, | |
"coords": coords, | |
"children": [], | |
"score": None | |
} | |
def create_mov_tree(board, jugs, turn, me): | |
jug_actual = jugs[turn%2] | |
tree = create_node(board.copy(), jug_actual, "") | |
pendientes = {turn: [tree]} | |
while True: | |
turn += 1 | |
pendientes_este_turno = pendientes[turn-1] | |
pendientes[turn] = [] | |
for actual in pendientes_este_turno: | |
nuevos_nodos = [] | |
if ganador(actual["board"]): | |
continue | |
for i in huecos(actual["board"]): | |
tablero_nuevo = actual["board"].copy() | |
jug_anterior = jugs[(turn-1)%2] | |
jug_actual = jugs[turn%2] | |
tablero_nuevo[i] = jug_anterior | |
nuevos_nodos.append( create_node(tablero_nuevo, jug_actual, i) ) | |
actual["children"] = nuevos_nodos.copy() | |
pendientes[turn].extend(actual["children"]) | |
if pendientes[turn] == []: | |
break | |
for level in sorted(pendientes.keys(), reverse=True): | |
for move in pendientes[level]: | |
if len(move["children"]) == 0: | |
winner = ganador(move["board"]) | |
if winner is None: | |
move["score"] = 0 | |
elif winner == me: | |
move["score"] = 1 | |
else: | |
move["score"] = -1 | |
else: | |
if move["jug"] == me: | |
mejor_hijo = max(move["children"], key= lambda x: x["score"]) | |
move["score"] = mejor_hijo["score"] | |
else: | |
peor_hijo = min(move["children"], key= lambda x: x["score"]) | |
move["score"] = peor_hijo["score"] | |
return tree | |
def move_tree(tree, board): | |
for i in tree["children"]: | |
if i["board"] == board: | |
return i | |
def select_move(tree): | |
return max(tree["children"], key= lambda x: x["score"])["coords"] | |
def jugar_solo(): | |
board = create_board() | |
jug1 = "o" | |
jug2 = "x" | |
jugs = (jug1, jug2) | |
turn = 0 | |
while not terminado(board): | |
jug_actual = jugs[turn%2] | |
print(board) ## XXX: Hay que pintarlo bien | |
coord = pedir(jug_actual) | |
while coord not in board or board[coord] != " ": | |
coord = pedir(jug_actual) | |
board[coord] = jug_actual | |
turn += 1 | |
def pintar_tablero(board): | |
print(" a | b | c ") | |
print( " " + board["a1"] + " | " + board["b1"] + " | " + board["c1"] ) | |
print("---+---+---") | |
print( " " + board["a2"] + " | " + board["b2"] + " | " + board["c2"] ) | |
print("---+---+---") | |
print( " " + board["a3"] + " | " + board["b3"] + " | " + board["c3"] ) | |
def jugar_acompañado(): | |
board = create_board() | |
jug1 = "o" | |
jug2 = "x" | |
jugs = (jug1, jug2) | |
turn = 0 | |
tree = create_mov_tree(board, jugs, turn, jug2) # AI | |
while not terminado(board): | |
jug_actual = jugs[turn%2] | |
pintar_tablero(board) | |
if jug_actual == jug2: | |
coord = select_move(tree) # AI | |
else: | |
coord = pedir(jug_actual) | |
while coord not in board or board[coord] != " ": | |
coord = pedir(jug_actual) | |
board[coord] = jug_actual | |
tree = move_tree(tree, board) # AI | |
turn += 1 | |
jugar_acompañado() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment