我正在尝试编写一个连接4游戏。如果您不知道规则,请查找它,这是一个相对简单的游戏。我已将每一行都制成一个列表。但是,它将每一行打印在另一行之下。我希望它将每一行打印在另一行旁边。
#Variables that determine how close up or if the slot is full for each column
ZeroTime = 0
OneTime = 0
TwoTime = 0
ThreeTime = 0
FourTime = 0
FiveTime = 0
SixTime = 0
#Makes the List for Each Row
ListZero = ["0",".", ".", ".", ".", ".", ".", "."]
ListOne = ["1",".", ".", ".", ".", ".", ".", "."]
ListTwo = ["2",".", ".", ".", ".", ".", ".", "."]
ListThree = ["3",".", ".", ".", ".", ".", ".", "."]
ListFour = ["4",".", ".", ".", ".", ".", ".", "."]
ListFive = ["5",".", ".", ".", ".", ".", ".", "."]
ListSix = ["6",".", ".", ".", ".", ".", ".", "."]
#Asks The Players For Their Usernames
namo = str(input("Player O what is your name: "))
namx = str(input("Player X what is your name: "))
print (namo + " is using O and " + namx + " is using X")
#Prints Current Board
print ("""
The current board is:
0 1 2 3 4 5 6
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
""")
#Asks the O player for which column they are going to choose
ot = str(input(namo + ", you're O! Enter which column you choose:"))
#Adds the slot
#For Slot 0
if ot == "0":
ListZero [7 - ZeroTime] = "O"
print (ListZero [0 + ZeroTime])
ZeroTime = ZeroTime + 1
if ot == "1":
ListOne [7 - ZeroTime] = "O"
print (ListZero [0 + ZeroTime])
ZeroTime = ZeroTime + 1
if ot == "2":
ListTwo [7 - ZeroTime] = "O"
print (ListZero [0 + ZeroTime])
ZeroTime = ZeroTime + 1
if ot == "3":
ListThree [7 - ZeroTime] = "O"
print (ListZero [0 + ZeroTime])
ZeroTime = ZeroTime + 1
if ot == "4":
ListFour [7 - ZeroTime] = "O"
print (ListZero [0 + ZeroTime])
ZeroTime = ZeroTime + 1
else:
print ("""We Hit an Error!
Sorry, we don't have a slot for that. The code only allows these
slots:
0, 1, 2, 3, 4, 5, 6.
Your turn has been skipped. """) #Added turn has been skipped, I can
fix that later but we're in the prototype right now
#Prints the Board After That
print ("""
The current board is:
""")
#I was confused on printing lists with the [] and '' so I googled
online, the base code for this was found online. However, I added
ListZero, ListOne, etc.
print(*ListZero, sep='\n')
print(*ListOne, sep='\n')
print(*ListTwo, sep='\n')
print(*ListThree, sep='\n')
print(*ListFour, sep='\n')
print(*ListFive, sep='\n')
print(*ListSix, sep='\n')
答案 0 :(得分:0)
以下是有关如何垂直相邻地打印两个列表的简短代码段。也许您可以将此扩展到您的代码。 zip
可以采用任意数量的支持迭代的对象。
one = ['a', 'b', 'c']
two = [1, 2, 3]
three = ['q', 'w', 'e']
for x, y, z in zip(one,two, three):
print(x, y, z)
输出:
a 1 q
b 2 w
c 3 e
这里与您的代码相对应。
ListZero = ["0",".", ".", ".", ".", ".", ".", "."]
ListOne = ["1",".", ".", ".", ".", ".", ".", "."]
ListTwo = ["2",".", ".", ".", ".", ".", ".", "."]
ListThree = ["3",".", ".", ".", ".", ".", ".", "."]
ListFour = ["4",".", ".", ".", ".", ".", ".", "."]
ListFive = ["5",".", ".", ".", ".", ".", ".", "."]
ListSix = ["6",".", ".", ".", ".", ".", ".", "."]
for a,b,c,d,e,f,g in zip(ListZero, ListOne, ListTwo, ListThree, ListFour, ListFive, ListSix):
print(a,b,c,d,e,f)
输出:
0 1 2 3 4 5
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
希望这会有所帮助。
答案 1 :(得分:0)
您可以依次遍历列表元素的位置,而不是顺序打印列表,然后遍历列表:
mylists = [ListZero, ListOne, ListTwo, ListThree, ListFour, ListFive, ListSix]
for i in range(8):
for alist in mylists:
print(alist[i], end=' ')
print()
Vineeth的解决方案更为优雅。
答案 2 :(得分:0)
您可以使用提供多维数组的numpy数组。
import numpy
a = numpy.array([[YOUR LISTS],[YOUR LISTS],...])
print(a.T)
答案 3 :(得分:0)
由于它是四个连接点,所以我建议使用numpy
,这样也可以得到对角线:
import numpy as np
a = np.full((7, 6), '.')
print(a)
print('')
上面的代码生成矩阵并将其打印出来。在下面的代码中填充一些单元格并显示如何获取对角线,请参见https://docs.scipy.org/doc/numpy/reference/generated/numpy.diagonal.html
a[0][0] = 'T'
a[6][0] = 'B'
a[5][0] = 'S'
a[4][0] = 'L'
a[6][1] = 'R'
a[5][1] = 'X'
a[6][5] = 'K'
a[3][3] = 'E'
a[0][5] = 'Z'
a[2][5] = 'M'
a[1][0] = 'N'
for i in range(6):
print(a.diagonal(i,0,1))
for i in range(5):
print(a.diagonal(-i-1,0,1))