-1
Good afternoon, people. This is a code I saw that plays an old game. Could someone please explain to me how the Loop function evaluate and play_game is working? Specifically that part for player in [1, 2]:
. Thank you very much.
def create_board():
board = np.zeros((3,3), dtype=int)
return board
def place(board, player, position):
if board[position] == 0:
board[position] = player
return (board)
def possibilities(board):
return list(zip(*np.where(board == 0)))
def random_place(board, player):
escolhe = random.choice(possibilities(board))
return place(board,player,escolhe)
def row_win(board, player):
return np.any(np.all(board==player,axis=1))
def col_win(board, player):
return np.any(np.all(board==player,axis=0))
def diag_win(board, player):
if np.any(np.all(np.diagonal(board)==player)) or np.any(np.all(np.fliplr(board).diagonal() == player)):
return True
else:
return False
def evaluate(board):
winner = 0
for player in [1, 2]:
if row_win(board, player) or col_win(board, player) or diag_win(board, player):
winner = player
if np.all(board != 0) and winner == 0:
winner = -1
return winner
def play_game():
board = create_board()
winner = 0
while winner == 0:
for player in [1, 2]:
random_place(board, player)
winner = evaluate(board)
if winner != 0:
break
return winner