Whenever I run this file I get a IndexError: string index out of range, not sure where I'm going wrong here
Whenever I run this file I get a IndexError: string index out of range, not sure where I'm going wrong here
首先,post 很抱歉,如果这不是格式化所有内容的正确方法,但我正在尝试为两个玩家制作一个 connect 4 游戏,但我不明白为什么我收到此错误。
#Creates the board
def Boardmaker(field):
for row in range(12): #0,1,2,3,4,5,6,7,8,9,10,11
NewRow = int(row/2)
if row%2 == 0:
for column in range(13):
NewColumn = int(column/2)
if column%2 == 0:
if column != 12:
print(field[NewColumn][NewRow],end="")
else:
print(field[NewColumn][NewRow])
else:
print("|",end="")
else:
print("-------------")
#Play space
player = 1 #0 1 2 3 4 5 6
Currentfield = [[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "]]
#Player Controller and turn allocator
Boardmaker(Currentfield)
while(True):
MoveColumn = int(input("Please enter a column"))
if player == 1:
#if Currentfield[MoveColumn] == " ":
Currentfield[MoveColumn] = "X"
player = 2
else:
#if Currentfield[MoveColumn] == " ":
Currentfield[MoveColumn] = "O"
player = 1
Boardmaker(Currentfield)
在代码中
if player == 1:
#if Currentfield[MoveColumn] == " ":
Currentfield[MoveColumn] = "X"
player = 2
您正在用字符串 "X"
替换 Currentfield
的列表之一,因为您没有指定行索引。然后,在打印游戏板时迭代这个字符串,因为这是一个长度为 1 的字符串,所以你得到索引错误。
在 connect 4 游戏中,您必须设置棋盘的 column
和 row
,您的代码将是:
player = 1
Currentfield = [[" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "]]
while(True):
MoveColumn = int(input("Please enter a column:"))
MoveRow = int(input("Please enter a row:"))
if player == 1:
#if Currentfield[MoveColumn] == " ":
Currentfield[MoveColumn][MoveRow] = "X"
player = 2
else:
#if Currentfield[MoveColumn] == " ":
Currentfield[MoveColumn][MoveRow] = "O"
player = 1
Boardmaker(Currentfield)
此外,我注意到您没有正确使用列表的索引,首先是 row
,然后是 column
,上面的代码应该可以在您的程序中使用,但是,如果您想要更改它,只需将 field[NewColumn][NewRow]
更改为 field[NewRow][NewColumn]
并将 Currentfield[MoveColumn][MoveRow]
更改为 Currentfield[MoveRow][MoveColumn]
首先,post 很抱歉,如果这不是格式化所有内容的正确方法,但我正在尝试为两个玩家制作一个 connect 4 游戏,但我不明白为什么我收到此错误。
#Creates the board
def Boardmaker(field):
for row in range(12): #0,1,2,3,4,5,6,7,8,9,10,11
NewRow = int(row/2)
if row%2 == 0:
for column in range(13):
NewColumn = int(column/2)
if column%2 == 0:
if column != 12:
print(field[NewColumn][NewRow],end="")
else:
print(field[NewColumn][NewRow])
else:
print("|",end="")
else:
print("-------------")
#Play space
player = 1 #0 1 2 3 4 5 6
Currentfield = [[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "]]
#Player Controller and turn allocator
Boardmaker(Currentfield)
while(True):
MoveColumn = int(input("Please enter a column"))
if player == 1:
#if Currentfield[MoveColumn] == " ":
Currentfield[MoveColumn] = "X"
player = 2
else:
#if Currentfield[MoveColumn] == " ":
Currentfield[MoveColumn] = "O"
player = 1
Boardmaker(Currentfield)
在代码中
if player == 1:
#if Currentfield[MoveColumn] == " ":
Currentfield[MoveColumn] = "X"
player = 2
您正在用字符串 "X"
替换 Currentfield
的列表之一,因为您没有指定行索引。然后,在打印游戏板时迭代这个字符串,因为这是一个长度为 1 的字符串,所以你得到索引错误。
在 connect 4 游戏中,您必须设置棋盘的 column
和 row
,您的代码将是:
player = 1
Currentfield = [[" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "]]
while(True):
MoveColumn = int(input("Please enter a column:"))
MoveRow = int(input("Please enter a row:"))
if player == 1:
#if Currentfield[MoveColumn] == " ":
Currentfield[MoveColumn][MoveRow] = "X"
player = 2
else:
#if Currentfield[MoveColumn] == " ":
Currentfield[MoveColumn][MoveRow] = "O"
player = 1
Boardmaker(Currentfield)
此外,我注意到您没有正确使用列表的索引,首先是 row
,然后是 column
,上面的代码应该可以在您的程序中使用,但是,如果您想要更改它,只需将 field[NewColumn][NewRow]
更改为 field[NewRow][NewColumn]
并将 Currentfield[MoveColumn][MoveRow]
更改为 Currentfield[MoveRow][MoveColumn]