Home > other >  Manual digging ray end version
Manual digging ray end version

Time:11-26

The final version, increase the time display, ray number display and end test content, optimized the details,
 import numpy as np 
Import the random
Import a datetime
The random seed ()

ISMINEFLAG=0 x10
OPENEDFLAG=0 x20
MARKEDFLAG=0 x40
ISMMAKFLAG=0 # x50 ISMINEFLAG and MARKEDFLAG
ISOPMAFLAG=0 # x70 ISMINEFLAG and OPENEDFLAG and MARKEDFLAG
AROUNDFLAG=0 x0f

Def makemine (dim) :
"" "Make a mime ndarray, default size is 10 * 10, the default mines is 50

Mime - array item value type is np. The byte in 8 who, the details as follows:
7 -- -- -- -- -- - 0
XXXXXXXX
0-3: surround mines (0 to 8 is valid)
4: mime flag. 1 if mime, otherwise 0
5: the opened flag. 1 means the opened.
6: marked flag. 1 means marked
7: reserved
"" "
If dim [2] & gt;=dim dim [0] * [1] :
Print (" Provided mines are wining mime - array items!" )
Return None
Try:
A=np. Zeros ((dim dim [0], [1]), dtype=np, byte)
Except:
Print (" mime - array constructed failed!" )
Return None
For the mm in the random sample (range (dim dim [0] * [1]), dim [2]) : # mark all rand mines
X, y=divmod (mm, dim [1])
A (x, y) |=ISMINEFLAG
For x in the range (dim [0]) :
For y in range (dim [1]) :
V=0
For I in range (x + x - 1, 2) :
For j in range (1 y, y + 2) :
If 0 & lt;=iV +=(a (I, j] & amp; ISMINEFLAG) & gt;> 4
A (x, y) +=v
Return a

Def testbit (bit: np byte, n: int) :
If n>=8 or n<0:
Return None
Return a bool (bit& (1 & lt;
Def action (arr, act) : # x, y, is mime the action 1, 2 is marked, 3 is double click, return marked number
X, y, a=act
If (a not in range (1, 4)) or (x not in range (arr. Shape [0])) or (y not in range (arr. Shape [1])) :
Print (" \ nAct prarmetier ({}, {}, {}) is wrong! \ n ". The format (x, y, a))
Return 0
If a==2: # mark or unmark
If not testbit (arr (x, y), 5) :
Arr (x, y) ^=MARKEDFLAG
Return 1 if the bool (arr (x, y) & amp; MARKEDFLAG) else - 1
Elif a==1:
If testbit (arr (x, y), 5) : # from the Opened
Return 0
If testbit (arr (x, y), (4) :
Print (" \ n ({}, {}) from a mine!" . The format (x, y))
The else:
Openmine (arr, x, y)
Elif a==3:
If testbit (arr (x, y), 5) : # the opened the item can be doubl - clicked
For I in range (x + x - 1, 2) :
For j in range (1 y, y + 2) :
If 0 & lt;=iIf not bool (arr [I, j] & amp; ISOPMAFLAG) :
Arr (I, j] |=OPENEDFLAG
If (arr (I, j] & amp; AROUNDFLAG)==0: # continue mine after doubleclick and found surround mines is 0
Openmine (arr, I, j)
Return 0

Def openmine (arr, x, y) :
If (arr (x, y) & amp; AROUNDFLAG)==0:
For I in range (x + x - 1, 2) :
For j in range (1 y, y + 2) :
If 0 & lt;=iIf not testbit (arr [I, j), 5) : # from the opened
Arr (I, j] |=OPENEDFLAG
Openmine (arr, I, j)
The else:
Arr (x, y) |=OPENEDFLAG
Return

Def show (arr) :
Print (" \ n ", end="")
For I in range (0, arr. Shape [1], 2) :
Dd=3 if i<10 the else 2
Print (I, end="" * dd)
Print ()
Print (" - "* (2 * (arr. Shape [1] + 1) + 1))
For the x in the range (arr. Shape [0]) :
Print (STR (x). Rjust (2), end="|")
For y in range (arr. Shape [1]) :
If testbit (arr (x, y), 5) : # from the opened
Print (arr (x, y) & amp; AROUNDFLAG, end="")
The else:
If testbit (arr (x, y), 6) : # has marked
Print (r "?" , end="")
The else:
Print (r "*", end="")
Print ()
Return

Def testover (arr) :
For z in arr. Flat:
If a bool (z& ISMINEFLAG) :
If (z& ISMMAKFLAG)!=ISMMAKFLAG:
Return False
The else:
If not bool (z& OPENEDFLAG) :
Return False
The else:
Return True

If __name__=="__main__" :
Try:
Xs and ys, mines=map (int, input (r "input xs and ys, mines, SPACE seperates nums. Ctrl + C exit) :"). The split ())
If xs<0 or ys<0 or mines> Xs * ys:
Raise ValueError
Except, ValueError:
Print (" \ nInput xs and ys, mines the error! Use the default (10,10,50) \ n ")
Xs and ys, mines=(10,10,50)
A=makemine ((xs and ys, mines))
Show (a); Marks=0; Remains=1
T1=datetime. Datetime. Now ()
While True:
Try:
X, y, act=map (int, input (r "input x, y, act. Act=1 or 2 or 3, means mime, mark, doubleclick. SPACE seperates three nums Ctrl + C exit. - & gt;" ). The split ())
Marks +=action (a, (x, y, act)); Remains=mines - marks
T=a datetime. Datetime. Now () - t1
Mm, ss=divmod (t.s econds, 60). Hh, mm=divmod (mm, 60)
Print (" \ nArray sizes: {}, {} * Total: {} mines, Marked: {} mines, Remained: {} mines, informs the time: {}, {}, {} ". The format (xs and ys, mines, marks, and remains, hh, mm, ss), end="")
Show (a)
Print ()
Except, ValueError:
Print (" \ nInput x, y, act error! \ n ")
If remains==0:
If testover (a) :
Print (" Congratulation. The Game is over! \ n ")
Break
  • Related