from random import randint
Hidden_Pattern=[[' ']*8 for x in range(8)]
Guess_Pattern=[[' ']*8 for x in range(8)]
let_to_num={'A':0,'B':1, 'C':2,'D':3,'E':4,'F':5,'G':6,'H':7}
def print_board(board):
print(' A B C D E F G H')
print(' ***************')
row_num=1
for row in board:
print("%d|%s|" % (row_num, "|".join(row)))
row_num +=1
def get_ship_location():
#Enter the row number between 1 to 8
row=input('Please enter a ship row 1-8 ').upper()
while row not in '12345678':
print("Please enter a valid row ")
row=input('Please enter a ship row 1-8 ')
#Enter the Ship column from A TO H
column=input('Please enter a ship column A-H ').upper()
while column not in 'ABCDEFGH':
print("Please enter a valid column ")
column=input('Please enter a ship column A-H ')
return int(row)-1,let_to_num[column]
#Function that creates the ships
def create_ships(board):
for ship in range(5):
ship_r, ship_cl=randint(0,7), randint(0,7)
while board[ship_r][ship_cl] =='X':
ship_r, ship_cl = randint(0, 7), randint(0, 7)
board[ship_r][ship_cl] = 'X'
def count_hit_ships(board):
count=0
for row in board:
for column in row:
if column=='X':
count+=1
return count
create_ships(Hidden_Pattern)
turns = 10
while turns > 0:
print('\n\n============== Battleship ==============\n\n')
print_board(Guess_Pattern)
row,column =get_ship_location()
if Guess_Pattern[row][column] == '-':
print(' You already guessed that ')
elif Hidden_Pattern[row][column] =='X':
print(' Congratulations you have hit the battleship ')
Guess_Pattern[row][column] = 'X'
turns -= 1
else:
print('Sorry,You missed')
Guess_Pattern[row][column] = '-'
turns -= 1
if count_hit_ships(Guess_Pattern) == 5:
print("Congratulations you have sunk all the battleships ")
break
print(' You have ' +str(turns) + ' turns remaining ')
if turns == 0:
print('Game Over ')
bre
新华
- 粉丝: 1w+
- 资源: 628
最新资源
- 自动 wget 脚本用于下载并安装 PC 游戏所需的所有 .Net,VC++ 可再发行组件 + 一些其他工具(如 7zip、Powershell 等) 焦土红色政策 .zip
- 自动化连线工具使用教程
- ES7210-userGuide-REV1-0.pdf ES7210用户指南 ES7210规格书寄存器版
- 简单的包装器,用于从 .NET 应用程序渲染 hrtf DirectX,Xaudio2 声音.zip
- 简单的 dll 将提供一些信息来帮助在应用程序中挂接 directx shitcode.zip
- 简单的 DirectX 11 游戏引擎.zip
- 基于ASP.NET+SqlServer 实现的简单教学管理系统课程设计
- python进阶-04课程源码
- 简单的 DirectX 11 Hello World.zip
- curl的so库以及头文件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈