Search code examples
pythontic-tac-toe

How do I allow the user to choose the plane by planes in Tic Tac Toe


I want to allow the user to choose how big the board will be in Tic Tac Toe.

I don't understand how to make a dynamic variable to check if the player wins.

The only viable option I can think of is the make if statement for every possible x-by-x the user can choose. This is what I have so far for making the board.

x = int(input("How many x-by-x do you want?: "))
index = x**2
value = ["-"]*index


def board(index):
    for i in range(index):
        if i != 0:
            if (i % x) == 0:
                print()
        print("-", end=" ")

Solution

  • This question is a bit broad, but I will do my best to send you in the right direction:

    First of all, letting the user pick their board size is fairly simple. Something similar to the following should work, where you assign a variable to the user's input and then run loops to create an array of arrays in the size of the board:

    board_size = int(input("How big do you want your tic-tac-toe board to be?"))
    board = []
    
    for row in range(board_size):
      board.append([])
      for column in range(board_size):
        board[row].append([])
    

    Given an input of 4, the board variable would be equal to:

    [[[], [], [], []], 
     [[], [], [], []], 
     [[], [], [], []], 
     [[], [], [], []]]
    

    But defining win conditions is a bit more difficult. We have to look at all the patterns you could have to win: Three in a row sideways, three in a row vertically, and three in a row diagonally. Since our board is nicely saved as an array of arrays, we can make general rules to see if there's 3 "x" or 3 "o" in a row anywhere no matter how big the board is.

    Horizontal rows are pretty easy: Just check if there are three "x"s or "o"s in a row:

    for row in range(board_size):
      for column in range(board_size-2):
        if board[row][column] == board[row][column+1] == board[row][column+2]:
          if board[row][column] == "x":
            print("x wins!")
          else:
            print("o wins!")
    

    Now, to add vertical rows, we have to do the same thing but iterating through columns then rows instead of rows then columns:

    for column in range(board_size):
      for row in range(board_size-2):
        if board[row][column] == board[row+1][column] == board[row+2][column]:
          if board[row][column] == "x":
            print("x wins!")
          else:
            print("o wins!")
    

    and finally the diagonals. We do this by going through each row and column, but we add 1 to both of them OR add 1 to the rows and take one away from the columns. This allows us to get this shape (rows and columns both increase) or that shape (rows increase and columns decrease):

    "x"                         "x"
       "x"                   "x"
          "x"             "x"
    

    To find increasing rows & increasing columns:

    for row in range(board_size-2):
      for column in range(board_size-2):
        if board[row][column] == board[row+1][column+1] == board[row+2][column+2]:
          if board[row][column] == "x":
            print("x wins!")
          else:
            print("o wins!")
    

    And to find increasing rows & decreasing columns:

    for row in range(board_size-2):
      for column in range(board_size-2):
        if board[row][column+2] == board[row+1][column+1] == board[row+2][column]:
          if board[row][column] == "x":
            print("x wins!")
          else:
            print("o wins!")
    

    When we put this all together using a function, it looks like this:

    #user inputs preferred board size
    board_size = int(input("How big do you want your tic-tac-toe board to be?"))
    board = []
    
    # we make the board arrays
    for row in range(board_size):
      board.append([])
      for column in range(board_size):
        board[row].append([])
    
    # returns who won the game
    def game_state(board):
    
      # horizontal wins?
      for row in range(board_size):
        for column in range(board_size-2):
          if board[row][column] == board[row][column+1] == board[row][column+2]:
            if board[row][column] == "x":
              return "x wins!" 
            else:
              return "o wins!" 
      
      # vertical wins?
      for column in range(board_size):
        for row in range(board_size-2):
          if board[row][column] == board[row+1][column] == board[row+2][column]:
            if board[row][column] == "x":
              return "x wins!" 
            else:
              return "o wins!" 
    
      # diagonal wins (aigu)?
      for row in range(board_size-2):
        for column in range(board_size-2):
          if board[row][column+2] == board[row+1][column+1] == board[row+2][column]:
            if board[row][column] == "x":
              return "x wins!" 
            else:
              return "o wins!" 
      
      # diagonal wins (grave)?
      for row in range(board_size-2):
        for column in range(board_size-2):
          if board[row][column] == board[row+1][column+1] == board[row+2][column+2]:
            if board[row][column] == "x":
              return "x wins!"
            else:
              return "o wins!"
      
      return "the game is still in progress"
    
    '''
    Here's where your game would run. 
    You would get people to play moves, and after each move, see if someone has won.
    Once the board is filled up with moves (when number of moves == board_size**2), \
    you will run game_state on the final position to see who won, or if it is a draw.
    Good luck!
    '''