<a href="http://www.jianshu.com/p/54870e9541fc">总目录</a>
课程页面:https://www.codecademy.com/
内容包含课程笔记和自己的扩展折腾
Welcome to Battleship!
In this project you will build a simplified, one-player version of the classic board game Battleship! In this version of the game, there will be a single ship hidden in a random location on a 5x5 grid. The player will have 10 guesses to try to sink the ship.
To build this game we will use our knowledge of lists, conditionals and functions in Python. When you're ready to get started, click run to continue.
"""
以下我的代码依然是充满了额外折腾,
codecademy上给的代码会简单很多
"""
# 第一步: 写一个打印board的function
board = []
for i in range(5):
board.append(["[O]"]*5 + [str(i)])
board.append([" 0 ", " 1 ", " 2 ", " 3 ", " 4 "])
def print_board(board):
for row in board:
print " ".join(row)
# 第二步: 开始游戏
from random import randint
while 2 == 2:
if raw_input("Are you ready? Enter Y or N. ") == "Y":
print "\n Let's play Battleship!"
break
def random_number():
return randint(0, 4)
ship_row = random_number()
ship_col = random_number()
turn = 0
for i in range(4):
print
print
print "Turn: %s/4" % (i+1)
print_board(board)
guess_row = int(raw_input("Guess row: "))
guess_col = int(raw_input("Guess column: "))
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk my battleship!"
break
elif guess_row < 0 or guess_col > 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]"
if turn == 3:
print
print
print "Game over"
board[ship_row][ship_col] = "[*]"
print "Here is the ship!"
print_board(board)
turn += 1
Console:
Are you ready? Enter Y or N. N
Are you ready? Enter Y or N. N
Are you ready? Enter Y or N. N
Are you ready? Enter Y or N. N
Are you ready? Enter Y or N. Y
Let's play Battleship!
Turn: 1/4
[O] [O] [O] [O] [O] 0
[O] [O] [O] [O] [O] 1
[O] [O] [O] [O] [O] 2
[O] [O] [O] [O] [O] 3
[O] [O] [O] [O] [O] 4
0 1 2 3 4
Guess row: 1
Guess column: 1
You missed my battleship!
Turn: 2/4
[O] [O] [O] [O] [O] 0
[O] [X] [O] [O] [O] 1
[O] [O] [O] [O] [O] 2
[O] [O] [O] [O] [O] 3
[O] [O] [O] [O] [O] 4
0 1 2 3 4
Guess row: 2
Guess column: 1
You missed my battleship!
Turn: 3/4
[O] [O] [O] [O] [O] 0
[O] [X] [O] [O] [O] 1
[O] [X] [O] [O] [O] 2
[O] [O] [O] [O] [O] 3
[O] [O] [O] [O] [O] 4
0 1 2 3 4
Guess row: 2
Guess column: 1
You guessed that one already.
Turn: 4/4
[O] [O] [O] [O] [O] 0
[O] [X] [O] [O] [O] 1
[O] [X] [O] [O] [O] 2
[O] [O] [O] [O] [O] 3
[O] [O] [O] [O] [O] 4
0 1 2 3 4
Guess row: 5
Guess column: 5
Oops, that's not even in the ocean.
Game over
Here is the ship!
[O] [O] [O] [O] [O] 0
[O] [X] [O] [O] [O] 1
[O] [X] [O] [O] [O] 2
[O] [O] [O] [O] [O] 3
[O] [O] [O] [O] [*] 4
0 1 2 3 4
Process finished with exit code 0
划重点:
-
randint(a, b)
:- 包含b!!
from random import randint
- 譬如:掷硬币:
randint(0, 1)
;掷骰子:randint(1, 6)
int()
: 能把string变成integer
n = raw_input("Give me an integer: ")
print type(n)
n = int(n)
print type(n)
Output:
Give me an integer: 7
<type 'str'>
<type 'int'>