2016-11-03 9 views
0

私がしたいことは、座標系(grid [0] [0])を 'A1'、(grid [0] [1]) 'A2'グリッド[1] [0])を 'B1'、(グリッド[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") 

私は、指定された座標にB2 '変数のA1、A2、B1の割り当てについては移動するかどうかはわかりません。それが助け場合、私は以下の私のコードを添付した

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.私のコードは非常にノービーであることを知っています。私はPython LOLに3週間しかいません。私はコードで終わっていない、この部分にちょうどハングアップ....

答えて

0

私は、 ifステートメント。だから(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 - 

しかし私は、彼らの行動は、彼らが命名されているだけのようにしてください、私は上記で使用される機能のカップルが含まれていませんでした。私がここで取り上げたことについて何か質問があれば、私の知見の他の側面を分かち合うことがうれしいです。