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'>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

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