Battle ship
操作规程
You win!
Okay—now for the fun! We have the actual location of the ship and the player’s guess so we can check to see if the player guessed right.
For a guess to be right, guess_col should be equal to ship_col and guess_row should be equal to ship_row.
if guess_col == 0 and guess_row == 0:
print “Top-left corner.”
The example above is just a reminder about if statements.
说明
On line 29, add an if guess_row equals ship_row and guess_col equals ship_col.
If that is the case, please print out "Congratulations! You sank my battleship!"
源码
from random import randint
board = []
for x in range(0, 5):
board.append([“O”] * 5)
def print_board(board):
for row in board:
print ” “.join(row)
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input(“Guess Row:”))
guess_col = int(raw_input(“Guess Col:”))
print ship_row
print ship_col
if guess_row == ship_row and guess_col == ship_col:
print “Congratulations! You sank my battleship!”
生成控制台图示
接下来
Great! Of course, the player isn’t going to guess right all the time, so we also need to handle the case where the guess is wrong.
The example above prints out “O”, the element in the 3rd row and 4th column.
说明
Add an else under the if we wrote in the previous step.
Print out "You missed my battleship!"
Set the list element at guess_row, guess_col to "X".
As the last line in your else statement, call print_board(board) again so you can see the "X".
Make sure to enter a col and row that is on the board!
好玩的代码
from random import randint
board = []
for x in range(0, 5):
board.append([“O”] * 5)
def print_board(board):
for row in board:
print ” “.join(row)
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input(“Guess Row:”))
guess_col = int(raw_input(“Guess Col:”))
print ship_row
print ship_col
if guess_row == ship_row and guess_col == ship_col:
print “Congratulations! You sank my battleship!”
else:
print “You missed my battleship!”
board[guess_row][guess_col] = “X”
print_board(board)
图示