Python_10_Codecademy_10_Battleship!

<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'>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343

推荐阅读更多精彩内容

  • 有人说,韩剧太过肤浅,严重影响了年轻一代的价值观和是非观,让人沉迷于不切实际的幻想当中。而我想说,韩剧之所以风靡,...
    纠缠的离骚阅读 1,172评论 13 11
  • 用友爱的眼睛去发现美好的事物!
    屿森迷妹666阅读 178评论 0 0
  • 现如今,在线旅游发展如火如荼,竞技场犹如一片血海。无论互联网如何发展,旅游出行需求总是存在的,旅游信息的获取也是必...
    喻拓阅读 671评论 0 49
  • 正如张爱玲的“红玫瑰与白玫瑰”,平常夫妻要么没有纷争;要么所有的不满都会聚集,等待一件小事,就一起爆发,最终或继续...
    滑过_指尖阅读 123评论 0 0