如何简化这个战舰计划? (蟒蛇)

时间:2014-03-12 05:01:27

标签: python list if-statement random

忍受我,我是编程的新手,我知道这包含了很多不必要的代码。

有没有办法在没有代码永久延伸的情况下添加更多船只? 也是在我的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!"

1 个答案:

答案 0 :(得分:0)

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

  • 战斗类:每侧包含船只,得分方法,战区尺寸
  • 船级:船舶的大小,船舶受损的数量,船舶的位置

每当制作一个新的Battle类时,你都可以为它传递一个初始的船只列表。这些列表可以根据您的需要(只要船只可以放入战斗区域而不重叠)。