如何在Python战舰游戏中将一个功能连接到另一个功能?

时间:2018-11-05 08:07:18

标签: python-3.x

看来,当输入坐标(例如13 45)时,它总是会被“遗失”,我认为这是因为功能gamemode_one()未链接到主要功能print_board(board)。船只的位置是随机生成的。以下是仅适用于初学者模式(1)的代码。

我还希望您能提供一些提示,以帮助在击中某艘船的坐标时如何对整个船进行遮罩? (船长5个字符)。

import random
from random import randint

board = []
board_size = 60

board_top = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]

#creating the board
def print_board(board):
    boardHeader=[]
    boardHeader.extend(board_top*6)
    print('    ', end='')
    for i in range(1,7):
        print(str(i).rjust(20), end="")
    print()
    print('    ', end='')
    for i in boardHeader:
        print(str(i).rjust(2), end='')
    print()

    for x in range(20):
        board.append(["#"] * board_size)

    row = 0
    for i in range(len(board)):
       print (str(row + 1).rjust(2), ' ', end = "")
       for j in range(len(board[row])):
           print(board[row][j].rjust(2), end='')
       row +=1
       print()
    return board
print("Let's play Battleship game!")            
print_board(board)

#generate random row and column        
def random_row(board):
    return randint(0, len(board) - 1)
def random_col(board):
    return randint(0, len(board[0]) - 1)
def random_ship(board):
    ship_row = random_row(board)
    ship_col = random_col(board) 
    return ([ship_row, ship_col])

#select game mode
selection = int(input("Select 1 to start or ""Q"" to quit "))    
if selection == 1:
    gamemode = int(input("Select mode: (1)Beginner Mode, (2)Intermediate Mode or (3)Advance  "))
    for i in range(5):
        if gamemode == "1" or gamemode == "2" or gamemode == "3":
            break
        elif gamemode == "q" or gamemode == "Q":
            print ("Sys: That's too bad. Hope to see you soon!")
            exit = True
            break
    else:
        exit

#generate random coordinates according to game mode 1 (Beginner)
def gamemode_one():
    mainShips = []
    for i in range (80):
        tempShips = []
        tempShipRand = random_ship(board)
        ship_row=tempShipRand[0]
        ship_col=tempShipRand[1]
        tempShips.append(tempShipRand)
        for i in range(1,5):
            tempShips.append([ship_row,ship_col+i])

        dup=False
        for i in tempShips:
            for j in mainShips:
                if i in j:
                    dup=True

        if not dup:
            mainShips.append(tempShips)
    return ([ship_row,ship_col])
    return mainShips


#Game mode 1
if gamemode == 1:
    ship_row = gamemode_one()[0]
    ship_col = gamemode_one()[1]
    ships = 1
    booms = 15
    while booms > 0 and ships > 0:
        board=[]
        limit_row=20
        limit_col=60
        guess_row,guess_col = map(int,input("Enter coordinates for row and column: ").split(" "))
        if guess_row==ship_row and guess_col==ship_col:
            print("BOOM!")
            print("O")
            booms -= 1
            ships -= 1
            print("You have", booms, "amount of booms left.")
        elif booms == 0:
            print ("You ran out of booms")
        elif ships == 75:
            game_ends() 
        elif guess_row>limit_row or guess_col>limit_col:
            print("Invalid coordinate!!")
            print("You have", booms, "amount of booms left.")
        else:
            guess_row!=ship_row or guess_col!=ship_col
            print("You missed")
            print(" ")
            booms -= 1
            print("You have", booms, "amount of booms left.")
    print_board(board)



def game_ends():
    if booms >= 13 and booms <= 15:
        print ("You are a novice")
    elif booms >= 10 and booms <= 12:
        print ("Not too bad")
    elif booms < 10:
        print ("You have a talent")
    return

1 个答案:

答案 0 :(得分:0)

您总是通过以下方式从gamemode_one()返回:

return (9,18)

没有随机性。

编辑后:

生成随机数后,您可以很容易地看到通过写入print(ship_row, ship_col)生成了正确的随机数。您将始终获得随机数。但是,您的board的大小为60x20,因此有1200个元素。因此,您的猜测输入等于随机数的概率约为0.1%,您将需要平均重复进行1200次猜测才能获得成功。

您的整个代码中也充满了冗余,未使用的部分,不必要的部分以及对我没有任何意义的内容。特别是您要比较的随机数是由ship_row = gamemode_one()[0]ship_col = gamemode_one()[1]生成的。 gamemode_one将使用全局board的尺寸作为随机变量的限制。除开头的print_board外,永远不要使用其内容。

我认为您不了解局部和全局变量背后的概念。只要让board是所有函数都使用的全局变量,就不要通过函数参数传递它并返回它。