0
私はTic-Tac-Toeのゲームをほぼ完了しました。ただし、ユーザーが33を超える数値を入力すると、次のエラーが表示されます。Tic-Tac-Toeエラー処理Python
while board[choice] != " ":
"IndexError: list index out of range"
マイリストは0-33からのものです。誤って50を入力すると、このエラーを防ぐ方法がわかりません。あなたが入力したユーザに尋ねるライン41の後にこれを追加すること
TY
#Defining the board (The board is initally an empty list, which evenutally holds the user's data/input.
#The desired user input of a x(column) and y(row) coordinate "1,2,3 by 1,2,3" is held by the associated list value point.
#For example, "board[31] will hold either "X" or "O" when the user enters "31" (column 3, row 1)
board = ["", "", "", "", "", "", "", "", "", "", "", " ", " ", " ", "", "", "", "", "", "", "", " ", " ", " ", "", "", "", "", "", "", "", " ", " ", " "]
#Defining the print_board function. This produces/prints the actual board in real-time for the console when needed. (When a user chooses an available block)
def print_board():
print (' 1 2 3 ')
print (' | | ')
print ('1 '+board[11]+" | "+board[21]+" | "+board[31]+"")
print (' ----+----+----')
print (' | | ')
print ('2 '+board[12]+" | "+board[22]+" | "+board[32]+"")
print (' | | ')
print (' ----+----+----')
print (' | | ')
print ('3 '+board[13]+" | "+board[23]+" | "+board[33]+"")
print (' | | ')
#Welcome message and instructions.
print("Welcome to Tic-Tac-Toe\n-----------------------\n")
print("Please enter a column followed by a row, e.g, 22 equates to the middle box.")
print(" 1 2 3 ")
print("1 | | ")
print("-----+-----+----")
print("2 | x | ")
print("-----+-----+----")
print("3 | | ")
print("-----|-----|----\n\n")
#while True statement to start the game.
while True:
print_board()
#Firstly print the board
#Player "X"
#Player "X" input
while True:
try:
choice = int(input("Please choose an empty space for X. (column,row) "))
break
except ValueError:
print ("Error, Please enter a coordinate.")
else:
break
#While loop, in case location is already taken.
while board[choice] != " ":
choice = int(input("Please choose an empty space for X. (column,row) "))
if board[choice] == " ":
board[choice] = "X"
#Possible combinations for "X" to win.
if board[11] == "X" and board[21] =="X" and board[31] == "X" or \
board[12] == "X" and board[22] =="X" and board[32] == "X" or \
board[13] == "X" and board[23] =="X" and board[33] == "X" or \
board[11] == "X" and board[12] =="X" and board[13] == "X" or \
board[21] == "X" and board[22] =="X" and board[23] == "X" or \
board[31] == "X" and board[32] =="X" and board[33] == "X" or \
board[11] == "X" and board[22] =="X" and board[33] == "X" or \
board[31] == "X" and board[22] =="X" and board[13] == "X":
print_board()
print ("X wins! Congratulations")
break
#Displays the last move.
print_board()
# True statement to identify a tie. If all locations are occupied then the game will result in a tie.
# isFull = True, is assuming the board is already full and a empty space " " will DISPROVE the True statement.
isFull = True
if " " in board:
isFull = False
if isFull == True:
print ("Tie")
break
#Player "O"
#Player "O" input
while True:
try:
choice = int(input("Please choose an empty space for O. (column,row) "))
break
except ValueError:
print ("Error")
else:
break
#While loop, in case location is already taken.
while board[choice] != " ":
choice = int(input("Please choose an empty space for O. (column,row) "))
if board[choice] == " ":
board[choice] = "O"
#Possible combinations for "O" to win.
if board[11] == "O" and board[21] =="O" and board[31] == "O" or \
board[12] == "O" and board[22] =="O" and board[32] == "O" or \
board[13] == "O" and board[23] =="O" and board[33] == "O" or \
board[11] == "O" and board[12] =="O" and board[13] == "O" or \
board[21] == "O" and board[22] =="O" and board[23] == "O" or \
board[31] == "O" and board[32] =="O" and board[33] == "O" or \
board[11] == "O" and board[22] =="O" and board[33] == "O" or \
board[31] == "O" and board[22] =="O" and board[13] == "O":
print_board()
print ("O wins! Congratulations")
break
# True statement to identify a tie. If all locations are occupied then the game will result in a tie.
# isFull = True, is assuming the board is already full and a empty space " " will DISPROVE the True statement.
isFull = True
if " " in board:
isFull = False
if isFull == True:
print ("Tie")
break