# 2. Tic-tac-toe
size = int(input("Size--> "))
board = list()
moves = list()
# initalize the board
for i in range(size):
row = []
for j in range(size):
row.append(str(i * size + j))
board.append(row)
def displayBoard():
for row in board:
for j in row:
print(j.rjust(2), end=' ')
print()
def win():
for move in moves:
# check the row
counter = 0
for i in range(size):
if board[move // size][i] == 'X':
counter += 1
if counter == size:
return True
#check the column
counter = 0
for i in range(size):
if board[i][move % size] == 'X':
counter += 1
if counter == size:
return True
# check the first diagonal
counter = 0
for i in range(size):
if board[i][i] == 'X':
counter += 1
if counter == size:
return True
# check another diagonal
counter = 0
for i in range(size):
if board[i][size - 1 - i] == 'X':
counter += 1
if counter == size:
return True
def inputX():
x = int(input("X--> "))
if board[x // size][x % size] != 'X':
board[x // size][x % size] = 'X'
moves.append(x)
while True:
displayBoard()
inputX()
if win():
displayBoard()
print("You win!")
break