2016-11-03 19 views
0

我想要做的事情是將'ad'(grid [0] [0])標記爲'A1',(grid [0] [1])爲'A2',( (grid [1] [0])爲'B1',(grid [1] [1])爲'B2',依此類推。我正在創建一個遊戲,玩家必須選擇座標以便在遊戲開始之前移除它的內容。在Python中標註多維數組座標

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周進入蟒蛇大聲笑。我沒有完成代碼,只是掛在這部分....

回答

0

至於標籤的網格座標,我發現我可以只使用變量'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 

這是我的reslut:

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 - 

我並沒有包括一對夫婦,我在上面使用的功能,但是他們的行爲做的,就像他們的名字命名的。如果您對我在此處提到的任何問題有任何疑問,我很樂意分享我的發現的其他方面。