Home > other >  The manual version dig ray, welcome clap brick
The manual version dig ray, welcome clap brick

Time:11-26

Beginners Python, made up a manual digging, welcome to master program to provide improvements,

 import numpy as np 
Import the random
The random seed ()

ISMINEFLAG=0 x10
OPENEDFLAG=0 x20
MARKEDFLAG=0 x40
MINESFLAG=0 xf0
AROUNDFLAG=0 x0f

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

Mine 's 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 from the mine, otherwise 0
5: the opened flag.
6: marked flag
7: reserved
"" "
If dim [2] & gt;=dim dim [0] * [1] :
Print (" Provided mines is wining mime array items!" )
Return None
Try:
A=np. Ndarray ((dim dim [0], [1]), dtype=np, byte)
Except:
Print (" nparray construct failed!" )
Return None
A.f ill (0)
For mm in the random sample (range (dim dim [0] * [1]), dim [2]) :
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 +=int (bool (a [I, j] & amp; ISMINEFLAG))
A (x, y)=(a (x, y) & amp; MINESFLAG) + 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=(0, 1)) : # x, y, is mime the action 1, 2 is marked
X, y, a=act
If (a not in range (1, 3)) or (x not in range (arr. Shape [0])) or (y not in range (arr. Shape [1])) :
Print (" \ nAct prarmetier is wrong! \ n ", x, y, a)
Return
If a==2: # mark or unmark
If not testbit (arr (x, y), 5) :
Arr (x, y) ^=MARKEDFLAG
Elif a==1:
If testbit (arr (x, y), 5) : # from the Opened
Return
If testbit (arr (x, y), (4) :
Print (" \ n ({}, {}) from a mine! \ n ". The format (x, y))
The else:
Openmine (arr, x, y)
The else:
Pass

Def openmine (arr, x, y) : # from the opened
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) :
Arr (I, j] |=OPENEDFLAG
Openmine (arr, I, j)
The else:
Arr (x, y) |=OPENEDFLAG
Return

Def show (arr) :
For the x in the range (arr. Shape [0]) :
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 (" \ n ")
Return

If __name__=="__main__" :
While True:
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! \ n ")
The else:
Break
A=makemine ((xs and ys, mines))
# # print (a)
Show (a)
While True:
Try:
X, y, act=map (int, input (r "input x, y, act=1 or 2, SPACE seperates Nums. Ctrl + C exit) :"). The split ())
The action (a, (x, y, act))
# # print (" \ n ", a)
Show (a)
Except, ValueError:
Print (" \ nInput error! \ n ")

CodePudding user response:

Don't run

CodePudding user response:

The
reference 1/f, fill the gaps in the reply:
don't run
what tips? I can run normally

CodePudding user response:

Improved version, increased the double-click and double-click the continuous digging the thunder after the operation, increase the index clew, perfect the other details. There are other welcome expert pointed out that in need of improvement.
 import numpy as np 
Import the random
The random seed ()

ISMINEFLAG=0 x10
OPENEDFLAG=0 x20
MARKEDFLAG=0 x40
AROUNDFLAG=0 x0f

Def makemine (dim=,10,50) (10) :
"" "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> nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related