Tictactoe
Tictactoe
Tictactoe
import numpy as np
import random
def create_board():
return(np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]))
def possibilities(board):
l = []
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == 0:
l.append((i, j))
return(l)
selection = possibilities(board)
current_loc = random.choice(selection)
board[current_loc] = player
return(board)
for x in range(len(board)):
win = True
for y in range(len(board)):
if board[x, y] != player:
win = False
continue
if win == True:
return(win)
return(win)
for x in range(len(board)):
win = True
for y in range(len(board)):
if board[y][x] != player:
win = False
continue
if win == True:
return(win)
return(win)
win = True
y=0
for x in range(len(board)):
if board[x, x] != player:
win = False
if win:
return win
win = True
if win:
for x in range(len(board)):
y = len(board) - 1 - x
if board[x, y] != player:
win = False
return win
# Evaluates whether there is
# a winner or a tie
def evaluate(board):
winner = 0
if (row_win(board, player) or
col_win(board, player) or
diag_win(board, player)):
winner = player
winner = -1
return winner
def play_game():
print(board)
sleep(2)
while winner == 0:
print(board)
sleep(2)
counter += 1
winner = evaluate(board)
if winner != 0:
break
return(winner)
# Driver Code
###########################Output###########################
# [[0 0 0]
# [0 0 0]
# [0 0 0]]
# [[0 0 0]
# [0 0 0]
# [0 0 1]]
# [[0 0 0]
# [0 0 0]
# [2 0 1]]
# [[0 0 0]
# [0 0 1]
# [2 0 1]]
# [[0 0 0]
# [2 0 1]
# [2 0 1]]
# [[0 0 0]
# [2 1 1]
# [2 0 1]]
# [[0 0 0]
# [2 1 1]
# [2 2 1]]
# [[0 1 0]
# [2 1 1]
# [2 2 1]]
# [[2 1 0]
# [2 1 1]
# [2 2 1]]
# Winner is: 2