在Python中标记多维数组坐标

时间:2016-11-03 02:07:25

标签: python multidimensional-array indexing grid

我想做的是将Coordinante(grid [0] [0])标记为'A1',(grid [0] [1])为'A2',(grid [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: ")

提前感谢您回答我的问题所花费的时间和精力。

P.S。我知道我的代码非常苛刻。我只有3周进入python大声笑。我没有完成代码,只是挂在这一部分....

1 个答案:

答案 0 :(得分:0)

就标记网格坐标而言,我发现我可以在if语句中使用变量'removeB'(存储的用户输入)。所以if(removeB ==“1”)然后将grid [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 -   

我没有包含我在上面使用过的几个函数,但是它们的行为就像命名一样。如果您对此处涉及的内容有任何疑问,我很乐意与您分享我的发现的其他方面。