在codeacademy第一步
board = []
for i in range(5):
board.append(["O"]*5)
print board
在codeacademy
第二步
board = []
def print_board(board):
for x in range(5):
board= (["O"] * 5)
print board
return board
print_board(board)
在codeacademy
第三步
board = []
def print_board(board):
for x in range(5):
board= (["O"] * 5)
print " ".join(board)
return board
print_board(board)
第4步
from random import randint
board = []
for x in range(0, 5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
board = " ".join(row)
return board
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board) - 1)
print print_board(board)
print random_row(board)
print random_col(board)
buttleship對codeacademy
第五步驟
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)
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: "))
最後步驟允許.. Play遊戲BettleShip CodeAcademy
from random import randint
板= [] 對於x在範圍(5): board.append([「O」] * 5)
def print_board(board): fo r排在板上: print「」.join(row)
print「讓我們玩戰艦吧!「 print_board(板)
DEF random_row(板): 返回randint(0,LEN(板) - 1)
DEF random_col(板): 返回randint(0,LEN(板[0 ]) - 1)
ship_row = random_row(板) ship_col = random_col(板) 打印ship_row 打印ship_col
變爲= 0 贏= 0 用於依次範圍(4): 如果反過來< = 5: guess_row = INT(的raw_input( 「猜行:」)) guess_col = INT(的raw_input( 「猜山口:」))
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk my battleship!"
win +=1
if win == 2:
break
else:
turns +=1
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
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!"
board[guess_row][guess_col] = "X"
print (turn + 1)
print_board(board)
elif turns == 3:
print "Game Over"
else:
print "Max Turn try is over Try Again"