所以我正在開發遊戲連接4,我需要擺脫負面索引,因爲它會導致遊戲行爲有趣。基本上,玩家訪問的列基於一組列表組合成一個列表以形成一個數組。例如是否可以禁用負向索引?
grid1 = ['A','B','C','1']
grid2 = ['D','E','F','2']
grid3 = ['G','H','I','3']
grid4 = ['J','K','L','4']
# Now if we combine all three lists, we get
Total_Grid = [['A','B','C','1']
['D','E','F','2']
['G','H','I','3']
['J','K','L','4']]
# We have a total of 4 columns and 4 rows in this grid
# Here is the format of how we access values in list Total_Grid[row][col]
因此,要訪問字母'G',我們做Total_Grid [2] [0]。因爲「G」是在第2行,列0抽出實際電網,我們有:
| | | | |
-------------
| | | | |
-------------
| | | | |
-------------
| | | | |
-------------
# As you can see, the grid is 4x4
現在因爲在連接4,你不會選擇什麼排櫃檯進去,(它通常會下降到網格的底部),我們將爲行指定一個值。
row = 3
# Lets ask the user for input
col = input("What column would you like to drop your counter in? ")
# let's say user inputs 3, the counter will drop to [3][3] in the grid
col = 3
| | | | |
-----------------
| | | | |
-----------------
| | | | |
-----------------
| | | | X |
-----------------
我現在的問題的產生是因爲,例如,如果用戶輸入該列的值是負數,它仍然有效,因爲它的索引落後,但我想禁用此,因爲它打亂了遊戲時的AI嘗試從連接4點
阻止玩家
你有沒有試過'如果我<0'? –
你想要什麼?一個錯誤,或忽略它?你提到兩個。 – roganjosh
您可以更改代碼嗎?我的意思是,您是否可以將「print」替換爲其他自定義方法? –