**很簡單,注釋很詳細。附上。
#coding:utf-8
import sys
import random
"""地圖標記: map_
"""map_blank = 0 # 地圖空白
map_mine = 1 # 有雷存在
map_used = 2 # 已經排除
"""錯誤碼:error_
"""error_ok = 5 # 結果ok
error_fail = 6 # 結果失敗
error_null = 7 # 無效的輸入
error_pass = 8 # 遊戲勝利
class mine_sweep():
def __init__(self, row = 10, column = 10, mine_num = 20):
self.row = row # 地圖 行
self.column = column # 地圖 列
self.mine_num = mine_num # 地雷數量
# map_list row * column 的地圖
self.map_list = [[0 for i in range(self.row)] for j in range(self.column)]
# 用來顯示的地圖,在顯示時隱藏地雷
self.map_list_show = [[0 for i in range(self.row)] for j in range(self.column)]
def init_data(self):
"""初始化玩家資料, 隨機生成地雷"""
_map_list_ = [[0 for i in range(self.row)] for j in range(self.column)]
self.map_list = self.map_list or _map_list_
mine_num = self.mine_num # 確保重開不會影響到self.mine_num的值
while mine_num > 0:
map_x = random.randint(0, self.row) # 隨機生成列和行
map_y = random.randint(0, self.column)
if self.map_list[map_x][map_y] == map_blank: # 如果是隨機出相同的點了,重新隨機。
self.map_list[map_x][map_y] = map_mine
mine_num = mine_num - 1
def printf_map_list(self):
for i in range(self.row):
print(self.map_list[i])
# def game_show(self):
# print("歡迎來到掃雷遊戲")
# self.run()
def set_pos(self, x, y):
if self.map_list[x][y] == map_blank:
self.derived_mark(x, y, true)
self.derived_mark(x, y, false)
return error_ok
elif self.map_list[x][y] == map_mine:
print("你踩到雷了,game over")
self.map_list_show[x][y] = map_mine
return error_fail
else:
print("該處已經踩過了, 請選擇其它地方")
return error_null
def safe_input(self):
while true:
try:
pos = raw_input("x, y = ")
pos = pos.replace(' ', '')
x = int(pos[0:1])
y = int(pos[2:3])
if x >= 0 and x <= self.row \
and y >= 0 and y <= self.column:
break
else:
print("輸入的x y不在區間(0-7)中,超出了地圖的大小,請重新輸入")
except :
print("輸入的引數不是數字,請檢查重新輸入")
return x, y
def check_win(self):
for i in range(self.row):
for j in range(self.column):
if self.map_list[i][j] == map_blank:
return error_fail
return error_pass
def run(self):
status = map_blank
self.printf_map_list()
while status != error_fail:
print("請輸入x y做座標,逗號隔開,如果輸入多個引數,只選擇前兩個引數")
x, y = self.safe_input()
status = self.set_pos(x,y)
# 預設每次輸入之後會及時重新整理map_list,然後列印出來
self.printf_map_list()
res = self.check_win()
if res == error_pass:
print("恭喜你獲得勝利")
break
"""衍生標記,當輸入乙個點後,橫縱座標沒有雷的地方也都被掃空"""
def derived_mark(self, row, col, add):
x, y = row, col
while x >= 0 and x < self.row:
if self.map_list[x][y] != map_mine:
self.map_list[x][y] = map_used
self.map_list_show[x][y] = map_used
else:
break
if add:
x = x + 1
else:
x = x - 1
x, y = row, col
while y >= 0 and y < self.column:
if self.map_list[x][y] != map_mine:
self.map_list[x][y] = map_used
self.map_list_show[x][y] = map_used
else:
break
if add:
y = y + 1
else:
y = y - 1
def mine_game():
print(
"***************= 掃雷 **********====\n"
"1:遊戲開始\t"
"2:遊戲介紹\t"
"3:退出遊戲\n"
"***********************************=\n"
)while true:
try:
player = input("請輸入:")
if player >=1 and player <= 3:
break
else:
print("超出界限,無可選項,請重試。")
except:
print("請檢測輸如是否為數字(1-3)")
if player == 1:
obj = mine_sweep(8,8,16)
obj.init_data()
obj.run()
elif player == 2:
print("x,y輸入要選定的位置\n"
"如果觸發地雷,遊戲結束\n"
"非雷會衍生標記橫縱座標沒有雷的地方\n"
"祝遊戲愉快\n")
elif player == 3:
print("再見")
sys.exit()
if __name__ == "__main__":
while true:
mine_game()
用python寫了乙個zip檔案暴力破解的程式
這裡查閱許多資料,學習用python寫了乙個zip檔案暴力破解的程式。若有不足之處請大家多多指教!本隨筆僅供學習交流,切勿拿去做違法之事!import itertools as its import threading import zipfile import sys import time fr...
用java寫了乙個漢諾塔
package com.brzhang 漢諾塔 開始盤子全部都放在第一根柱子上 目的 將第一根柱子上的盤子全部移動到第三根柱子上,規則是不能編號較大的盤子放在編號交小的盤子上面。public class hanota else 移動盤子,從from移動到to借助assite param from p...
用css3 js寫了乙個鐘錶
先給個成品圖,最終結果是個樣子的 動態的 html 如下 div class dial div div class bigdiv bigdiv1 id secondhand div class secondhand div div div class bigdiv bigdiv2 id minute...