【问题标题】:How to simplify this battleship program? (Python) [closed]如何简化这个战舰程序? (Python)[关闭]
【发布时间】:2014-03-12 05:01:27
【问题描述】:

请耐心等待,我是编程新手,我知道其中包含很多不必要的代码。

有没有一种方法可以添加更多船只而无需永久扩展代码? 也是在我的 while 循环下编写比较代码的更好方法。感谢您抽出宝贵时间阅读和评论。

from random import randint

board = []

for x in range(10):
    board.append(["O"] * 10)

# creating the "map" of "O"'s for an ocean - 10x10
def print_board(board):
    for row in board:
        print " ".join(row)

print "Let's play Battleship!"
print_board(board)

# random number for row from 0 to length of board (9) in this case
def random_row(board):
    return randint(0, len(board) - 1)

# random number for column from 0 to length of board (9) in this case
def random_col(board):
    return randint(0, len(board[0]) - 1)

# 1st ship, size = 1x1
ship_row = random_row(board)
ship_col = random_col(board)

# 2nd ship, size = 1x2
ship_row2 = random_row(board)
if ship_row2 == ship_row: #Did not want a ship coordinate to fall onto a previous ship
    ship_row2 = random_row(board) #so I set it to generate a new number if it did
ship_row2_1 = ship_row2 - 1 #2nd part of this ship, needs to be adjacent so I set - 1 (could be +1)
if not ship_row2_1 >= 0: # if first part is 0, then ship_row2_1 would be off the "map",
    ship_row2_1 += 2 #so I added 2 to bring it to a positive number


ship_col2 = random_col(board)#same things as above for the column code
if ship_col2 == ship_col:
    ship_col2 = random_col(board)
ship_col2_1 = ship_col2


turn = 0
ships_sunk = 0
while True:
    try:
        guess_row = int(raw_input("Guess Row:"))
        guess_col = int(raw_input("Guess Col:"))
        # so my code here basically says if you hit the 1x1 ship it should turn it to an X,
        #and not be able to hit it again.
        if (guess_row == ship_row and guess_col == ship_col) and (board[guess_row][guess_col] != "X"):
            print "\nCongratulations! You sunk a battleship!"
            print "One more to go!\n"
            board[guess_row][guess_col] = "X"
            print_board(board)
            ships_sunk += 1
            if ships_sunk == 2:
                print "\n You got them all! Pokemon!"
                break
        # here is the same but for the 1x2 ship, I had to turn them all to X to prevent user,
        # from hitting the same part of the "ship".
        elif ((guess_row == ship_row2 and guess_col == ship_col2) or \
        (guess_row == ship_row2_1 and guess_col == ship_col2_1)) and (board[guess_row][guess_col] != "X"):
            print "\nCongratulations! You sunk a battleship!"
            print "One more to go!\n"
            board[guess_row][guess_col] = "X"
            board[ship_row2][ship_col2] = "X"
            board[ship_row2_1][ship_col2_1] = "X"
            print_board(board)
            ships_sunk += 1
            if ships_sunk == 2:
                print "\n You got them all! Pokemon!"
                break
        else:
            if (guess_row < 0 or guess_row > (len(board) -1 )) or (guess_col < 0 or guess_col > (len(board[0]) - 1)):
                print "\nOops, that's not even in the ocean."
            elif(board[guess_row][guess_col] == "X"):
                print "\nYou guessed that one already."
            elif type(guess_row) is not int or type(guess_col) is not int:
                print "Type a number!"
            else:
                print "\nYou missed my battleship!"
                board[guess_row][guess_col] = "X"
                turn += 1
                print_board(board)
                if turn == 4:
                    print "Game Over"
                    break
    except ValueError:
        print "Please type a number!"

【问题讨论】:

  • 您的问题现在与您的标题不匹配。如果您想回答标题中的问题,您可能会在codereview.stackexchange.com 获得更好的运气(尽管它可能太宽泛了?)。否则,您可能会在这里将其分解为一两个问题,例如,“我怎样才能轻松添加更多船只?”只有代码的相关部分。

标签: python list if-statement random


【解决方案1】:

您需要以更灵活的方式存储“游戏状态”。决定玩家何时获胜的逻辑也是如此。一种方法是将您的应用程序分解为classes,可能类似于:

  • Battle class:包含双方战舰、计分方法、战区尺寸
  • 船舶等级:船舶大小、船舶遭受的损坏程度、船舶位置

每当创建一个新的 Battle 类时,您都可以向其传递双方的初始舰船列表。这些列表可以任意长(前提是舰船可以在不重叠的情况下进入战场)。

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    相关资源
    最近更新 更多