Check winner tic-tac-toe

posted Feb 3, 2017, 7:43 AM by Attila Lengyel
def drawBoard(board):
  print("    |   |   ")
  print(" ",board[0],"|",board[1],"|",board[2])
  print("    |   |   ")  
  print("-------------")
  print("    |   |   ")
  print(" ",board[3],"|",board[4],"|",board[5])
  print("    |   |   ")  
  print("-------------")
  print("    |   |   ")
  print(" ",board[6],"|",board[7],"|",board[8])
  print("    |   |   ")  

def win(board, player):
  if board[0] == player and board[1] == player and board[2] == player:
    return True
  if board[3] == player and board[4] == player and board[5] == player:
    return True
  if board[6] == player and board[7] == player and board[8] == player:
    return True
  if board[0] == player and board[3] == player and board[6] == player:
    return True
  if board[1] == player and board[4] == player and board[7] == player:
    return True
  if board[2] == player and board[5] == player and board[8] == player:
    return True
  if board[0] == player and board[4] == player and board[8] == player:
    return True
  if board[2] == player and board[4] == player and board[6] == player:
    return True
  
  return False
  
board = [" "] * 9
drawBoard(board)

user = input("X or O?:")
if user != "O" and user != "X":
  print("You will be X")
  user = "X"

move = input("Make your move (1~9):")
board[int(move) - 1] = user

drawBoard(board)


Comments