def getDimension(): row = int(input("Enter number of rows (between 4 and 10): ")) while row < 4 or row > 10: row = int(input("Invalid input! Enter number of rows (between 4 and 10): ")) column = int(input("Enter number of columns (between 4 and 10): ")) while column < 4 or column > 10: column = int(input("Invalid input! Enter number of columns (between 4 and 10): ")) return row, column def printBoard(board): for row in board: print("|", end="") for cell in row: if cell == "": print(" ", end="") else: print(cell, end="") print("|", end="") print() print("-" * (len(board[0]) * 2 + 1)) def dropToken(board, col, token): row = len(board) - 1 while row >= 0: if board[row][col] == "": board[row][col] = token return row row -= 1 return -1 def isValidLocation(board, col): return board[0][col] == "" def isTie(board): for row in board: for cell in row: if cell == "": return False return True def checkWinner(board, token): # Check horizontal for row in board: for col in range(len(row) - 3): if row[col] == token and row[col+1] == token and row[col+2] == token and row[col+3] == token: return True # Check vertical for col in range(len(board[0])): for row in range(len(board) - 3): if board[row][col] == token and board[row+1][col] == token and board[row+2][col] == token and board[row+3][col] == token: return True # Check diagonal positive for col in range(len(board[0]) - 3): for row in range(len(board) - 3): if board[row][col] == token and board[row+1][col+1] == token and board[row+2][col+2] == token and board[row+3][col+3] == token: return True # Check diagonal negative for col in range(len(board[0]) - 3): for row in range(3, len(board)): if board[row][col] == token and board[row-1][col+1] == token and board[row-2][col+2] == token and board[row-3][col+3] == token: return True return False # Main game loop row, column = getDimension() board = [["" for j in range(column)] for i in range(row)] game_over = False current_player = "X" while not game_over: printBoard(board) if current_player == "X": col = int(input("Player X, enter column to drop your token (0-{}): ".format(column-1))) while not isValidLocation(board, col): col = int(input("Column is full or invalid! Player X, enter column to drop your token (0-{}): ".format(column-1))) row = dropToken(board, col, current_player) if row == -1: print("Error: invalid row") continue if checkWinner(board, current_player): printBoard(board) print("Congratulations! Player {} wins!".format(current_player)) game_over = True elif isTie(board): printBoard(board) print("The game is a tie.") game_over = True current_player = "O" # Switch player else: col = int(input("Player O, enter column to drop your token (0-{}): ".format(column-1))) while not isValidLocation(board, col): col = int(input("Column is full or invalid! Player O, enter column to drop your token (0-{}): ".format(column-1))) row = dropToken(board, col, current_player) if row == -1: print("Error: invalid row") continue if checkWinner(board, current_player): printBoard(board) print("Congratulations! Player {} wins!".format(current_player)) game_over = True elif isTie(board): printBoard(board) print("The game is a tie.") game_over = True current_player = "X" # Switch player print("Thanks for playing!") =================================================================================== Question: can you change the else statement into the picture attached
def getDimension():
row = int(input("Enter number of rows (between 4 and 10): "))
while row < 4 or row > 10:
row = int(input("Invalid input! Enter number of rows (between 4 and 10): "))
column = int(input("Enter number of columns (between 4 and 10): "))
while column < 4 or column > 10:
column = int(input("Invalid input! Enter number of columns (between 4 and 10): "))
return row, column
def printBoard(board):
for row in board:
print("|", end="")
for cell in row:
if cell == "":
print(" ", end="")
else:
print(cell, end="")
print("|", end="")
print()
print("-" * (len(board[0]) * 2 + 1))
def dropToken(board, col, token):
row = len(board) - 1
while row >= 0:
if board[row][col] == "":
board[row][col] = token
return row
row -= 1
return -1
def isValidLocation(board, col):
return board[0][col] == ""
def isTie(board):
for row in board:
for cell in row:
if cell == "":
return False
return True
def checkWinner(board, token):
# Check horizontal
for row in board:
for col in range(len(row) - 3):
if row[col] == token and row[col+1] == token and row[col+2] == token and row[col+3] == token:
return True
# Check vertical
for col in range(len(board[0])):
for row in range(len(board) - 3):
if board[row][col] == token and board[row+1][col] == token and board[row+2][col] == token and board[row+3][col] == token:
return True
# Check diagonal positive
for col in range(len(board[0]) - 3):
for row in range(len(board) - 3):
if board[row][col] == token and board[row+1][col+1] == token and board[row+2][col+2] == token and board[row+3][col+3] == token:
return True
# Check diagonal negative
for col in range(len(board[0]) - 3):
for row in range(3, len(board)):
if board[row][col] == token and board[row-1][col+1] == token and board[row-2][col+2] == token and board[row-3][col+3] == token:
return True
return False
# Main game loop
row, column = getDimension()
board = [["" for j in range(column)] for i in range(row)]
game_over = False
current_player = "X"
while not game_over:
printBoard(board)
if current_player == "X":
col = int(input("Player X, enter column to drop your token (0-{}): ".format(column-1)))
while not isValidLocation(board, col):
col = int(input("Column is full or invalid! Player X, enter column to drop your token (0-{}): ".format(column-1)))
row = dropToken(board, col, current_player)
if row == -1:
print("Error: invalid row")
continue
if checkWinner(board, current_player):
printBoard(board)
print("Congratulations! Player {} wins!".format(current_player))
game_over = True
elif isTie(board):
printBoard(board)
print("The game is a tie.")
game_over = True
current_player = "O" # Switch player
else:
col = int(input("Player O, enter column to drop your token (0-{}): ".format(column-1)))
while not isValidLocation(board, col):
col = int(input("Column is full or invalid! Player O, enter column to drop your token (0-{}): ".format(column-1)))
row = dropToken(board, col, current_player)
if row == -1:
print("Error: invalid row")
continue
if checkWinner(board, current_player):
printBoard(board)
print("Congratulations! Player {} wins!".format(current_player))
game_over = True
elif isTie(board):
printBoard(board)
print("The game is a tie.")
game_over = True
current_player = "X" # Switch player
print("Thanks for playing!")
===================================================================================
Question:
can you change the else statement into the picture attached
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images