_TITLE "shoot test, press any to quit..."
'first get a function to return ture/false to shoot spot x, y if it is part of coverage or not
'DONE and what a cool function it is too!!!
'2018-05-18 It's working!!! time to test in real app
k$ = ""
restart:
shotCnt = 0: sunk = 0
colA = -1 '<<<<<<<<<<<<<<< don't forget to signal start!
'draw grid
rgb 999
drawGrid gox, goy, gsq, sqPerSide
''destroyer target
dx = rand(0, n1 - 1)
dx2 = dx + 1
dy = rand(0, n1)
'draw the Destroyer
LINE (gox
+ dx
* gsq
+ 2 * i
, goy
+ dy
* gsq
+ 2 * i
)-STEP(2 * gsq
- 4 * i
, gsq
- 4 * i
), , BF
'start shooting
shoot sx, sy
'accounting
hits(sx, sy) = 1
shotCnt = shotCnt + 1
'show shot
LINE (gox
+ sx
* gsq
, goy
+ sy
* gsq
)-STEP(gsq
, gsq
), , BF
'hit target first time?
sunk
= 1: rgb
999:
LOCATE 1, 1:
PRINT "Destroyer Hit! in";
STR$(shotCnt
);
" shots."
'first test cover COMMENT OUT ABOVE CODE AND UNCOMMENT following for cool graphic!
'm = 3
'WHILE 1
' FOR bump = 0 TO m - 1
' CLS
' FOR y = 0 TO n1
' FOR x = 0 TO n1
' FOR mm = m TO 2 STEP -1
' SELECT CASE mm
' CASE 8: rgb 9
' CASE 7: rgb 90
' CASE 6: rgb 900
' CASE 5: rgb 99
' CASE 4: rgb 990
' CASE 3: rgb 909
' CASE 2: rgb 999
' END SELECT
' IF cover(mm, bump, x, y) THEN
' LINE (gox + x * gsq + 1, goy + y * gsq + 1)-STEP(gsq - 2, gsq - 2), , BF
' END IF
' NEXT
' NEXT
' NEXT
' _DISPLAY
' _LIMIT 1
' NEXT
'WEND
SUB drawGrid
(x
, y
, sq
, n
) d = sq * n
LINE (x
+ sq
* i
, y
)-(x
+ sq
* i
, y
+ d
) LINE (x
, y
+ sq
* i
)-(x
+ d
, y
+ sq
* i
)
SUB rgb
(n
) ' New (even less typing!) New Color System 1000 colors with up to 3 digits
rand%
= INT(RND * (hi%
- lo%
+ 1)) + lo%
SUB shoot
(col
, row
) 'col, row aren't inputs so mush as outputs like a double function return wo input parameters
'i = nShips
'WHILE shipSunk(i) 'find smallest ship not sunk
' i = i - 1
'WEND
'SELECT CASE i 'm for modulus, d for direction to run a check from
' CASE nShips: m = 2 'still have destroyer
' CASE nShips - 1: m = 3 'still have sub
' CASE nShips - 2: m = 3 'still have cruiser
' CASE nShips - 3: m = 4 'still have battleship
' CASE nShips - 4: m = 5 'still have carrier
' CASE ELSE: TxtBx "990", "m", "Aren't all the Player's ships sunk?": m = 7
'END SELECT
'IF m <> lastm THEN
lastm = m 'lastm is solely needed for coloring in main
bc = 0 'if bc is reset to 0 each time don't need it shared
'END IF
IF colA
= -1 THEN 'col the Attact starts from notice it is random so player can't anticipate colA = rand%(0, n1): col = colA: row = rand(0, n1): bump = rand(0, m - 1): bc = 0
'm 7 is just in case some error occurs have fall back
cc = 1
rc = 0
WHILE rc
<= sqPerSide
'find a space to hit if one left in this column IF cover
(m
, bump
, col
, row
) THEN 'are we on a place to cover board row
= (row
+ 1) MOD sqPerSide
rc = rc + 1
row = row - 1
'still here means we checked all rows in col so check next col
col
= (col
+ 1) MOD sqPerSide
cc = cc + 1
'still here ? then up the bump
bc = bc + 1
m = 7 'error fall through
'rexamine all the hits and check all holes filled around them
'TO DO
' if all else fails
IF m
= 7 THEN 'just find a hole not tried yet and exit! lastm = 7
'FOR row = 0 TO n1
' FOR col = 0 TO n1
' IF hits(col, row) = 0 THEN EXIT SUB
' NEXT
'NEXT
END SUB ' 128 lines to take a random shot!!!!
'using a modulus m coverage with a bump so that opponent can't predict where
'the hardest place to plant the Detroyer will be
bm
= bump
MOD m
'make sure bump is in modulus
'turns out I did not need this but it should be noted:
' the cnt is different according to bump if modulus is odd
'amd maybe if nSquare is odd and modulus is even
'how many (non repeating) shots to cover a square n X n
FUNCTION coverageCnt
(nSquare
, modulus
, bump
) IF cover
(modulus
, bump
, x
, y
) THEN cnt
= cnt
+ 1 coverageCnt = cnt