Question
Quick question! ~~How can i resolve the issue on this python code to enable it play the game. The screen shot above pointed were the
Quick question! ~~How can i resolve the issue on this python code to enable it play the game. The screen shot above pointed were the error emanate from. Specifically from the last "else" statement in the code. The full code is shown below.
Your help will be appreciated.
Full code:
# This is a function that displays the boards
def displayBoard(board):
for row in board:
for col in row:
print(col, end= '\t')
print(" ")
def makeMove(moveVal, board): # This function asks for user to give input for 'X' player and for 'O' player then updates the value in 2D array
print(moveVal + " what row?", end= ' ')
row = int(input())
print(moveVal + " what col?", end= ' ')
col = int(input())
#ensure the move is legal or ask them again
if (board[row][col] != '_'):
print ("Illegal move. Enter again ! ")
makeMove(moveVal, board)
board[row][col] = moveVal
def chkBoard(mark, board):
if (mark == board[0][0] == board[1][1] == board[2][2]):
return True
if (mark == board[2][0] == board[1][1] == board[0][2]):
return True
for i in range(2):
if (mark == board[i][0] == board[i][1] == board[i][2]):
return True
if (mark == board[0][i] == board[1][i] == board[2][i]):
return True
return False
# this function checks if there is a winner of the game yet
def isWinner(board):
#return true if their is a winner and false otherwise
if (chkBoard('X', board) == True):
print ("X is winner ")
return True
if (chkBoard('O', board) == True):
print ("O is winner ")
return True
return False
def clearBoard(board):
#this function should perform the logic to clear the board
board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]
for rowNum in range(0, len(board)):
for colNum in range(0, len(board[rowNum])):
board[rowNum][colNum] = '_'
#This is the functions which keeps the game running untill there is a winner
def playGame(board):
moves = ['X', 'O']
currPlayer = 0
while(isWinner(board) == False):
displayBoard(board)
makeMove(moves[currPlayer], board)
if(currPlayer == 0):
currPlayer = 1
else:
currPlayer = 0
#we want to ask if they want to play again
user_ip = int(input("Want to play again ?? If yes=1, no=0:"))
if (user_ip == 1):
board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]
playGame(board)
else: print("Exiting..")
board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']] #if yes, clear the board and start over
playGame(board)
print ("sum of x+y =", z);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started