f = open("sample.txt", "r")

text = f.readlines()

f.close()

f2 = open("sample2.txt", "w")

f2.write("Let's see if this works")

f2.close()

###

"""
makeNewBoard(): construct and return the starter board (a 2D list of strings)

showBoard(board): display a given board

takeTurn(board, player): let the given player ("X" or "O")
make a move on the board, returning the updated board

isGameOver(board): return True or False based on whether or not the game is over
"""

def playGame():
    print("Let's play tic-tac-toe!")
    b = makeNewBoard()
    showBoard(b)
    isXTurn = True
    while not isGameOver(b):
        if isXTurn == True:
            b = takeTurn(b, "X")
        else:
            b = takeTurn(b, "O")
        showBoard(b)
        isXTurn = not isXTurn
    print("Game over!")

# 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:
    row = input("Enter a row for " + player + ":")
    col = input("Enter a col for " + player + ":")
    # Make sure it's a number!
    if row.isdigit() and col.isdigit():
      row = int(row)
      col = int(col)
      # Make sure it's 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
          return board
        else:
          print("That space isn't open!")
      else:
        print("Not a valid space!")
    else:
      print("That's not a number!")

"""
allHorizLines, allVertLines, allDiagLines -> take board, return a list of strings
isBoardFull -> take board, return True/False
"""

def isGameOver(board):
    allLines = allHorizLines(board) + allVertLines(board) + allDiagLines(board)
    for line in allLines:
        if line == "XXX" or line == "OOO":
            return True
    if isBoardFull(board):
        return True # tie
    else:
        return False

# Generate all horizontal lines
def allHorizLines(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 allVertLines(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 allDiagLines(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 isBoardFull(board):
    for row in range(3):
        for col in range(3):
            if board[row][col] == ".":
                return False
    return True
