Can anyone debug this? TicTacToe assignment | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone debug this? TicTacToe assignment

import random #FUNCTIONS def int_input(choice): try: int(choice) / 1 return True except ValueError: return False def print_board(board): rowIndex = 0 for row in board: rowIndex += 1 index = 0 for spot in row: index += 1 if index != len(row): print("" , spot , "|" , end = "") else: print("" , spot, end = "") if rowIndex != len(board): print("\n --+---+--") print ("\n") def user_choice(turn): if turn == 1: print_board(board) choice = input(player1 + " enter your move: ") while int_input(choice) == False: choice = input("That choice is not an integer 1-9, where would you like to go? ") while spot_filled(choice) == True: choice = input("That choice is already taken, where would you like to go? ") choice = int(choice) change_board(choice , 1) if turn == 2: print_board(board) choice = input(player2 + " where would you like to go? ") while int_input(choice) == False: choice = input("That choice is not an integer 1-9, where would you like to go? ") while spot_filled(choice) == True: choice = input("That choice is already taken, where would you like to go? ") choice = int(choice) change_board(choice , 2) def change_board(choice , turn): yIndex = ((choice -1) - (choice - 1) % 3) // 3 xIndex = (choice - 1) % 3 if turn == 1: board[yIndex][xIndex] = symbol1 if turn == 2: board[yIndex][xIndex] = symbol2 def spot_filled(choice): while int_input(choice) == False: choice = input("That choice is not an integer 1-9, where would you like to go? ") choice = int(choice) yIndex = ((choice - 1) - (choice - 1) % 3) // 3 xIndex = (choice - 1) % 3 if board[yIndex][xIndex] == symbol1 o

3rd Jun 2021, 6:09 PM
Henry Fournier
Henry Fournier - avatar
4 Answers
+ 2
That's a lot of code. Can you just link your original code?
3rd Jun 2021, 7:09 PM
Lisa
Lisa - avatar
+ 2
You're more likely to get help if you copy your code to the playground and post a link to it in your OP. Then remove the code from your post. When you comment with additional information on your own post it can look like it has already been answered and can then be overlooked. I, personally have grown to avoid posts like this, because there is typically to much work involved in trying to solve someone else's issues. For instance first we'd have to copy all the code to the playground ourselves, then decipher your code and figure out how your program works or is supposed to work, then figure out what bug or bugs you're talking about. All this information should have already been given if you want us to help. Take more time with your posts and ask a good question providing the needed info correctly with a good description of what the problem(s) is, with examples etc!
3rd Jun 2021, 7:22 PM
ChaoticDawg
ChaoticDawg - avatar
0
def spot_filled(choice): while int_input(choice) == False: choice = input("That choice is not an integer 1-9, where would you like to go? ") choice = int(choice) yIndex = ((choice - 1) - (choice - 1) % 3) // 3 xIndex = (choice - 1) % 3 if board[yIndex][xIndex] == symbol1 or board[yIndex][xIndex] == symbol2: return True else: return False #Test Horizonatal winners by inputting board 2d list in and seeing if it matches up to the winning list def horizontal_win(board): #Create a list for every row and compare it to the winning list list = [] player1Wins = [] player2Wins = [] for row in range (0,3): for x in range (0, 3): list.append(board[row][x]) if list == player1Wins: return ("1") if list == player2Wins: return ("2") list = [] #Test the diagonal winners by inputting board list in and seeing if it matches up to winning list def diagonal_win(board): #First Diagonal list = [] player1Wins = [] player2Wins = [] for n in range (0,3): list.append(board[n][n]) if list == player1Wins: return ("1") if list == player2Wins: return("2") list = [] #Second Diagonal Xspot = 3 Yspot = 0 list2 = [] for n in range (0,3): list2.append (board[n][3 - 1 - n]) if list2 == player1Wins: return ("1") if list2 == player2Wins: return("2") list2 = []
3rd Jun 2021, 6:11 PM
Henry Fournier
Henry Fournier - avatar
0
def vertical_win(board): #The function works by creating a list of all verticals and seeing if they match up to the winning list list = [] player1Wins = [] player2Wins = [] for column in range (0,3): for y in range (0, 3): list.append(board[y][column]) if list == player1Wins: return ("1") if list == player2Wins: return ("2") list = [] def win_tester(board): if vertical_win(board) == "1": return ("1") if vertical_win(board) == "2": return ("2") if horizontal_win(board) == "1": return ("1") if horizontal_win(board) == "2": return ("2") if diagonal_win(board) == "1": return ("1") if diagonal_win(board) == "2": return ("2") print("Welcome to tic-tac-toe!") print("\n") board = [[y * 3 + x + 1 for x in range (3)] for y in range(3)] print_board(board) # Get player names and symbols
3rd Jun 2021, 6:11 PM
Henry Fournier
Henry Fournier - avatar