用Python标注多维数组坐标

2024-04-23 06:09:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我要做的是将坐标(网格[0][0])标记为“A1”,(网格[0][1])标记为“A2”,(网格[1][0])标记为“B1”,(网格[1][1])标记为“B2”,依此类推。我正在创建一个游戏,玩家必须选择坐标,以便在游戏开始前删除其中的内容。你知道吗

  B W B W                                  A1 A2 A3 A4
  W B W B    I want to access like this    B1 B2 B3 B4
  B W B W                                  C1 C2 C3 C4

现在,我正在询问用户他们想要删除哪一个片段(“B”或“W”)(可以是他们自己的片段)。我希望他们能够为左上方的“B”键入“A1”。你知道吗

removeB = input("BLACK, remove one of your pieces by typing in it's coordinates")

我不知道如何将A1,A2,B1,B2‘变量’分配到指定的坐标。我希望能够做到:

if(removeB == A1):
      grid[row -1][col -1].append('-')       # '-' = empty

如果有帮助,我将代码附在下面:

 import random
  numrows = 3
  numcols = 4
  def initial():
     grid = []
     count = 0
     y = 2
     for x in range(numrows)s
     grid.append([])
     for y in range(numcols):
        if ((x + y)%2):    
            grid[x].append('W')  
        else:
            grid[x].append('B')  
  for x in grid:       
    print(*x, sep=' ',end="\n")
  print("")
  color = input("Press 'Enter' to see which color you will be playing")
  print("")
  rand=random.randrange(1,3)
  if(rand == 1):
      print("Player1 you will be playing as BLACK")
      print("Player2 you will be playing as WHITE")
  else:
      print("Player1 you will be playing as WHITE")
      print("Player2 you will be playing as BLACK")
 print("")
 print(" The game board can be navigated as if it were: ")
 print("")
 example = '''\
 A1 A2 A3 A4        B W B W    
 B1 B2 B3 B4   =    W B W B
 C1 C2 C3 C4        B W B W
 '''
 print(example)
 print("and so on.....")
 print("")
 if(rand == 1):
      removeB = input("~BLACK Player, remove one of your pieces by typing in the coordinates: ")
      removeW = input("~WHITE Player, remove one of your pieces by typing in the coordinates: ")
  else:
      removeW = input("~WHITE Player, remove one of your pieces by typing in the coordinates: ")
      removeB = input("~BLACK Player, remove one of your pieces by typing in the coordinates: ")

提前感谢您花时间和精力回答我的问题。你知道吗

顺便说一下,我知道我的代码很难理解。我对python只有3个星期的了解,哈哈。我还没有完成代码,只是挂断了这部分。。。。你知道吗


Tags: ofintypinginputyourbya1be
1条回答
网友
1楼 · 发布于 2024-04-23 06:09:54

至于标记网格坐标,我发现我可以在if语句中使用变量'removeB'(存储的用户输入)。因此,如果(removeB==“1”),则将网格[0][0]的坐标从“B”设置为“-”(空)

newgrid = copy_grid(board) # here is where i copied the board (with separate function)
if(removeB == "1"): #if BLACKS's input is 1
    newgrid[0][0] = '-' #Change the top left grid coordinate value (grid[0][0]) to '-'
elif(removeB == "3"): #because coordinate 2 is a W piece, I skip to 3 likewise as we go
    newgrid[0][2] = '-'
elif(removeB == "6"):
    newgrid[1][1] = '-'
elif(removeB == "8"):
    newgrid[1][3] = '-'
elif(removeB == "9"):
    newgrid[2][0] = '-'#changes bottom left B to -
elif(removeB == "11"):
    newgrid[2][2] = '-'
if(removeW == "2"): #start comparing WHITE's input
    newgrid[0][1] = '-'
elif(removeW == "4"):
    newgrid[0][3] = '-'#changes top right W to -
elif(removeW == "5"):
    newgrid[1][0] = '-'
elif(removeW == "7"):
    newgrid[1][2] = '-'
elif(removeW == "10"):
    newgrid[2][1] = '-'
elif(removeW == "12"):
    newgrid[2][3] = '-'#changes bottom right W to -
board = copy_grid(newgrid)#copy the newgrid, which contains altered coordinates
show_grid(board)#function that prints out the new board with altered coordinates

这是我的简历:

 B W B W   <-gameboard             1  2  3  4
 W B W B                           5  6  7  8
 B W B W           coordinates->   9 10 11 12

BLACK's user input = 1
WHITE's user input = 12

输出:

 - W B W  
 W B W B   
 B W B -   

我并没有包括我在上面使用的几个函数,但是它们的操作与它们的名称一样。如果你对我在这里所涉及的内容有任何疑问,我很乐意分享我的发现的其他方面。你知道吗

相关问题 更多 >