2016-04-10 17 views
-1

私は完全に機能するグリッドを作成しました。移動して宝箱とゴブリンを集めるプレイヤーがいます。私がトレゼルとゴブリンに産卵するときに問題があります。彼らは私が起こりたくないグリッドのサイズを拡張します。誰かがこれを修正するのに役立つでしょうか?修正が必要な部分は、ChestsandGoblins関数です。私のコードは以下の通りです:リスト操作を使用するPython

from random import * 
# Set up Initial Variables 
Money = "0" 
grid = [] 
character = "X" 
# player_loc will hold the x, y location of the player 
player_loc = (0, 0) 
# These are a mapping of direction 
treasure_loc = (0, 0) 
NORTH = "N" 
SOUTH = "S" 
EAST = "E" 
WEST = "W" #All variables used for Later on 
Treasure = "T" 
Goblin = "G" 





def setupGrid(): 
global grid 
global row 
global N 
N = input("How big would you like the grid to be?") 
for x in range(0, (int(N))): 
    row = [] 
    for y in range(0, (int(N))): 
     if x == player_loc[0] and y == player_loc[1]: 
      row.append(character) 
     else: 
      row.append('O') 
    grid.append(row) 

def Chests_and_Goblins(): 
    global grid 
    global row 
    global N 
    print("How many chests would you like in the grid?")  
    B = input("The amount of chests you like is given by the amount of C's") 
    for each in B: 
     grid[randint(0, (int(N)))].insert(randint(0, (int(N))), Treasure) 
     grid[randint(0, (int(N)))].insert(randint(0, (int(N))), Goblin) 









def moveSouth(n): 
    global player_loc 
    grid[player_loc[0]][player_loc[1]] = "O" 
    grid[player_loc[0] + n][player_loc[1]] = character 
    player_loc = (player_loc[0] + n, player_loc[1]) 

def moveNorth(n): 
    global player_loc 
    grid[player_loc[0]][player_loc[1]] = "O" 
    grid[player_loc[0] - n][player_loc[1]] = character 
    player_loc = (player_loc[0] - n, player_loc[1]) 

def moveEast(n): 
    global player_loc 
    grid[player_loc[0]][player_loc[1]] = "O" 
    grid[player_loc[0]][player_loc[1] + n] = character 
    player_loc = (player_loc[0], player_loc[1] + n) 

def moveWest(n): 
    global player_loc 
    grid[player_loc[0]][player_loc[1]] = "O" 
    grid[player_loc[0]][player_loc[1] - n] = character 
    player_loc = (player_loc[0], player_loc[1] - n) 

def gridRunner(): 
    while True: 
     for row in grid: 
      print (row) 

     switch = {NORTH : moveNorth, 
        SOUTH : moveSouth, 
        EAST : moveEast, 
        WEST : moveWest } 
     P = input("What direction would you like to move in? North (N), South(S), East(E) or West(W)?").upper() 

     if P not in switch: 
      print ("invalid move") 
      continue 

     distance = int(input("How far would you like to move in this direction? (blocks are the units)")) 
     switch[P](distance) 







setupGrid() 
Chests_and_Goblins() 
gridRunner() 

答えて

0

insertリストのサイズを1つ増やしてください。リストの元のサイズを保持するには、代わりに割り当てを使用します。これは、既存のゴブリン/宝物/プレイヤーの位置を上書きすることが

grid[randint(0, int(N)-1)][randint(0, int(N)-1)] = Treasure 

は注意してください。次に何がその場所に既に存在しないことを確認し、最初のオブジェクトのx、y座標を生成するために、良いアイデア、およびのみ次に割り当てを実行することができます。

while True: 
    x = randint(0, int(N)) 
    y = randint(0, int(N)) 
    if grid[x][y] == "O": 
     grid[x][y] = Treasure 
     break 
+0

ありがとう!あなたが書いた他のコードが何をしているのか分かりませんが、コードの最上位ビットがトンを助けましたか?私のコードはそれなしで正常に動作するようです。再び –

+0

ああおかげで、私は今理解しています。私はコード内にどこに配置するのかはわかりませんが。あなたは私のためにそれを行うことができますか? –