# 重制版
import random
from os import system
import time
import sys
sys.setrecursionlimit(15000)
def showc() : # Define show checkerboard's function
print("-----" * length + "-")
for h in range(height) :
for l in range(1,length + 1) :
print("|",end="")
print(c[h*length+l],end="")
print("|")
print("-----" * length + "-")
def isup(gid) :
if 1 < gid and gid < length :
return True
else :
return False
def isleft(gid) :
if gid % length == 1 and length < gid and gid != length * (height - 1) + 1 :
return True
else :
return False
def isright(gid) :
if gid % length == 0 and length < gid and gid != length * height :
return True
else :
return False
def isdown(gid) :
if length * (height - 1) + 1 < gid and gid < length * height :
return True
else :
return False
def islu(gid) :
if gid == 1 :
return True
else :
return False
def isru(gid) :
if gid == length :
return True
else :
return False
def isld(gid) :
if gid == length * (height - 1) + 1 :
return True
else :
return False
def isrd(gid) :
if gid == length * height :
return True
else :
return False
def mark(gid) :
global c
if gid < 10 :
if c[gid] == "00" + str(gid) + " " :
c[gid] = "00" + str(gid) + "S"
elif c[gid] == "00" + str(gid) + "S" :
c[gid] = "00" + str(gid) + "?"
elif c[gid] == "00" + str(gid) + "?" :
c[gid] = "00" + str(gid) + " "
else :
print("This grid is digged.")
elif gid < 100 :
if c[gid] == "0" + str(gid) + " " :
c[gid] = "0" + str(gid) + "S"
elif c[gid] == "0" + str(gid) + "S" :
c[gid] = "0" + str(gid) + "?"
elif c[gid] == "0" + str(gid) + "?" :
c[gid] = "0" + str(gid) + " "
else :
print("This grid is digged.")
else :
if c[gid] == str(gid) + " " :
c[gid] = str(gid) + "S"
elif c[gid] == str(gid) + "S" :
c[gid] = str(gid) + "?"
elif c[gid] == str(gid) + "?" :
c[gid] = str(gid) + " "
else :
print("This grid is digged.")
def countmine(gid,lu=0,up=0,ru=0,left=0,right=0,ld=0,down=0,rd=0) :
return mine[gid-length-1] * lu + mine[gid-length] * up + mine[gid-length+1] * ru + mine[gid-1] * left + mine[gid+1] * right + mine[gid+length-1] * ld + mine[gid+length] * down + mine[gid+length+1] * rd
def dig(gid) :
global c
global digged
global won
global yourturn
global mine
if mine[gid] == 1 :
if avoid :
set_comp = False
while set_comp == False :
pre_mine = random.randint(1,length*height)
if mine[pre_mine] == 0 :
mine[pre_mine] = 1
set_comp = True
mine[gid] = 0
dig(gid)
else :
print('You has been "mined"!')
won = 2 # lose
for i in range(1,length*height+1) :
if mine[i] == 1 :
if "S" in c[i] :
c[i] = " M √"
else :
c[i] = " M "
else :
if "S" in c[i] :
c[i] = " S ×"
system("cls")
showc()
else :
if islu(gid) :
if digged[gid] == 1 and yourturn :
print("This grid is digged.")
elif countmine(gid,0,0,0,0,1,0,1,1) == 0 :
yourturn = False
c[gid] = " "
digged[gid] = 1
if auto :
if digged[gid+1] == 0 :
dig(gid+1)
if digged[gid+length] == 0 :
dig(gid+length)
if digged[gid+length+1] == 0 :
dig(gid+length+1)
else :
c[gid] = " " + str(countmine(gid,0,0,0,0,1,0,1,1)) + "M "
digged[gid] = 1
elif isup(gid) :
if digged[gid] == 1 and yourturn :
print("This grid is digged.")
elif countmine(gid,0,0,0,1,1,1,1,1) == 0 :
yourturn = False
c[gid] = " "
digged[gid] = 1
if auto :
if digged[gid-1] == 0 :
dig(gid-1)
if digged[gid+1] == 0 :
dig(gid+1)
if digged[gid+length-1] == 0 :
dig(gid+length-1)
if digged[gid+length] == 0 :
dig(gid+length)
if digged[gid+length+1] == 0 :
dig(gid+length+1)
else :
c[gid] = " " + str(countmine(gid,0,0,0,1,1,1,1,1)) + "M "
digged[gid] = 1
elif isru(gid) :
if digged[gid] == 1 and yourturn :
print("This grid is digged.")
elif countmine(gid,0,0,0,1,0,1,1,0) == 0 :
yourturn = False
c[gid] = " "
digged[gid] = 1
if auto :
if digged[gid-1] == 0 :
dig(gid-1)
if digged[gid+length-1] == 0 :
dig(gid+length-1)
if digged[gid+length] == 0 :
dig(gid+length)
else :
c[gid] = " " + str(countmine(gid,0,0,0,1,0,1,1,0)) + "M "
digged[gid] = 1
elif isleft(gid) :
if digged[gid] == 1 and yourturn :
print("This grid is digged.")
elif countmine(gid,0,1,1,0,1,0,1,1) == 0 :
yourturn = False
c[gid] = " "
digged[gid] = 1
if auto :
if digged[gid-length] == 0 :
dig(gid-length)
if digged[gid-length+1] == 0 :
dig(gid-length+1)
if digged[gid+1] == 0 :
dig(gid+1)
if digged[gid+length] == 0 :
dig(gid+length)
if digged[gid+length+1] == 0 :
dig(gid+length+1)
else :
c[gid] = " " + str(countmine(gid,0,1,1,0,1,0,1,1)) + "M "
digged[gid] = 1
elif isright(gid) :
if digged[gid] == 1 and yourturn :
print("This grid is digged.")
elif countmine(gid,1,1,0,1,0,1,1,0) == 0 :
yourturn = False
c[gid] = " "
digged[gid] = 1
if auto :
if digged[gid-length-1] == 0 :
dig(gid-length-1)
if digged[gid-length] == 0 :
dig(gid-length)
if digged[gid-1] == 0 :
dig(gid-1)
if digged[gid+length-1] == 0 :
dig(gid+length-1)
if digged[gid+length] == 0 :
dig(gid+length)
else :
c[gid] = " " + str(countmine(gid,1,1,0,1,0,1,1,0)) + "M "
digged[gid] = 1
elif isld(gid) :
if digged[gid] == 1 and yourturn :
print("This grid is digged.")
elif countmine(gid,0,1,1,0,1,0,0,0) == 0 :
yourturn = False
c[gid] = " "
digged[gid] = 1
if auto :
if digged[gid-length] == 0 :
dig(gid-length)
if digged[gid-length+1] == 0 :
dig(gid-length+1)
if digged[gid+1] == 0 :
dig(gid+1)
else :
c[gid] = " " + str(countmine(gid,0,1,1,0,1,0,0,0)) + "M "
digged[gid] = 1
elif isdown(gid) :
if digged[gid] == 1 and yourturn :
print("This grid is digged.")
elif countmine(gid,1,1,1,1,1,0,0,0) == 0 :
yourturn = False
c[gid] = " "
digged[gid] = 1
if auto :
if digged[gid-length-1] == 0 :
dig(gid-length-1)
if digged[gid-length] == 0 :
dig(gid-length)
if digged[gid-length+1] == 0 :
dig(gid-length+1)
if digged[gid-1] == 0 :
dig(gid-1)
if digged[gid+1] == 0 :
dig(gid+1)
else :
c[gid] = " " + str(countmine(gid,1,1,1,1,1,0,0,0)) + "M "
digged[gid] = 1
elif isrd(gid) :
if digged[gid] == 1 and yourturn :
print("This grid is digged.")
elif countmine(gid,1,1,0,1,0,0,0,0) == 0 :
yourturn = False
c[gid] = " "
digged[gid] = 1
if auto :
if digged[gid-length-1] == 0 :
dig(gid-length-1)
if digged[gid-length] == 0 :
dig(gid-length)
if digged[gid-1] == 0 :
dig(gid-1)
else :
c[gid] = " " + str(countmine(gid,1,1,0,1,0,0,0,0)) + "M "
digged[gid] = 1
else :
if digged[gid] == 1 and yourturn :
print("This grid is digged.")
elif countmine(gid,1,1,1,1,1,1,1,1) == 0 :
yourturn = False
c[gid] = " "
digged[gid] = 1
if auto :
if digged[gid-length-1] == 0 :
dig(gid-length-1)
if digged[gid-length] == 0 :
dig(gid-length)
if digged[gid-length+1] == 0 :
dig(gid-length+1)
if digged[gid-1] == 0 :
dig(gid-1)
if digged[gid+1] == 0 :
dig(gid+1)
if digged[gid+length-1] == 0 :
dig(gid+length-1)
if digged[gid+length] == 0 :
dig(gid+length)
if digged[gid+length+1] == 0 :
dig(gid+length+1)
else :
c[gid] = " " + str(countmine(gid,1,1,1,1,1,1,1,1)) + "M "
digged[gid] = 1
do_mine = 0
for i in range(1,length*height+1) :
if digged[i] == 0 :
do_mine += 1
if do_mine == mines :
won = 1 # Win
for i in range(1,length*height+1) :
if mine[i] == 1 :
if i < 10 :
c[i] = "00" + str(i) + "S"
elif i < 100 :
c[i] = "0" + str(i) + "S"
else :
c[i] = str(i) + "S"
system("cls")
showc()
print("You win!")
print("Time:" + str(time.time() - time1) + " seconds")
if input("Type “help” for help,or enter to start the game.") in ["help","Help","HELP"] :
print("""For example :
-----------------
|01 |02S|02M|04 |
-----------------
|05 |06 |07S|01M|
-----------------
| |01M|01M|01M|
-----------------
| | | | |
-----------------
Represent for :
-----------------------------------------------------------------------------------------------
|Strings(texts) |Represent |
===============================================================================================
|Some numbers plus a space|Undigged grids |
-----------------------------------------------------------------------------------------------
|Some numbers plus "M" |Digged grids,and the numbers show how many mines in the 3*3 squeres|
-----------------------------------------------------------------------------------------------
|Nothing |Digged grids,and there is no mines in the 3*3 squeres |
-----------------------------------------------------------------------------------------------
|Some numbers plus "S" |Marked undigged grids |
-----------------------------------------------------------------------------------------------
|Some numbers plus "?" |Marked undigged grids with questions or problems |
-----------------------------------------------------------------------------------------------""")
else :
won = 0
length = int(input("Please input the checkerboard's length:"))
if length < 3 :
raise OverflowError("The checkerboard's length cannot less than 3")
height = int(input("Please input the checkerboard's height:"))
if height < 3 :
raise OverflowError("The checkerboard's height cannot less than 3")
c = [""]
if length * height < 1000 :
for i in range(1,length*height+1) :
if i < 10 :
list.append(c,"00"+str(i)+" ")
elif i < 100 :
list.append(c,"0"+str(i)+" ")
else :
list.append(c,str(i)+" ")
else :
raise OverflowError("The amount of grids of checkerboard cannot more than 999")
if input("Avoid touch a mine in the first step?(Y/N)") in ["Y","y"] :
avoid = True
else :
avoid = False
mines = int(input("Please input the amount of mines:"))
if mines < length * height - avoid :
miness = 0
mine = [0] * 1200
digged = [0] * (length*height+1)
while miness < mines :
pre_mine = random.randint(1,length*height)
if mine[pre_mine] == 0 :
mine[pre_mine] = 1
miness += 1
else :
raise OverflowError("The amount of mines cannot more than " + str(length*height-1-avoid))
if input("Digging automatically?(Y/N)") in ["Y","y"] :
auto = True
else :
auto = False
time1 = time.time()
while won == 0 :
yourturn = True
system("cls")
showc()
marks = 0
for i in c[1:(length*height+1)] :
if "S" in i :
marks += 1
print("Remaining mines:" + str(mines - marks))
setgrid = input("Please type the grid number do you want to do.")
try :
int(setgrid)
except :
print("Please input a valid grid number")
else :
setgrid = int(setgrid)
if setgrid in list(range(1,length*height+1)) :
print("Dig Mark/?/unmark Cancel")
ctrl = input("- - -")
if ctrl in ["D","d"] :
dig(setgrid)
avoid = False
elif ctrl in ["M","m"] :
mark(setgrid)
else :
print("Please input a valid grid number")