' update 2019-08-02 _title change line number so can reflect changes to mines constant.
' 2019-08-03 Minesweeper Custom Field.bas creates a file: "Custom Field Specs.txt"
' that you edit with your text editor, if you select that option in the opening screen menu.
' 2019-08-04 add assets can use to the customize file, also if the file exits don't rewrite a blank one.
' Thanks to SoundBible for sounds dowmloaded under License Attribution 3.0
' http://soundbible.com
' https://creativecommons.org/licenses/by/3.0/us/
DIM SHARED xmax
, ymax
, Xsq
, Ysq
, Xarrd
, Yarrd
, mines
, Xmargin
, Ymargin
'set all this in customField sub _TITLE "Minesweeper Custom wAssets" customField
ApplauseSnd
= _SNDOPEN("Applause.wav") 'ShipBellSnd
= _SNDOPEN("Ships bell.wav") 'rndSnd
(2) = _SNDOPEN("Carpet Ripping.wav") 'rndSnd
(5) = _SNDOPEN("ElectricMotor.wav") ''DIM i 'test load and sounds
'FOR i = 6 TO 6
' _SNDPLAY (rndSnd(i))
' PRINT i;
' IF rndSnd(i) = 0 THEN PRINT i; " not loaded." ELSE PRINT
' _DELAY 3
'NEXT
_TITLE STR$(mines
) + " Minesweeper: left click cell to reveal, right click to mark mine (red)" reveal
AS INTEGER ' 1 for marked, 0 hidden, -1 for revealedREDIM SHARED b
(0 TO Xarrd
+ 1, 0 TO Yarrd
+ 1) AS boardType
'oversize the board to make it easy to count mines DIM gameOver
, cc
, cr
, mbN
, c
, r
, s$
, sz!
restart = 1
gameOver = 0
mbN = 0
getCell cc, cr, mbN
'LOCATE 1, 1: PRINT cc, cr, mbN
IF b
(cc
, cr
).mine
THEN 'ka boom FOR r
= 1 TO Yarrd
'show all mines IF b
(c
, r
).mine
THEN b
(c
, r
).reveal
= -1: showCell c
, r
s$ = "KA - BOOOMMMM!"
sz!
= 1.2 * xmax
/ LEN(s$
) cText xmax / 2, ymax / 2, sz!, &HFF000000, s$
cText xmax / 2 - 4, ymax / 2 - 4, sz!, &HFFFF0000, s$
cText xmax / 2 - 8, ymax / 2 - 8, sz!, &HFFFFFF00, s$
gameOver = -1
b(cc, cr).reveal = -1: showCell cc, cr
IF b
(cc
, cr
).id
= 0 THEN sweepZeros cc
, cr
b(cc, cr).reveal = 0: showCell cc, cr
IF b
(cc
, cr
).reveal
= 0 THEN b
(cc
, cr
).reveal
= 1: showCell cc
, cr
s$ = "Good Job!"
sz!
= 1.2 * xmax
/ LEN(s$
) cText xmax / 2, ymax / 2, sz!, &HFF000000, s$
cText xmax / 2 - 1, ymax / 2 - 2, sz!, &HFF000055, s$
gameOver = -1
restart = 1
'set all this 'DIM SHARED xmax, ymax, Xsq, Ysq, XarrD, YarrD, mines, Xmargin, Ymargin
DIM fName$
, fe
, fLine$
, p
, inCnt
, beenHere
, w$
, allow$
, choice$
fName$ = "Custom Field Specs.txt"
PRINT " Minesweeper options:" PRINT " 1. Use mine field settings: 800 x 600 screen with 9 X 9 cells and 10 mines." PRINT " 2. Customize your own field settings." IF fe
THEN PRINT " 3. Use the last customized mine field settings.": allow$
= allow$
+ "3" PRINT " or press esc to quit." choice$ = getChar$(allow$)
editCustom:
IF fe
= 0 THEN 'need to start the file, otherwise just edit what's there PRINT #1, " Custom Field Specs for your Minesweeper Game" PRINT #1, " Please fill out the right side of all Equal signs." PRINT #1, " X dimensions across the screen:" PRINT #1, " Screen Width (pixels) = " PRINT #1, " Number of Horizontal Cells Across = " PRINT #1, " Cell Width (12 - 100? pixels) = " PRINT #1, " Y dimensions going down:" PRINT #1, " Screen Height (pixels) = " PRINT #1, " Number of Cells Down = " PRINT #1, " Cell Height (24 - 80? pixels) = " PRINT #1, "The percent of mines (8 easy - 15 hard) = " PRINT #1, " To finish, Save the file and then close the editor." ' I picked up this shortcut from Ken, normally I would call a text editor that I don't know if you have!
loadCustom:
beenHere = beenHere + 1 'we'll give it 5 tries
PRINT "OK we tried 5 times, going with default settings..." GOTO default
'exit from there inCnt = 0
WHILE EOF(1) = 0 ' look to get 7 values from 7 = signs 'PRINT fLine$
inCnt = inCnt + 1
CASE 1: xmax
= VAL(rightOf$
(fLine$
, "=")) CASE 2: Xarrd
= VAL(rightOf$
(fLine$
, "=")) CASE 3: Xsq
= VAL(rightOf$
(fLine$
, "=")) CASE 4: ymax
= VAL(rightOf$
(fLine$
, "=")) CASE 5: Yarrd
= VAL(rightOf$
(fLine$
, "=")) CASE 6: Ysq
= VAL(rightOf$
(fLine$
, "=")) CASE 7: mines
= VAL(rightOf$
(fLine$
, "=")) * Xarrd
* Yarrd
/ 100
'debug....................
'PRINT "xmax = "; xmax, "ymax = "; ymax, "Xarrd = "; Xarrd, "Yarrd = "; Yarrd
'PRINT "Xsq = "; Xsq, "Ysq = "; Ysq, "Mines = "; mines
'INPUT "OK... enter "; w$
IF ymax
>= Yarrd
* Ysq
THEN 'all good PRINT "Opps, Screen height is not big enough for Y cells * pixels down. " PRINT "Opps, Screen width is not big enough for X cells * pixels across. " PRINT "Y Cell pixels down probably not enough. " PRINT "X Cell pixels across probably not enough. " PRINT "We did not get everything filled out by = signs."
calcMargins:
Xmargin = (xmax - Xarrd * Xsq) / 2: Ymargin = (ymax - Yarrd * Ysq) / 2
default:
xmax
= 800: ymax
= 600: Xarrd
= 9: Yarrd
= 9: Xsq
= 60: Ysq
= 60: mines
= 10:
GOSUB calcMargins
DIM minesPlaced
, rx
, ry
, x
, y
, nMines
restart = 0
minesPlaced = 0
WHILE minesPlaced
< mines
b(rx, ry).mine = -1: minesPlaced = minesPlaced + 1
'count mines amoung the neighbors
IF b
(x
, y
).mine
<> -1 THEN 'not already a mine nMines = b(x - 1, y - 1).mine + b(x - 1, y).mine + b(x - 1, y + 1).mine
nMines = nMines + b(x, y - 1).mine + b(x, y + 1).mine
nMines = nMines + b(x + 1, y - 1).mine + b(x + 1, y).mine + b(x + 1, y + 1).mine
b(x, y).id = -nMines
b(x, y).id = 0
b(x, y).reveal = 0
showCell x, y
x = (c - 1) * Xsq + Xmargin: y = (r - 1) * Ysq + Ymargin
CASE -1:
IF b
(c
, r
).mine
THEN clr
= &HFF883300 ELSE clr
= &HFFFFFFFF 'revealed white with number of mine neighbors CASE 0: clr
= &HFF008800 'hidden green CASE 1: clr
= &HFFFF0000 'marked red LINE (x
, y
)-STEP(Xsq
- 1, Ysq
- 1), clr
, BF
LINE (x
, y
)-STEP(Xsq
- 1, Ysq
- 1), &HFF000000, B
IF Xsq
< Ysq
THEN sz
= .8 * Xsq
ELSE sz
= .8 * Ysq
IF b
(c
, r
).id
> 0 THEN cText x
+ Xsq
/ 2, y
+ Ysq
/ 2, sz
, &HFF000000, _TRIM$(STR$(b
(c
, r
).id
)) IF b
(c
, r
).mine
= -1 THEN cText x
+ Xsq
/ 2, y
+ Ysq
/ 2, sz
, &HFFFFFFFF, "*"
IF b
(x
, y
).reveal
= -1 AND b
(x
, y
).mine
= 0 THEN c
= c
+ 1 IF c
= Xarrd
* Yarrd
- mines
THEN TFwin
= -1
IF mb1
OR mb2
THEN ' get last place mouse button was down WHILE mb1
OR mb2
' wait for mouse button release as a "click" returnCol
= INT((mx
- Xmargin
) / Xsq
) + 1: returnRow
= INT((my
- Ymargin
) / Ysq
) + 1 IF returnCol
< 1 OR returnCol
> Xarrd
OR returnRow
< 1 OR returnRow
> Yarrd
THEN mbNum
= 0
SUB sweepZeros
(col
, row
) ' recursive sweep with Rod's limits set DIM c
, r
, cMin
, cMax
, rMin
, rMax
, x
, y
, id
c = col: r = row 'get copies for recursive sub
IF c
< Xarrd
- 1 THEN cMax
= c
+ 1 ELSE cMax
= Xarrd
IF r
< Yarrd
- 1 THEN rMax
= r
+ 1 ELSE rMax
= Yarrd
id = b(x, y).id
b(x, y).reveal = -1 'mark played
showCell x, y
sweepZeros x, y
b(x, y).reveal = -1
showCell x, y
'center the text around (x, y) point, needs a graphics screen!
'screen snapshot
mult! = textHeight / 16
xlen
= LEN(txt$
) * 8 * mult!
_PUTIMAGE (x
- .5 * xlen
, y
- .5 * textHeight
)-STEP(xlen
, textHeight
), I&
, cur&
getChar$ = k$