"""
Learning Goals:
 - Use the input command and try/except structures to handle direct user input in code
 - Implement and use helper functions in code to break up large problems into solvable subtasks
 - Install external modules with the pip command
 - Read documentation to learn how to use a new module
"""


name = input("What is your name?")
print("Hi,", name, "!")

###

try:
    age = int(input("What is your age?"))
    print("You'll be", age+1, "next year")
except:
    print("That's not a number!")

###

while True:
    try:
        num1 = int(input("Number 1:"))
        num2 = int(input("Number 2:"))
        break
    except:
        print("error")

result = num1 * num2
print(result)

###

# Construct the tic-tac-toe board
def makeNewBoard():
    board = []
    for row in range(3):
        # Add a new row to board
        board.append([".", ".", "."])
    return board

# Print the board as a 3x3 grid
def showBoard(board):
    for row in range(3):
        line = ""
        for col in range(3):
            line += board[row][col]
        print(line)

# Ask the user to input where they want
# to go next with row,col position
def takeTurn(board, player):
  while True:
    try:
      row = int(input("Enter a row for " + player + ":"))
      col = int(input("Enter a col for " + player + ":"))
      # Make sure its in the grid!
      if 0 <= row < 3 and 0 <= col < 3:
        if board[row][col] == ".":
          board[row][col] = player
          # stop looping when move is made
          break
        else:
          print("That space isn't open!")
      else:
        print("Not a valid space!")
    except:
      print("That's not a number!")
  return board

# Generate all horizontal lines
def horizLines(board):
  lines = []
  for row in range(3):
    lines.append(board[row][0] + board[row][1] + board[row][2])
  return lines

# Generate all vertical lines
def vertLines(board):
  lines = []
  for col in range(3):
    lines.append(board[0][col] + board[1][col] + board[2][col])
  return lines

# Generate both diagonal lines
def diagLines(board):
  leftDown = board[0][0] + board[1][1] + board[2][2]
  rightDown = board[0][2] + board[1][1] + board[2][0]
  return [ leftDown, rightDown ]

# Check if the board has no empty spots
def isFull(board):
    for row in range(3):
        for col in range(3):
            if board[row][col] == ".":
                return False
    return True

###

def isGameOver(b):
    if isFull(b) == True:
        return True
    allLines = horizLines(b) + vertLines(b) + diagLines(b)
    for line in allLines:
        if line == "XXX" or line == "OOO":
            return True
    return False

def playGame():
    board = makeNewBoard()
    showBoard(board)
    xTurn = True
    while isGameOver(board) == False:
        if xTurn:
            board = takeTurn(board, "X")
        else:
            board = takeTurn(board, "O")
        showBoard(board)
        xTurn = not xTurn
    print("all done!")

playGame()