2014-10-11 35 views
0

我正在研究一个小项目,并且在编程中遇到了一些小错误。这是一个基本的战列舰游戏,到目前为止,我有两个“舰船”的设置,并且当我的侧面或敌人侧都被击中时,我的游戏就结束了。列表对象发生检查器python

def enemy_board(): 
global enemy_grid 
enemy_grid = [] 
for i in range (0,10): 
    enemy_grid.append(["="] * 10) 
def random_row_one(enemy_grid): 
    return randint(0, len(enemy_grid) - 1) 
def random_col_one(enemy_grid): 
    return randint(0, len(enemy_grid) - 1) 
def random_row_two(enemy_grid): 
    return randint(0, len(enemy_grid) - 1) 
def random_col_two(enemy_grid): 
    return randint(0, len(enemy_grid) - 1) 
global x_one 
x_one = random_row_one(enemy_grid) 
global y_one 
y_one = random_col_one(enemy_grid) 
global x_two 
x_two = random_row_two(enemy_grid) 
global y_two 
y_two = random_col_two(enemy_grid) 
print(x_one) 
print(y_one) 
print(x_two) 
print(y_two) 

所以这就是我的列表的基础,但后来在代码中是它给我一点麻烦。

elif enemy_grid.count("H") == 2: 
    print("\nYou got them all!\n") 
    break 

有人能帮助我吗?帮助将不胜感激。

更新 对不起,我对我的意思有点不清楚。

def my_board(): 
    global my_grid 
    my_grid = [] 
    for i in range (0,10): 
     my_grid.append(["O"] * 10) 
     def my_row_one(my_grid): 
     int(input("Where do you wish to position your first ship on the x-axis? ")) 
    def my_col_one(my_grid): 
     int(input("Where do you wish to position your first ship on the y-axis? ")) 
    global x_mio 
    x_mio = my_row_one(my_grid) 
    global y_mio 
    y_mio = my_col_one(my_grid) 
    def my_row_two(my_grid): 
     int(input("\nWhere do you wish to position your other ship on the x-axis? ")) 
    def my_col_two(my_grid): 
    int(input("Where do you wish to position your other ship on the y-axis? ")) 
    global x_mit 
    x_mit = my_row_two(my_grid) 
    global y_mit 
    y_mit = my_col_two(my_grid) 
def enemy_board(): 
    global enemy_grid 
    enemy_grid = [] 
    for i in range (0,10): 
     enemy_grid.append(["="] * 10) 
    def random_row_one(enemy_grid): 
     return randint(0, len(enemy_grid) - 1) 
    def random_col_one(enemy_grid): 
     return randint(0, len(enemy_grid) - 1) 
    def random_row_two(enemy_grid): 
     return randint(0, len(enemy_grid) - 1) 
    def random_col_two(enemy_grid): 
     return randint(0, len(enemy_grid) - 1) 
    global x_one 
    x_one = random_row_one(enemy_grid) 
    global y_one 
    y_one = random_col_one(enemy_grid) 
    global x_two 
    x_two = random_row_two(enemy_grid) 
    global y_two 
    y_two = random_col_two(enemy_grid) 
    print(x_one) 
    print(y_one) 
    print(x_two) 
    print(y_two) 
title() 
my_board() 
enemy_board() 
m = 20 
guesses = m 
while guesses > 0:  
    def printmi_board(my_grid): 
     for row in my_grid: 
      print(" ".join(row)) 
    def printyu_board(enemy_grid): 
     for row in enemy_grid: 
      print (" ".join(row)) 
    print(printmi_board(my_grid)) 
    print(printyu_board(enemy_grid)) 
    try: 
     guess_x = int(input("Take aim at the x-xalue: ")) 
    except ValueError: 
     print("\nI SAID TAKE AIM!\n") 
     guess_x = int(input("Take aim at the x-xalue: ")) 
    try: 
     guess_y = int(input("Take aim at the y-value: ")) 
    except ValueError: 
     print("\nDo you have wax in your ears?? AIM!\n") 
     guess_y = int(input("Take aim at the y-value: ")) 
    comp_x = randint(0, len(my_grid) - 1) 
    comp_y = randint(0, len(my_grid) - 1) 
    if x_one == guess_x and y_one == guess_y: 
     print("\nYou hit one! \n") 
     enemy_grid[guess_x - 1][guess_y - 1] = "H" 
     continue 
    elif x_two == guess_x and y_two == guess_y: 
     enemy_grid[guess_x - 1][guess_y - 1] = "H" 
     print("\nYou hit one! \n") 
     continue 
    elif enemy_grid[guess_x - 1][guess_y - 1] == "O": 
     print("\nYou've tried there before! Here's another round.\n") 
     print("You have " + str(guesses) + " rounds left, cadet.\n\n") 
     continue 
    elif enemy_grid.count("H") == 2: 
     print("\nYou got them all!\n") 
     break 
    else: 
     if guess_x not in range(10) or guess_y not in range(10): 
      print("\nThat's not even in the OCEAN!! Take another free round then.\n") 
      print("You have " + str(guesses) + " rounds left, cadet.\n\n") 
      continue 
     elif enemy_grid[guess_x][guess_y] == "O": 
      print("\nYou've tried there before! Here's another round.\n") 
      print("You have " + str(guesses) + " rounds left, cadet.\n\n") 
      continue 
     else: 
      print("\nYou missed, soldier!\n") 
      guesses = guesses - 1 
      print("You have " + str(guesses) + " rounds left, cadet.\n\n") 
      enemy_grid[guess_x - 1][guess_y - 1] = "O" 
      if comp_x == x_mio and comp_y == y_mio: 
       my_grid[comp_x - 1][comp_y - 1] = "H" 
       print("\nThe enemy hit you! \n") 
       continue 
      elif comp_x == x_mit and comp_y == y_mit: 
       my_grid[comp_x - 1][comp_y - 1] = "H" 
       print("\nThe enemy hit you! \n") 
       continue 
      elif my_grid.count("H") == 2: 
       print("We have to retreat! They've sunken all of your ships...") 
       break 
      else: 
       my_grid[comp_x - 1][comp_y - 1] = "=" 
       continue 

我正在使用python 3,如果这有什么区别。所以如果玩家在网格上击中正确的位置,那么它将显示为“H”而不是“=”或“O”。所以我只是想知道我是否可以计算那些“H”用来结束IF循环。再次感谢,并为此感到抱歉。

+3

问题是什么? – 2014-10-11 00:55:17

+1

破损的缩进使得代码非常难以遵循。功能之外的'全局'声明不起作用。你不需要用不同的名字('random_(row/col)_(one/two)')定义相同的函数四次!只定义一次,然后调用它四次。 – jacg 2014-10-11 00:56:16

+0

“_给我一点麻烦_”什么麻烦?请显示完整的错误信息。 – John1024 2014-10-11 01:20:23

回答

0

你还没有真正解释过这个问题,因此很多代码缺失,很难告诉你什么是错误的,但是我会猜测它。

我的猜测是你创建了一个'='网格来表示一个玩家的棋盘,然后如果他们的船被'命中',你用'H'代替那个位置上的'='。

创建(enemy_grid)的结构似乎看起来是这样的:

在这种情况下
[[====] 
[====] 
[====] 
[....]] 

测试,enemy_grid.count("H")没有任何意义,因为enemy_grid是包含其他列表的列表(这样的数Hs总是0--他们在第二层列表中更深)。

你可能想沿着线测试更多:

[cell for row in enemy_grid for cell in row].count('H') 
+0

谢谢!我刚刚重新考虑了我的代码,并且您的建议很有用!对不起,我无法更容易地传达我的问题,但你明白了。我只需切换IF语句放在我的代码中的位置,并且根据您的建议,它现在可以完美工作!谢谢! – 2014-10-11 06:05:54