title: Tic Tac Toe - Game always ending in a tie
body: <p>I am having an issue with my Tic tic toe game.  After my first loop in def play_game the game is always ending in a tie.  I think it's is caused by my bool values in my check_for_tie function. I've tried playing with the values to no avail.  Bools always give me problems.</p>
  <p>Additionally, if I remove the tie portion to test the if there is a winner it only breaks the loop after both players go.</p>
  <pre><code># Global Variables
  grid_values = [[&quot; &quot;, &quot; &quot;, &quot; &quot;],
                 [&quot; &quot;, &quot; &quot;, &quot; &quot;],
                 [&quot; &quot;, &quot; &quot;, &quot; &quot;]]
  winner = None
  game_still_going = True
  
  
  def display_title():
      print(&quot;Welcome to Tic Tac Toe&quot;)
      print()
  
  
  def print_blank_board():
      print(&quot;+---+---+---+&quot;)
      print(&quot;|\t|\t|\t|&quot;)
      print(&quot;+---+---+---+&quot;)
      print(&quot;|\t|\t|\t|&quot;)
      print(&quot;+---+---+---+&quot;)
      print(&quot;|\t|\t|\t|&quot;)
      print(&quot;+---+---+---+&quot;)
      print()
  
  
  # Function to test if the players move is a untaken spot
  def grid_test(xcord, ycord, grid):
      if grid[xcord][ycord] == 1:
          print(&quot;Space is already taken.  Please select a different space:&quot;)
          return False
      elif grid[xcord][ycord] == 0:
          grid[xcord][ycord] = 1
          return True
  
  
  # checks row for a winner
  def check_rows():
      # Set global variables
      global game_still_going
      # Check if any of the rows have all the same value (and is not empty)
      row_1 = grid_values[0][0] == grid_values[0][1] == grid_values[0][2] != &quot; &quot;
      row_2 = grid_values[1][0] == grid_values[1][1] == grid_values[1][2] != &quot; &quot;
      row_3 = grid_values[2][0] == grid_values[2][1] == grid_values[2][2] != &quot; &quot;
      # If any row does have a match, flag that there is a win
      if row_1 or row_2 or row_3:
          game_still_going = False
      # Return the winner
      if row_1:
          return grid_values[0][0]
      elif row_2:
          return grid_values[0][0]
      elif row_3:
          return grid_values[0][0]
          # Or return None if there was no winner
      else:
          return None
  
  
  # checks column for a winner
  def check_columns():
      # Set global variables
      global game_still_going
      # Check if any of the rows have all the same value (and is not empty)
      column_1 = grid_values[0][0] == grid_values[1][0] == grid_values[2][0] != &quot; &quot;
      column_2 = grid_values[0][1] == grid_values[1][1] == grid_values[2][1] != &quot; &quot;
      column_3 = grid_values[0][2] == grid_values[1][2] == grid_values[2][2] != &quot; &quot;
      # If any row does have a match, flag that there is a win
      if column_1 or column_2 or column_3:
          game_still_going = False
      # Return the winner
      if column_1:
          return grid_values[0][0]
      elif column_2:
          return grid_values[0][1]
      elif column_3:
          return grid_values[0][2]
          # Or return None if there was no winner
      else:
          return None
  
  
  # # checks diagonals for a winner
  def check_diagonals():
      # Set global variables
      global game_still_going
      # Check if any of the rows have all the same value (and is not empty)
      diagonal_1 = grid_values[0][0] == grid_values[1][1] == grid_values[2][2] != &quot; &quot;
      diagonal_2 = grid_values[0][2] == grid_values[1][1] == grid_values[2][0] != &quot; &quot;
      # If any row does have a match, flag that there is a win
      if diagonal_1 or diagonal_2:
          game_still_going = False
      # Return the winner
      if diagonal_1:
          return grid_values[0][0]
      elif diagonal_2:
          return grid_values[0][1]
          # Or return None if there was no winner
      else:
          return None
  
  
  # at the end of of the play_game loop it runs this function that calls 2 more functions to check if there is a winner or a tie
  def check_if_game_over():
      check_for_winner()
      check_for_tie()
  
  
  # checks for a winner and returns who won.
  def check_for_winner():
      # Set global variables
      global winner
      # Check if there was a winner anywhere
      row_winner = check_rows()
      column_winner = check_columns()
      diagonal_winner = check_diagonals()
      # Get the winner
      if row_winner:
          winner = row_winner
      elif column_winner:
          winner = column_winner
      elif diagonal_winner:
          winner = diagonal_winner
      else:
          winner = None
  
  
  # check if the game is a tie or not
  def check_for_tie():
      global game_still_going
    # If board is full
      if &quot; &quot; not in grid_values:
          game_still_going = False
          return True
    # Else there is no tie
      else:
          return False
  
  
  def play_game():
      turn = 1
      grid_tester = [[0, 0, 0],
                     [0, 0, 0],
                     [0, 0, 0]]
      test = False
      while game_still_going:
          #print(&quot;turn = &quot;, turn)  #turn counter.  here just for testing.
          print(&quot;X's Turn&quot;)
          xtest = False
          otest = False
          # ASK user to select a row              FOR X
          while xtest == False:
              xrow = int(input(&quot;pick a row (1, 2, 3): &quot;))
              xrow += - 1
              while xrow &lt; 0 or xrow &gt;= 3:
                  if xrow not in range(0 - 3):
                      print(&quot;Invalid option!!!&quot;)
                      xrow = int(input(&quot;pick a row (1, 2, 3): &quot;))
                      xrow += - 1
  
              # ASk user to select a column               FOR X
              xcolumn = int(input(&quot;pick a column (1, 2, 3): &quot;))
              xcolumn += - 1
              while xcolumn &lt; 0 or xcolumn &gt;= 3:
                  if xcolumn not in range(0 - 3):
                      print(&quot;Invalid option!!!&quot;)
                      xcolumn = int(input(&quot;pick a column (1, 2, 3): &quot;))
                      xcolumn += - 1
  
              xtest = grid_test(xrow, xcolumn, grid_tester)
              if xtest == True:
                  grid_values[xrow][xcolumn] = &quot;X&quot;
                  turn += 1
  
          check_if_game_over()
  
          #turn += 1
          print()
          print(&quot;+---+---+---+&quot;)
          print(&quot;|&quot;, grid_values[0][0], &quot;|&quot;, grid_values[0][1], &quot;|&quot;, grid_values[0][2], &quot;|&quot;)
          print(&quot;+---+---+---+&quot;)
          print(&quot;|&quot;, grid_values[1][0], &quot;|&quot;, grid_values[1][1], &quot;|&quot;, grid_values[1][2], &quot;|&quot;)
          print(&quot;+---+---+---+&quot;)
          print(&quot;|&quot;, grid_values[2][0], &quot;|&quot;, grid_values[2][1], &quot;|&quot;, grid_values[2][2], &quot;|&quot;)
          print(&quot;+---+---+---+&quot;)
          print()
  
          print(&quot;turn = &quot;, turn)  ## here for testing.  REMOVE!!!!!!!
          print(&quot;O's Turn&quot;)
  
  
          # ASK user to select a row               FOR O
  
          while otest == False:
              orow = int(input(&quot;pick a row (1, 2, 3): &quot;))
              orow += - 1
              while orow &lt; 0 or orow &gt;= 3:
                  if orow not in range(0 - 3):
                      print(&quot;Invalid option!!!&quot;)
                      orow = int(input(&quot;pick a row (1, 2, 3): &quot;))
                      orow += - 1
  
              # ASk user to select a column               FOR O
              ocolumn = int(input(&quot;pick a column (1, 2, 3): &quot;))
              ocolumn += - 1
              while ocolumn &lt; 0 or ocolumn &gt;= 3:
                  if ocolumn not in range(0 - 3):
                      print(&quot;Invalid option!!!&quot;)
                      ocolumn = int(input(&quot;pick a column (1, 2, 3): &quot;))
                      ocolumn += - 1
              print(turn)  ## here for testing.  REMOVE!!!!!!!
              otest = grid_test(orow, ocolumn, grid_tester)
              if otest == True:
                  grid_values[orow][ocolumn] = &quot;O&quot;
                  turn += 1
  
          print()
          print(&quot;+---+---+---+&quot;)
          print(&quot;|&quot;, grid_values[0][0], &quot;|&quot;, grid_values[0][1], &quot;|&quot;, grid_values[0][2], &quot;|&quot;)
          print(&quot;+---+---+---+&quot;)
          print(&quot;|&quot;, grid_values[1][0], &quot;|&quot;, grid_values[1][1], &quot;|&quot;, grid_values[1][2], &quot;|&quot;)
          print(&quot;+---+---+---+&quot;)
          print(&quot;|&quot;, grid_values[2][0], &quot;|&quot;, grid_values[2][1], &quot;|&quot;, grid_values[2][2], &quot;|&quot;)
          print(&quot;+---+---+---+&quot;)
          print()
  
          check_if_game_over()
  
      if winner == &quot;X&quot; or winner == &quot;O&quot;:
          print(winner + &quot; won.&quot;)
      elif winner is None:
          print(&quot;Game is a tie!&quot;)
  
  
  def main():
      display_title()
      print_blank_board()
      play_game()
  
  # if started as the main module, call the main function
  if __name__ == &quot;__main__&quot;:
      main()
  </code></pre>