Tic Tac Toe umbeatable AI megoldás?
import random
def get_umbeatable_ai_coordinates(board, current_player):
dot_count =0
for row in board:
dot_count += row.count(".")
if dot_count == 0:
return None
while True:
numbers =[0,1,2]
result = (random.choice(numbers),random.choice(numbers)) #random kiválasztani egyet
row_index = 0
for row in board:
coll_index = 0
for coll in row:
if (row_index,coll_index)==result:
if board[row_index][coll_index]==".":
return result
coll_index += 1
row_index +=1
"""
Should return a tuple of 2 numbers.
Each number should be between 0-2.
The chosen number should be only a free coordinate from the board.
The chosen coordinate should always stop the other player from winning or
maximize the current player's chances to win.
If the board is full (all spots taken by either X or O) than "None"
should be returned.
"""
pass
if __name__ == "__main__":
# run this file to test you have implemented correctly the function
board_1 = [
[".", "O", "."],
["X", "O", "."],
["X", "X", "O"],
]
print(get_umbeatable_ai_coordinates(board_1, "X")) # the printed coordinate should always be (0, 0)
board_2 = [
["X", "O", "."],
["X", ".", "."],
["O", "O", "X"],
]
print(get_umbeatable_ai_coordinates(board_2, "O")) # the printed coordinate should always be (1, 1)
board_3 = [
["O", "O", "."],
["O", "X", "."],
[".", "X", "."],
]
print(get_umbeatable_ai_coordinates(board_3, "X")) # the printed coordinate should be either (0, 2) or (2, 0)
a kód mükődik csak nem a feladat szerint.
Kapcsolódó kérdések:
Minden jog fenntartva © 2025, www.gyakorikerdesek.hu
GYIK | Szabályzat | Jogi nyilatkozat | Adatvédelem | Cookie beállítások | WebMinute Kft. | Facebook | Kapcsolat: info(kukac)gyakorikerdesek.hu
Ha kifogással szeretne élni valamely tartalommal kapcsolatban, kérjük jelezze e-mailes elérhetőségünkön!