Auto commit

上级 2b5a9659
print('欢迎来到 InsCode')
def sendMail():
\ No newline at end of file
import random
def draw_dot(level):
print(' ', end=' ')
for i in range(level):
print('-', end='-')
print('-')
def draw_head(level):
print(' ', end=' ')
for i in range(level):
print(chr(i+65), end=' ')
print()
def draw_board(board,level):
draw_head(level)
draw_dot(level)
for i in range(10):
print(i,end=' | ')
for j in range(level):
print(board[i][j], end = ' ')
print('| ' + str(i))
draw_dot(level)
draw_head(level)
def new_board(level):
board = []
for x in range(10):
board.append([])
for y in range(level):
board[x].append('-')
return board
def lay_mines(num, level):
mines = []
i = 0
while i < num:
mine = [random.randint(0, 9), random.randint(0, level-1)]
if mine not in mines:
mines.append(mine)
i += 1
return mines
def dig_mine(x, y, mines, level):
if [x, y] in mines:
board[x][y] = 'x'
return
sum = 0
for xdirection, ydirection in CO:
new_x = x
new_y = y
new_x += xdirection
new_y += ydirection
if 0<=new_x<=9 and 0<=new_y<level:
if [new_x, new_y] in mines:
sum += 1
board[x][y] = sum
if sum == 0:
board[x][y] = ' '
for xdirection, ydirection in CO:
new_x = x
new_y = y
new_x += xdirection
new_y += ydirection
if 0<=new_x<=9 and 0<=new_y<level:
if board[new_x][new_y] != ' ':
dig_mine(new_x, new_y, mines,level)
def is_valid_move(move:str, level:int):
warning = '输入有误,请输入正确格式,比如3H、3h、h3、H3:'
if len(move)!=2:
print(warning)
return False
if move[0].isdigit():
if any([ord(move[1].upper())>64+level, ord(move[1].upper())<65]):
print(warning)
return False
else:
return move.upper()
else:
if any([ord(move[1])>57, ord(move[1])<48, ord(move[0].upper())>(64+level), ord(move[0].upper())<65]):
print(warning)
return False
else:
return move[::-1].upper()
def is_win(board, mines, level):
# print(mines)
for i in range(10):
for j in range(level):
if [i, j] not in mines and board[i][j] == '-':
return False
for i, j in mines:
board[i][j] = 'O'
draw_board(board, level)
print('真棒,你赢了!')
return True
def is_lose(board, mines, level):
for i, j in mines:
if board[i][j] == 'x':
draw_board(board, level)
print('很遗憾,你输了 T-T')
return True
return False
def play_again():
while True:
answer = input('是否要再来一局?(y/n):')
if answer.lower() == 'y':
return True
elif answer.lower() == 'n':
return False
else:
print('输入有误,请输入 y 或 n')
CO = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]
while True:
print('欢迎来玩扫雷小游戏')
player_choose = input('请选择难度(1-简单,2-困难):')
if player_choose!='1' and player_choose!='2':
break
level = 10 * int(player_choose)
board = new_board(level)
mines = lay_mines(level, level)
draw_board(board,level)
while True:
move = is_valid_move(input('请选择位置:'), level)
if not move: continue
row = ord(move[0])-48
column = ord(move[1])-65
dig_mine(row, column, mines, level)
if is_win(board, mines, level) or is_lose(board, mines, level): break
draw_board(board,level)
if not play_again(): break
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册