[英]A battleship in Python
对于学校项目,我必须制作战舰游戏。 在游戏中,一个玩家有4艘飞船,并且如果一个玩家的所有飞船被摧毁,则游戏结束。 我正在尝试使此功能正常工作。
这是代码:
board = []
for x in range(10):
board.append(["O"] * 10)
def print_board(board):
for row in board:
print " ".join(row)
print "Let's play Battleship!"
print_board(board)
def Input_row1(board):
return int(raw_input("In what row do you want to place your first ship?"))
def Input_col1(board):
return int(raw_input("In what col do you want to place your first ship?"))
def Input_row2(board):
return int(raw_input("In what row do you want to place your second ship?"))
def Input_col2(board):
return int(raw_input("In what col do you want to place your second ship?"))
def Input_row3(board):
return int(raw_input("In what row do you want to place your third ship?"))
def Input_col3(board):
return int(raw_input("In what col do you want to place your third ship?"))
def Input_row4(board):
return int(raw_input("In what row do you want to place your fourth ship?"))
def Input_col4(board):
return int(raw_input("In what col do you want to place your fourth ship?"))
ship_row1 = Input_row1(board)
ship_col1 = Input_col1(board)
ship_row2 = Input_row2(board)
ship_col2 = Input_col2(board)
ship_row3 = Input_row3(board)
ship_col3 = Input_col3(board)
ship_row4 = Input_row4(board)
ship_col4 = Input_col4(board)
for turn in range(9):
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
if guess_row == ship_row1 and guess_col == ship_col1 or guess_row == ship_row2 and guess_col == ship_col2 or guess_row == ship_row3 and guess_col == ship_col3 or guess_row == ship_row4 and guess_col == ship_col4:
print "Congratulations! You sunk my battleship!"
if True:
total_ships = 4
total_ships = total_ships - 1
print total_ships
if total_ships == 0:
print "You destroyed all hostile ships!"
else:
if (guess_row < 0 or guess_row > 9) or (guess_col < 0 or guess_col > 9):
print "Oops, that's not even in the ocean."
elif(board[guess_row][guess_col] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
if turn == 9:
print "GAME OVER"
if turn == 9:
print "Game Over"
board[guess_row][guess_col] = "X"
print "Turn", turn + 1
print_board(board)
问题是,在击中第二艘船之后,total_ships保持为3,而不变为2。为什么这不能正常工作?
这是你的问题:
total_ships = 4
total_ships = total_ships - 1
您每次都重置total_ships
。 以上等同于:
total_ships = 3
您应该在轮到循环之外进行total_ships
的初始化。 因此,最好的办法是创建一个游戏循环函数和一个处理特定回合的函数,因此您不仅拥有一个长长的代码文件。
您的程序有很多奇怪的事情。 由于这是一个学校项目,所以我不确定在这里应该重写多少,但是这里有一些评论:
对于您的主要问题,正如纳撒尼尔·福特(Nathaniel Ford)回答的那样,问题是您将total_ships
重置为4,然后在每次total_ships
递减。 在程序开始时仅初始化一次,而不在turns循环中初始化。
在同一循环中,有一行
if True:
我不知道您的意思是什么,但是可能没有按照您的意愿去做。 由于始终执行if
内的代码块,因此它是完全没有用的(因此,您最好将if
完全删除,或者更可能重写测试,以便它执行您想要的操作)。
您在一开始就声明了一个称为board
的10x10数组,但从未使用过。 如果创建板子(这似乎是个好主意),则不应将每个战舰的位置存储在单独的变量中( ship_row1
, ship_col1
等)。 相反,当用户选择船的位置(i, j)
时,应将单元board[i][j]
为1
以指示那里有一艘船,并完全摆脱ship_col
和ship_row
变量。 这样一来,为所有船只选择位置的方法就可以通过循环的方式完成,并要求x
和y
(行和列)并更改板上的相应单元格(想象一下,用您编写的代码放置20艘船只,这会很多复制/粘贴...)。 这也将使检查用户没有将两艘飞船放置在同一位置变得更加简单(在您当前的实现中,当放置第四艘飞船时,您应该添加一个长检查以查看是否
ship_col4 == ship_col1 and ship_row4 == ship_row1 or ship_col4 == ship_col2 and ship_row4 == ship_row2 or ship_col4 == ship_col3 and ship_row4 == ship_row3
可以通过检查板上的单元格是否已经为1来简单地替换它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.