Battleship——疫情下的自我调节

最近疫情呆在家中不敢走动,看看有什么游戏,发现了这个battleship

玩了几把ing有意思,找来代码,发现还挺自嗨

做了较大的变化在:

加入语音功能

加入虚拟盲猜对手

大家一起无聊无聊

"""
codecademy上给的代码简单很多
"""
from random import randint
import time
import pyttsx3

engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate + 60)  # 调准语速


def say(self):
    print('****', self)
    engine.say(self)
    engine.runAndWait()


# 第一步: 写一个打印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))


# 第二步: 开始游戏


while 2 == 2:
    say("准备好了吗? 输入 y 或者 n")
    if input("准备好了吗? 输入 y 或者 n. ") == "y":
        print("\nLet's play Battleship!")
        break


def random_number():
    return randint(0, 4)


ship_row = random_number()
ship_col = random_number()
# print('敌人偷偷把舰艇放在了', ship_row, ship_col)
turn = 0
say("部署你的战舰行坐标")

my_row = int(input("部署你的战舰行坐标: "))
say("部署你的战舰列坐标")
my_col = int(input("部署你的战舰列坐标: "))
say("你把自己的舰艇放在了坐标[{},{}]".format(my_row, my_col))
say('战争爆发,进入一级战备状态!')
for i in range(4):
    print("当前回合数: %s/4" % (i + 1))
    say("当前回合数:{}".format(i+1))
    print_board(board)
    he_guessrow = random_number()
    he_guesscol = random_number()
    # print('敌人猜测你的军舰在', he_guessrow, he_guesscol)
    say('敌人猜测你的军舰在[{},{}]'.format(he_guessrow, he_guesscol))
    if he_guessrow == my_row and he_guesscol == my_col:
        # print("我方舰艇被击中!")
        say("我方舰艇被击中!")
        break
    elif (he_guessrow < 0 or he_guessrow > 4) or (he_guesscol < 0 or he_guesscol > 4):
        # print("Oops,炮弹落在海域外面了.")
        say("Oops,炮弹落在海域外面了.")
    elif board[he_guessrow][he_guesscol] == "[M]":
        say("他们太蠢了,炮弹打到同一个敌方去了")
    else:
        say("耶,没有被敌舰发现!")
        board[he_guessrow][he_guesscol] = "[M]"
    print_board(board)
    # time.sleep(2)
    say('轮到我们出击了')
    say('设定导弹行坐标')
    guess_row = int(input("设定导弹行坐标: "))
    say('设定导弹列坐标')
    guess_col = int(input("设定导弹列坐标: "))
    if guess_row == ship_row and guess_col == ship_col:
        say("【击中敌方舰艇!】")
        break
    elif (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
        say("【Oops,炮弹落在海域外面了.】")
    elif board[guess_row][guess_col] == "[X]":
        say("【这个地方已经被探测过了】")
    else:
        say("【落空了,我们没有发现敌舰】")
        board[guess_row][guess_col] = "[X]"
    if turn == 3:
        say("回合数到,游戏结束")
        board[ship_row][ship_col] = "[*]"
        say("敌人的舰艇在[*]这里!")
        board[my_row][my_col] = "[@]"
        say("我们的舰艇在[@]这里!")
        print_board(board)
    turn += 1

 

上一篇:【网易官方】极客战记(codecombat)攻略-森林-宠物小测试pet-quiz


下一篇:用代码移动桌面图标(贪吃蛇)