Author Topic: How to check if a box has been completed?  (Read 6107 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
How to check if a box has been completed?
« on: November 12, 2021, 01:25:37 pm »
So thanks to some help, the seeker no longer gets stuck. and I have implemented collision between seeker and gapper.
now anybody have a thought on how I should go about checking for completed boxes?
I thought about using an array and check the status of the lines with point.
something like

Code: QB64: [Select]
  1.  SUB Check_Box
  2. DIM Grid_Check(7, 6) AS _BYTE 'largest board(8) is 8x7(0-7,0-6)
  3.   SELECT CASE G.Level
  4.    CASE 1 '3x3(0-2,0-2)
  5.     'check horizontals
  6.     FOR Iy%% = 0 TO 2
  7.      FOR Ix%% = 0 TO 2 'check top line
  8.       FOR x%% = 0 TO 60 STEP 6 'check 10 points
  9.        IF POINT(4 + x%% + 60 * Ix%%, 10) = Magenta THEN full%% = TRUE ELSE full%% = FALSE: x%% = 66
  10.       NEXT x%%
  11.       IF full%% THEN Grid_Check(Ix%%, Iy%%) = TRUE
  12.      NEXT Ix%%
  13.     NEXT Iy%%
  14.     'check verticals
  15.  
The more I look at that, the more it seems like its not going to work as I intend. I gave some thought to using a UDT with UP,DOWN,LEFT,RIGHT to store the completion status of each box, but didn't like the thought of having so much overlap and double checks going on all the time.

I would really appreciate any input on possible methods to for completed boxes.

progress so far:
Code: QB64: [Select]
  1. 'Gapper(1986) Clone
  2. '10\25\2019 Cobalt
  3. 'ÃInital Start; Layout, Gapper, Seeker, L1 Grid finished [13:55EDT]
  4. 'ÀCorreted 'Catcher' to 'Seeker' as per original game
  5. '10\31\2021
  6. 'ÃFixed Seeker movement issue with help from Bplus and Keybone, used ABS as per Bplus
  7. 'Àadded startup screen, player initials input,game options
  8. '11\1\2021
  9. 'ÃAdded the instructions screens
  10. 'Àenabled collision of Seeker and Gapper
  11. '11\11\2021
  12. 'ÃFinished all 8 boards
  13. '11\12\2021
  14.  
  15. TYPE GameData
  16.  Player AS STRING * 4
  17.  Score AS LONG
  18.  Lives AS _BYTE
  19.  Level AS _BYTE
  20.  GX AS INTEGER 'Gapper X location
  21.  GY AS INTEGER 'Gapper Y location
  22.  GD AS _BYTE 'Gapper direction
  23.  SX AS INTEGER
  24.  SY AS INTEGER
  25.  SS AS _BYTE 'Seeker speed
  26.  SD AS _BYTE 'Seeker direction
  27.  Start AS _BYTE 'is level started or not(nothing happens until started)
  28.  FPS AS INTEGER
  29.  Quit AS _BYTE
  30.  Caught AS _BYTE 'has the seeker caught the gapper?
  31.  
  32. TYPE High_Scores
  33.  Nam AS STRING * 4
  34.  Score AS LONG
  35.  Level AS INTEGER
  36.  
  37. CONST TRUE = -1, FALSE = NOT TRUE, Gapper = 1, Seeker = 2
  38. CONST UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4
  39. CONST Key_Right = 19712, Key_Left = 19200, Key_Up = 18432, Key_Down = 20480
  40. CONST Key_Space = 32, Key_Enter = 13
  41. CONST Cyan = &HFF10E0E0~& '_RGB32(0, 170, 170)
  42. CONST Magenta = &HFFC040C0~&
  43. CONST OffWhite = &HFFC0C0C0~&
  44. CONST Brown = &HFFB06000~&
  45. CONST Yellow = &HFFFFFF00~&
  46. CONST Black = &HFF000000~&
  47.  
  48. SCREEN _NEWIMAGE(640, 440, 32)
  49. DIM SHARED Layer(10) AS LONG, G AS GameData, Frames%
  50. DIM SHARED Scores(10) AS High_Scores
  51.  
  52. Layer(0) = _DISPLAY
  53. Layer(1) = _NEWIMAGE(640, 440, 32) 'mix layer
  54. Layer(2) = _NEWIMAGE(320, 220, 32) 'Grid Layer
  55. Layer(3) = _NEWIMAGE(640, 440, 32) 'Sprite layer
  56. Layer(4) = _NEWIMAGE(320, 220, 32) 'Info layer
  57. Layer(5) = _NEWIMAGE(640, 440, 32) 'Collision 'Gapper\Catcher' layer
  58. Layer(6) = _NEWIMAGE(640, 440, 32) 'Collision 'Grid' layer
  59. Layer(7) = _NEWIMAGE(640, 440, 32) 'debug layer
  60. Layer(8) = _NEWIMAGE(320, 220, 32) 'Menu layer
  61. Layer(9) = _NEWIMAGE(320, 240, 32) 'instructions layer
  62. Layer(10) = _NEWIMAGE(320, 8, 32) 'title bar layer
  63.  
  64. _FONT 8, Layer(4)
  65. _FONT 8, Layer(8)
  66. _FONT 8, Layer(9)
  67. _FONT 8, Layer(10)
  68. _CLEARCOLOR _RGB32(0, 0, 0), Layer(3)
  69. _CLEARCOLOR _RGB32(0, 0, 0), Layer(7)
  70.  
  71. _SOURCE Layer(6)
  72.  
  73. ON TIMER(t1&, 1) FPS
  74. 'Title_Startup
  75. 'END
  76. Game_INIT
  77. DrawBoard
  78. TIMER(t1&) ON
  79.  IF _KEYDOWN(27) THEN ExitFlag%% = TRUE
  80.  IF _KEYDOWN(Key_Up) THEN
  81.   G.Start = TRUE
  82.   IF NOT Collision_Grid(Gapper, UP) THEN G.GD = UP
  83.  IF _KEYDOWN(Key_Down) THEN
  84.   G.Start = TRUE
  85.   IF NOT Collision_Grid(Gapper, DOWN) THEN G.GD = DOWN
  86.  IF _KEYDOWN(Key_Left) THEN
  87.   G.Start = TRUE
  88.   IF NOT Collision_Grid(Gapper, LEFT) THEN G.GD = LEFT
  89.  IF _KEYDOWN(Key_Right) THEN
  90.   G.Start = TRUE
  91.   IF NOT Collision_Grid(Gapper, RIGHT) THEN G.GD = RIGHT
  92.  
  93.  IF G.Start THEN Move_Seeker: Move_Gapper
  94.  
  95.  Game_Data_Update
  96.  _PUTIMAGE (0, 0)-STEP(639, 15), Layer(10), Layer(1)
  97.  _PUTIMAGE , Layer(4), Layer(1)
  98.  _PUTIMAGE , Layer(2), Layer(1)
  99.  Place_Gapper
  100.  Place_Seeker
  101.  IF G.Caught THEN
  102.   ClearLayer Layer(5)
  103.   Reset_Pos
  104.   DrawBoard
  105.   G.Lives = G.Lives - 1
  106.   IF G.Lives = -1 THEN ExitFlag%% = TRUE
  107.  _PRINTSTRING (600, 0), STR$(G.FPS), Layer(7)
  108.  _PUTIMAGE , Layer(7), Layer(1)
  109.  _PUTIMAGE , Layer(1), Layer(0)
  110.  _LIMIT 60
  111.  ClearLayer Layer(1)
  112.  Frames% = Frames% + 1
  113. LOOP UNTIL ExitFlag%%
  114.  
  115.  
  116. SUB Place_Gapper
  117.  old& = _DEST
  118.  _DEST Layer(5)
  119.  LINE (G.GX - 2, G.GY - 2)-STEP(20, 18), Black, BF
  120.  _DEST old&
  121.  'place gapper-----
  122.  _PUTIMAGE (G.GX, G.GY), Layer(3), Layer(1), (318, 218)-STEP(15, 13)
  123.  'place Gapper shadow
  124.  _PUTIMAGE (G.GX, G.GY), Layer(3), Layer(5), (318, 218)-STEP(15, 13)
  125.  
  126. SUB Place_Seeker
  127.  'place Seeker----
  128.  _PUTIMAGE (G.SX, G.SY), Layer(3), Layer(1), (341, 218)-STEP(14, 11)
  129.  '-----------------
  130.  
  131.  
  132. SUB Game_INIT
  133.  _DEST Layer(3)
  134.  'Player----
  135.  DRAW "c" + STR$(Cyan) + "drurd8l2dr3u9d2rd5ru5rd5ru5ru2d9r3ul2u8r2dlbl3c" + STR$(Magenta) + "u3ld3lu3ld3"
  136.  DRAW "bd7d3ru3rd3ru3bu3br3r4ul4bl9l4dr4"
  137.  '----------
  138.  DRAW "br20bu7"
  139.  'Seeker----
  140.  DRAW "c" + STR$(Cyan) + "r3dl3bd10r3ul3br10r3dl3bu10r3ul3c" + STR$(Magenta) + "bd2brl3dr3bd5l3dr3bl6l3ur3"
  141.  DRAW "bu5l3ur3d2lr5dl5dr5dl5"
  142.  '------------
  143.  Title_Header
  144.  _DEST Layer(5)
  145.  LINE (0, 0)-STEP(639, 439), Black, BF
  146.  _DEST Layer(0)
  147.  G.Score = 0
  148.  G.Lives = 2
  149.  G.Level = 1
  150.  
  151. SUB Title_Header
  152.  'Header
  153.  _DEST Layer(10)
  154.  LINE (84, 0)-STEP(151, 8), Magenta, BF
  155.  LINE (132, 0)-STEP(52, 8), Cyan, BF
  156.  COLOR Magenta
  157.  _PRINTSTRING (135, 1), "GAPPER"
  158.  COLOR OffWhite
  159.  
  160. SUB Game_Data_Update
  161.  'Info area updating ---------------
  162.  _PRINTSTRING (7, 192), "Score[      ]    Lives[ ]    Level[  ]", Layer(4)
  163.  _PRINTSTRING (95 - ((LEN(LTRIM$(STR$(G.Score))) - 1) * 8), 192), LTRIM$(STR$(G.Score)), Layer(4)
  164.  _PRINTSTRING (191, 192), LTRIM$(STR$(G.Lives)), Layer(4)
  165.  _PRINTSTRING (295 - ((LEN(LTRIM$(STR$(G.Level))) - 1) * 8), 192), LTRIM$(STR$(G.Level)), Layer(4)
  166.  '-----------------------------------
  167.  
  168. SUB DrawBoard
  169.  _DEST Layer(2)
  170.  SELECT CASE G.Level
  171.   CASE 1 'draw level 1 grid
  172.    Level_1_Board
  173.   CASE 2
  174.    Level_2_Board
  175.   CASE 3
  176.    Level_3_Board
  177.   CASE 4
  178.    Level_4_Board
  179.   CASE 5
  180.    Level_5_Board
  181.   CASE 6
  182.    Level_6_Board
  183.   CASE 7
  184.    Level_7_Board
  185.   CASE 8
  186.    Level_8_Board
  187.   CASE ELSE 'after level 8 board is random between 5 and 8
  188.    SELECT CASE INT(RND * 4) + 5
  189.     CASE 5
  190.      Level_5_Board
  191.     CASE 6
  192.      Level_6_Board
  193.     CASE 7
  194.      Level_7_Board
  195.     CASE 8
  196.      Level_8_Board
  197.    END SELECT
  198.  _DEST Layer(0)
  199.  ClearLayer Layer(6)
  200.  _PUTIMAGE , Layer(2), Layer(6) 'make a copy for the collision layer
  201.  
  202. SUB ClearLayer (L&)
  203.  _DEST L&
  204.  CLS
  205.  
  206. FUNCTION Collision_Seeker%%
  207.  Result%% = FALSE
  208.  _SOURCE Layer(5)
  209.  IF POINT(G.SX, G.SY) <> Black THEN Result%% = TRUE
  210.  IF POINT(G.SX + 8, G.SY) <> Black THEN Result%% = TRUE
  211.  IF POINT(G.SX, G.SY + 8) <> Black THEN Result%% = TRUE
  212.  IF POINT(G.SX + 8, G.SY + 8) <> Black THEN Result%% = TRUE
  213.  IF Result%% THEN LOCATE 1, 1: PRINT POINT(G.SX, G.SY), POINT(G.SX + 8, G.SY), POINT(G.SX, G.SY + 8), POINT(G.SX + 8, G.SY + 8)
  214.  Collision_Seeker = Result%%
  215.  _SOURCE Layer(6)
  216.  
  217. FUNCTION Collision_Grid%% (who%%, Direction%%)
  218.  Result%% = TRUE 'always assume collision
  219.  SELECT CASE who%% 'who are we checking collision for?
  220.   CASE Gapper
  221.    SELECT CASE Direction%%
  222.     CASE UP 'see if there is grid up from Gappers position
  223.      Check~& = POINT(G.GX + 8, G.GY + 5)
  224.     CASE DOWN 'see if there is Grid below Gapper
  225.      Check~& = POINT(G.GX + 8, G.GY + 9)
  226.     CASE LEFT 'check for grid to the left of Gapper
  227.      Check~& = POINT(G.GX + 6, G.GY + 7)
  228.     CASE RIGHT 'Check for grid to the Right of Gapper
  229.      Check~& = POINT(G.GX + 10, G.GY + 7)
  230.    END SELECT
  231.   CASE Seeker
  232.    SELECT CASE Direction%%
  233.     CASE UP 'see if there is grid up from Gappers position
  234.      Check~& = POINT(G.SX + 8, G.SY + 5)
  235.      _PRINTSTRING (250, 400), "UP   ", Layer(7)
  236.     CASE DOWN 'see if there is Grid below Gapper
  237.      Check~& = POINT(G.SX + 8, G.SY + 9)
  238.      _PRINTSTRING (250, 400), "DOWN ", Layer(7)
  239.     CASE LEFT 'check for grid to the left of Gapper
  240.      Check~& = POINT(G.SX + 6, G.SY + 7)
  241.      _PRINTSTRING (250, 400), "LEFT ", Layer(7)
  242.     CASE RIGHT 'Check for grid to the Right of Gapper
  243.      Check~& = POINT(G.SX + 10, G.SY + 7)
  244.      _PRINTSTRING (240, 400), "RIGHT", Layer(7)
  245.    END SELECT
  246.  Blue~%% = _BLUE32(Check~&) 'check for Blue, grid is Cyan in color
  247.  IF Blue~%% THEN Result%% = FALSE 'there is grid to move onto, no collision with edge of grid
  248.  
  249.  
  250.  Collision_Grid = Result%%
  251.  '_SOURCE Layer(0)
  252.  
  253.  
  254. SUB Move_Seeker
  255.  STATIC Mover%%
  256.  Seeker_Logic
  257.  IF Mover%% < G.SS THEN
  258.   Mover%% = Mover%% + 1 'Slow our seeker down
  259.   Mover%% = 0
  260.   SELECT CASE G.SD
  261.    CASE UP
  262.     IF NOT Collision_Grid(Seeker, UP) THEN G.SY = G.SY - 2
  263.    CASE DOWN
  264.     IF NOT Collision_Grid(Seeker, DOWN) THEN G.SY = G.SY + 2
  265.    CASE LEFT
  266.     IF NOT Collision_Grid(Seeker, LEFT) THEN G.SX = G.SX - 2
  267.    CASE RIGHT
  268.     IF NOT Collision_Grid(Seeker, RIGHT) THEN G.SX = G.SX + 2
  269.  IF Collision_Seeker THEN G.Caught = TRUE
  270.  _PRINTSTRING (10, 400), STR$(Mover%%) + STR$(G.SD) + STR$(G.SX - G.GX) + STR$(G.SY - G.GY) + " " + STR$(G.SY) + "      ", Layer(7)
  271.  
  272. SUB Move_Gapper
  273.  SELECT CASE G.GD
  274.   CASE UP
  275.    IF NOT Collision_Grid(Gapper, UP) THEN G.GY = G.GY - 2
  276.   CASE DOWN
  277.    IF NOT Collision_Grid(Gapper, DOWN) THEN G.GY = G.GY + 2
  278.   CASE LEFT
  279.    IF NOT Collision_Grid(Gapper, LEFT) THEN G.GX = G.GX - 2
  280.   CASE RIGHT
  281.    IF NOT Collision_Grid(Gapper, RIGHT) THEN G.GX = G.GX + 2
  282.  Color_Line
  283.  
  284. SUB Seeker_Logic
  285.  DistX% = (G.SX - G.GX) 'find X distance between seeker and gapper
  286.  DistY% = (G.SY - G.GY) 'find Y distance between Seeker and Gapper
  287.  IF ABS(DistX%) > ABS(DistY%) THEN 'if player is farther on the X then
  288.   IF DistX% < 0 THEN
  289.    Turn_Seeker RIGHT 'try going right to get closer
  290.   ELSE
  291.    Turn_Seeker LEFT 'try going left to get closer
  292.   END IF
  293.  ELSE 'player is farther on the Y then
  294.   IF DistY% < 0 THEN
  295.    Turn_Seeker DOWN 'try going down to get closer
  296.   ELSE
  297.    Turn_Seeker UP 'try going up to get closer
  298.   END IF
  299.  
  300. SUB Turn_Seeker (Direction%%)
  301.  SELECT CASE Direction%%
  302.   CASE UP
  303.    IF NOT Collision_Grid(Seeker, UP) THEN G.SD = UP
  304.   CASE DOWN
  305.    IF NOT Collision_Grid(Seeker, DOWN) THEN G.SD = DOWN
  306.   CASE LEFT
  307.    IF NOT Collision_Grid(Seeker, LEFT) THEN G.SD = LEFT
  308.   CASE RIGHT
  309.    IF NOT Collision_Grid(Seeker, RIGHT) THEN G.SD = RIGHT
  310.  
  311. SUB Color_Line
  312.  _DEST Layer(2)
  313.  LINE (4 + G.GX \ 2, 3 + G.GY \ 2)-STEP(0, 0), Magenta, BF
  314.  _DEST Layer(0)
  315.  
  316. SUB FPS
  317.  G.FPS = Frames%
  318.  Frames% = 0
  319.  
  320. SUB Title_Startup
  321.  _DEST Layer(8)
  322.  LINE (84, 0)-STEP(151, 8), Brown, BF
  323.  LINE (132, 0)-STEP(52, 8), Cyan, BF
  324.  COLOR Brown
  325.  _PRINTSTRING (135, 1), "GAPPER"
  326.  LINE (0, 140)-STEP(639, 8), Brown, BF
  327.  COLOR Black
  328.  _PRINTSTRING (0, 141), "Score:"
  329.  _PRINTSTRING (220, 141), "Level:"
  330.  _PRINTSTRING (100 - (LEN(LTRIM$(STR$(G.Score))) * 8), 141), STR$(G.Score) + "."
  331.  _PRINTSTRING (284 - (LEN(LTRIM$(STR$(G.Level))) * 8), 141), STR$(G.Level)
  332.  
  333.  COLOR Yellow
  334.  _PRINTSTRING (100, 160), "P: Play a game."
  335.  _PRINTSTRING (100, 170), "Q: Quit"
  336.  _PRINTSTRING (100, 180), "N: New Player"
  337.  _PRINTSTRING (100, 190), "I: Instuctions"
  338.  COLOR Magenta
  339.  _PRINTSTRING (0, 210), "Enter your initials:"
  340.  COLOR OffWhite
  341.  
  342.  Get_Player
  343.  IF Search_For_Player THEN AddPlayer 'first time this player has played!
  344.  Display_HighScores
  345.  IF NOT G.Quit THEN Get_Menu_Selection
  346.  
  347. SUB Get_Player
  348.  Blink%% = TRUE: I$ = ""
  349.  DO 'Initials input Loop
  350.   KBD& = _KEYHIT
  351.   SELECT CASE KBD&
  352.    CASE 8 'delete
  353.     IF LEN(I$) THEN I$ = LEFT$(I$, LEN(I$) - 1)
  354.     LINE (168, 210)-(248, 219), Black, BF
  355.    CASE 32 TO 122 'characters
  356.     IF LEN(I$) < 3 THEN I$ = I$ + CHR$(KBD&)
  357.     LINE (168, 210)-(248, 219), Black, BF
  358.    CASE 13 'accept input(if any)
  359.     IF RTRIM$(I$) <> "" THEN ExitFlag%% = TRUE
  360.    CASE 27
  361.     ExitFlag%% = TRUE
  362.     G.Quit = TRUE
  363.   COLOR OffWhite
  364.   _PRINTSTRING (168, 210), I$
  365.   IF Blink%% THEN COLOR Black ELSE COLOR OffWhite
  366.   _PRINTSTRING (168 + LEN(I$) * 8, 210), "_"
  367.   _PUTIMAGE , Layer(8), _DISPLAY
  368.   _LIMIT 30
  369.   t%% = t%% + 1
  370.   IF t%% = 4 THEN Blink%% = NOT Blink%%: t%% = 0
  371.  LOOP UNTIL ExitFlag%%
  372.  G.Player = I$
  373.  
  374. SUB Get_Menu_Selection
  375.  LINE (0, 210)-STEP(200, 9), Black, BF
  376.  _PRINTSTRING (0, 210), "Please enter selection:  "
  377.  DO
  378.   KBD& = _KEYHIT
  379.   SELECT CASE KBD&
  380.    CASE ASC("p") OR ASC("P")
  381.    CASE ASC("q") OR ASC("Q")
  382.     ExitFlag%% = TRUE
  383.     G.Quit = TRUE
  384.    CASE ASC("n") OR ASC("N")
  385.    CASE ASC("i") OR ASC("I")
  386.     Instructions
  387.    CASE 27
  388.     ExitFlag%% = TRUE
  389.     G.Quit = TRUE
  390.   _PUTIMAGE , Layer(8), _DISPLAY
  391.  LOOP UNTIL ExitFlag%%
  392.  
  393. SUB Display_HighScores
  394.  _DEST Layer(8)
  395.  IF LTRIM$(RTRIM$(Scores(1).Nam)) <> "" THEN
  396.   COLOR Magenta
  397.   _PRINTSTRING (0, 24), "High scores:"
  398.   FOR i%% = 1 TO 10
  399.    IF G.Player = Scores(i%%).Nam THEN
  400.     COLOR Yellow 'player shows up Yellow
  401.    ELSEIF i%% = 1 THEN
  402.     COLOR Magenta 'High score if not player shows magenta
  403.    ELSE
  404.     COLOR OffWhite 'everybody else is off white
  405.    END IF
  406.   NEXT i%%
  407.  
  408. SUB Instructions
  409.  ClearLayer Layer(0)
  410.  _DEST Layer(9)
  411.  LINE (84, 0)-STEP(151, 8), Brown, BF
  412.  LINE (132, 0)-STEP(52, 8), Cyan, BF
  413.  COLOR Brown
  414.  _PRINTSTRING (135, 1), "GAPPER"
  415.  COLOR OffWhite
  416.  _PRINTSTRING (84, 10), "OBJECT"
  417.  _PRINTSTRING (1, 20), "To color all the blue lines"
  418.  _PRINTSTRING (1, 30), "res by moving your man GAPPER"
  419.  _PRINTSTRING (1, 40), "with the arrow keys.  At the"
  420.  _PRINTSTRING (1, 50), "same time, you myst avoid"
  421.  _PRINTSTRING (1, 60), "the SEEKER."
  422.  
  423.  _PRINTSTRING (84, 80), "POINTS"
  424.  _PRINTSTRING (1, 90), "Fifty points are awarded for"
  425.  _PRINTSTRING (1, 100), "each square that you surround"
  426.  _PRINTSTRING (1, 110), "in red.  100 points are given"
  427.  _PRINTSTRING (1, 120), "when you encircle the extra"
  428.  _PRINTSTRING (1, 130), "point box while it is on. Each"
  429.  _PRINTSTRING (1, 140), "gap costs 5 points."
  430.  
  431.  _PRINTSTRING (84, 150), "GAPPING"
  432.  _PRINTSTRING (1, 160), "You can create a temporary"
  433.  _PRINTSTRING (1, 170), "gap in the lines by pressing"
  434.  _PRINTSTRING (1, 180), "the SPACE bar or RETURN.  The"
  435.  _PRINTSTRING (1, 190), "word ON appears as long as the"
  436.  _PRINTSTRING (1, 200), "gap is active.  Neither you nor"
  437.  _PRINTSTRING (1, 210), "the seeker can cross the gap."
  438.  
  439.  _PRINTSTRING (0, 230), "Press a key..."
  440.  _PUTIMAGE , Layer(9), _DISPLAY
  441.  _DELAY .5
  442.  'end of page 1
  443.  DO: _LIMIT 24: LOOP WHILE INKEY$ = ""
  444.  CLS
  445.  LINE (84, 0)-STEP(151, 8), Brown, BF
  446.  LINE (132, 0)-STEP(52, 8), Cyan, BF
  447.  COLOR Brown
  448.  _PRINTSTRING (135, 1), "GAPPER"
  449.  COLOR OffWhite
  450.  _PRINTSTRING (84, 10), "LEVELS"
  451.  _PRINTSTRING (1, 20), "The level increases each time"
  452.  _PRINTSTRING (1, 30), "you complete a screen.  The"
  453.  _PRINTSTRING (1, 40), "seeker gets faster, the gap"
  454.  _PRINTSTRING (1, 50), "time shorter and there will"
  455.  _PRINTSTRING (1, 60), "be more boxes."
  456.  
  457.  _PRINTSTRING (84, 80), "LIVES"
  458.  _PRINTSTRING (1, 90), "You start out with 2 extra"
  459.  _PRINTSTRING (1, 100), "men.  One more will be given"
  460.  _PRINTSTRING (1, 110), "for each 5000 points."
  461.  
  462.  _PRINTSTRING (84, 130), "START"
  463.  _PRINTSTRING (1, 140), "When the grid appears, you"
  464.  _PRINTSTRING (1, 150), "Will be in the top left and"
  465.  _PRINTSTRING (1, 160), "the seeker at the low right."
  466.  _PRINTSTRING (1, 170), "Press a key to begin each new"
  467.  _PRINTSTRING (1, 180), "screen."
  468.  
  469.  _PRINTSTRING (0, 230), "Press a key..."
  470.  'end of page 2
  471.  _PUTIMAGE , Layer(9), _DISPLAY
  472.  _DELAY .5
  473.  DO: _LIMIT 24: LOOP WHILE INKEY$ = ""
  474.  ClearLayer Layer(0)
  475.  
  476. SUB Load_Scores
  477.  OPEN "Gapper.HSL" FOR BINARY AS #1
  478.  GET #1, , Count%%
  479.  FOR i%% = 1 TO Count%%
  480.   GET #1, , Scores(i%%).Nam
  481.   GET #1, , Scores(i%%)
  482.   GET #1, , Scores(i%%)
  483.  NEXT i%%
  484.  CLOSE #1
  485.  
  486. FUNCTION Search_For_Player
  487.  FOR i%% = 1 TO 10
  488.   IF G.Player = Scores(i%%).Nam THEN Result%% = TRUE: i%% = 11 ELSE Result%% = FALSE
  489.  NEXT i%%
  490.  Search_For_Player = Result%%
  491.  
  492. SUB AddPlayer
  493.  OPEN "Gapper.HSL" FOR BINARY AS #1
  494.  GET #1, , Count%%
  495.  IF Count%% < 10 THEN Count%% = Count%% + 1
  496.  PUT #1, 1, Count%%
  497.  PUT #1, 1 + 10 * Count%%, G.Player
  498.  PUT #1, , NULL& 'filler for score
  499.  PUT #1, , NULL% 'filler for level
  500.  CLOSE #1
  501.  Load_Scores 'reload score list
  502.  
  503. SUB Reset_Pos
  504.  PLAY "o1l32aacceegg"
  505.  _DELAY .25
  506.  G.Start = FALSE
  507.  G.Caught = FALSE
  508.  SELECT CASE G.Level
  509.   CASE 1
  510.    'Level 1 Start Pos.------
  511.    G.GX = 0: G.GY = 14 'Gapper
  512.    G.SX = 626: G.SY = 374 'Seeker
  513.    '------------------------
  514.  
  515. SUB check_box
  516.  DIM Grid_Check(7, 6) AS _BYTE 'largest board(8) is 8x7(0-7,0-6)
  517.  IF G.Start THEN
  518.   SELECT CASE G.Level
  519.    CASE 1 '3x3(0-2,0-2)
  520.     'check horizontals
  521.     FOR Iy%% = 0 TO 2
  522.      FOR Ix%% = 0 TO 2 'check top line
  523.       FOR x%% = 0 TO 60 STEP 6 'check 10 points
  524.        IF POINT(4 + x%% + 60 * Ix%%, 10) = Magenta THEN full%% = TRUE ELSE full%% = FALSE: x%% = 66
  525.       NEXT x%%
  526.       IF full%% THEN Grid_Check(Ix%%, Iy%%) = TRUE
  527.      NEXT Ix%%
  528.     NEXT Iy%%
  529.     'check verticals
  530.     FOR Ix%% = 0 TO 2
  531.      FOR Iy%% = 0 TO 2
  532.       FOR x%% = 0 TO 60 STEP 6 'check 10 points
  533.        IF POINT(4 + x%% + 60 * Ix%%, 10) = Magenta THEN full%% = TRUE ELSE full%% = FALSE: x%% = 66
  534.       NEXT x%%
  535.       IF full%% THEN Grid_Check(Ix%%, Iy%%) = TRUE
  536.  
  537.      NEXT Iy%%
  538.     NEXT Ix%%
  539.   FOR Ix%% = 0 TO 7
  540.    FOR Iy%% = 0 TO 6
  541.     Grid_Check(Ix%%, Iy%%) = FALSE
  542.   NEXT Iy%%, Ix%%
  543.  
  544. SUB Level_1_Board
  545.  FOR I%% = 0 TO 3
  546.   LINE (4, 10 + I%% * 60)-STEP(311, 0), Cyan 'horizontal lines
  547.   LINE (4 + I%% * 104, 10)-STEP(0, 180), Cyan 'vertical lines
  548.  NEXT I%%
  549.  G.SS = 2 'set seeker speed 1\4th player speed
  550.  G.Start = FALSE
  551.  'Level 1 Start Pos.------
  552.  G.GX = 0: G.GY = 14 'Gapper
  553.  G.SX = 626: G.SY = 374 'Seeker
  554.  '------------------------
  555.  
  556. SUB Level_2_Board
  557.  FOR I%% = 0 TO 4
  558.   LINE (8, 12 + I%% * 44)-STEP(304, 0), Cyan 'horizontal lines
  559.   LINE (8 + I%% * 76, 12)-STEP(0, 176), Cyan 'vertical lines
  560.  NEXT I%%
  561.  G.SS = 1 'set seeker speed 1\4th player speed
  562.  G.Start = FALSE
  563.  'Level 2 Start Pos.------
  564.  G.GX = 8: G.GY = 18 'Gapper
  565.  G.SX = 616: G.SY = 370 'Seeker
  566.  '------------------------
  567.  
  568. SUB Level_3_Board
  569.  FOR I%% = 0 TO 5
  570.   LINE (10, 10 + I%% * 36)-STEP(300, 0), Cyan 'horizontal lines
  571.   LINE (10 + I%% * 60, 10)-STEP(0, 180), Cyan 'vertical lines
  572.  NEXT I%%
  573.  G.SS = 1 'set seeker speed 1\4th player speed
  574.  G.Start = FALSE
  575.  'Level 5 Start Pos.------
  576.  G.GX = 12: G.GY = 14 'Gapper
  577.  G.SX = 612: G.SY = 374 'Seeker
  578.  '------------------------
  579.  
  580. SUB Level_4_Board
  581.  FOR I%% = 0 TO 6
  582.   LINE (16, 16 + I%% * 24)-STEP(288, 0), Cyan 'horizontal lines
  583.   LINE (16 + I%% * 48, 16)-STEP(0, 168), Cyan 'vertical lines
  584.  NEXT I%%
  585.  LINE (16, 16 + I%% * 24)-STEP(288, 0), Cyan 'horizontal lines
  586.  G.SS = 1 'set seeker speed 1\4th player speed
  587.  G.Start = FALSE
  588.  'Level 4 Start Pos.------
  589.  G.GX = 24: G.GY = 26 'Gapper
  590.  G.SX = 600: G.SY = 362 'Seeker
  591.  '------------------------
  592.  
  593. SUB Level_5_Board
  594.  FOR I%% = 0 TO 4
  595.   LINE (16, 16 + I%% * 24)-STEP(288, 0), Cyan 'horizontal lines
  596.   LINE (88 + I%% * 36, 16)-STEP(0, 168), Cyan 'vertical lines
  597.  NEXT I%%
  598.  FOR I%% = 0 TO 1
  599.   LINE (88, 136 + I%% * 24)-STEP(144, 0), Cyan 'horizontal lines
  600.   LINE (16 + I%% * 36, 16)-STEP(0, 96), Cyan 'vertical lines
  601.   LINE (268 + I%% * 36, 16)-STEP(0, 96), Cyan 'vertical lines
  602.  NEXT I%%
  603.  
  604.  G.SS = 1 'set seeker speed = player speed
  605.  G.Start = FALSE
  606.  'Level 5 Start Pos.------
  607.  G.GX = 24: G.GY = 26 'Gapper
  608.  G.SX = 458: G.SY = 362 'Seeker
  609.  '------------------------
  610.  
  611. SUB Level_6_Board
  612.  FOR I%% = 0 TO 3
  613.   LINE (16, 64 + I%% * 24)-STEP(288, 0), Cyan 'horizontal lines
  614.   LINE (88 + I%% * 36, 16)-STEP(0, 168), Cyan 'vertical lines
  615.  NEXT I%%
  616.  LINE (88 + I%% * 36, 16)-STEP(0, 168), Cyan 'vertical lines
  617.  
  618.  FOR I%% = 0 TO 1
  619.   LINE (88, 16 + I%% * 24)-STEP(144, 0), Cyan 'horizontal lines
  620.   LINE (88, 160 + I%% * 24)-STEP(144, 0), Cyan 'horizontal lines
  621.   LINE (16 + I%% * 36, 64)-STEP(0, 72), Cyan 'vertical lines
  622.   LINE (268 + I%% * 36, 64)-STEP(0, 72), Cyan 'vertical lines
  623.  NEXT I%%
  624.  
  625.  G.SS = 1 'set seeker speed = player speed
  626.  G.Start = FALSE
  627.  'Level 6 Start Pos.------
  628.  G.GX = 168: G.GY = 26 'Gapper
  629.  G.SX = 458: G.SY = 362 'Seeker
  630.  '------------------------
  631.  
  632. SUB Level_7_Board
  633.  FOR I%% = 0 TO 4
  634.   LINE (16, 88 + I%% * 24)-STEP(288, 0), Cyan 'horizontal lines
  635.   LINE (88 + I%% * 36, 16)-STEP(0, 168), Cyan 'vertical lines
  636.  NEXT I%%
  637.  FOR I%% = 0 TO 1
  638.   LINE (88, 16 + I%% * 24)-STEP(144, 0), Cyan 'horizontal lines
  639.   LINE (16 + I%% * 36, 88)-STEP(0, 96), Cyan 'vertical lines
  640.   LINE (268 + I%% * 36, 88)-STEP(0, 96), Cyan 'vertical lines
  641.  NEXT I%%
  642.  LINE (88, 16 + I%% * 24)-STEP(144, 0), Cyan 'horizontal lines
  643.  
  644.  G.SS = 1 'set seeker speed = player speed
  645.  G.Start = FALSE
  646.  'Level 5 Start Pos.------
  647.  G.GX = 168: G.GY = 26 'Gapper
  648.  G.SX = 600: G.SY = 362 'Seeker
  649.  '------------------------
  650.  
  651. SUB Level_8_Board
  652.  FOR I%% = 0 TO 7
  653.   LINE (16, 16 + I%% * 24)-STEP(288, 0), Cyan 'horizontal lines
  654.   LINE (16 + I%% * 36, 16)-STEP(0, 168), Cyan 'vertical lines
  655.  NEXT I%%
  656.  LINE (16 + I%% * 36, 16)-STEP(0, 168), Cyan 'vertical lines
  657.  G.SS = 1 'set seeker speed 1\4th player speed
  658.  G.Start = FALSE
  659.  'Level 8 Start Pos.------
  660.  G.GX = 24: G.GY = 26 'Gapper
  661.  G.SX = 600: G.SY = 362 'Seeker
  662.  '------------------------
Level1.jpg
* Level1.jpg (Filesize: 19.3 KB, Dimensions: 640x400, Views: 565)
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to check if a box has been completed?
« Reply #1 on: November 12, 2021, 04:17:55 pm »
Follow the directions in the Title bar to test the demo I have for finding surrounded boxes. This requires not just the function BoxSurrounded%(boxNumber) which returns 0 or -1, but an integrated environment to make the function work easily.

Once a box is determined to be surrounded, it never needs to be checked again by checking all Points around it.

Code: QB64: [Select]
  1. _Title "Box Surrounded Test" ' b+ 2021-11-12
  2.  
  3. Screen _NewImage(800, 600, 32) '
  4. _Delay .25
  5. Dim Shared cellsDown, cellsAcross, nCells, cellWidth, cellHeight, surroundColor As _Unsigned Long
  6. cellsAcross = 5
  7. cellsDown = 3
  8. nCells = cellsAcross * cellsDown
  9. cellWidth = Int(_Width / cellsAcross - 1) 'subtract 1 to get borders around all cells on screen
  10. cellHeight = Int(_Height / cellsDown - 1)
  11. surroundColor = _RGB32(255, 0, 255) ' purple as I recall
  12. Dim Shared BoxesSurrounded(1 To nCells) As Integer
  13. _Title "Box Surrounded Test: To demo, we need to start with a grid... zzz = Sleep, press any to continue."
  14. 'drawGrid
  15. For i = 0 To _Width Step cellWidth
  16.     Line (i, 0)-(i, cellsDown * cellHeight)
  17. For i = 0 To _Height Step cellHeight
  18.     Line (0, i)-(cellsAcross * cellWidth, i)
  19. 'label cells (boxes) with id number
  20. i = 1
  21. For r = 1 To cellsDown
  22.     For c = 1 To cellsAcross
  23.         _PrintString ((c - 1) * cellWidth + 2, (r - 1) * cellHeight + 2), _Trim$(Str$(i))
  24.         i = i + 1
  25.     Next
  26. _Title "Demo BoxSurrounded, next we need to randomly surround 3 boxes... zzz"
  27. For i = 1 To 3
  28.     tryAgain:
  29.     B = Int(Rnd * nCells) + 1
  30.     If BoxesSurrounded(B) = 0 Then surroundBox B: BoxesSurrounded(B) = -1 Else GoTo tryAgain
  31. _Title "OK now find the boxes surrounded with the BoxSurrounded%() Function and light them up!... zzz"
  32.  
  33. 'OK now if a box is surrounded then fill it in
  34. For i = 1 To nCells
  35.     If BoxSurrounded%(i) Then fillBox i
  36. _Title "End of Demo, thanks for playing... zzz"
  37.  
  38. Sub fillBox (boxNumber)
  39.     row = Int((boxNumber - 1) / cellsAcross)
  40.     col = (boxNumber - 1) Mod cellsAcross + 1
  41.     Line ((col - 1) * cellWidth + 3, row * cellHeight + 3)-Step(cellWidth - 6, cellHeight - 6), &HFFFFFF00, BF
  42.  
  43. Sub surroundBox (boxNumber)
  44.     row = Int((boxNumber - 1) / cellsAcross)
  45.     col = (boxNumber - 1) Mod cellsAcross + 1
  46.     Line ((col - 1) * cellWidth, row * cellHeight)-Step(cellWidth, cellHeight), surroundColor, B
  47.  
  48. Function BoxSurrounded% (boxNumber As Integer) 'save already tested boxes in shared array BoxesSurrounded()
  49.     If BoxesSurrounded(boxNumber) = 0 Then
  50.         row = Int((boxNumber - 1) / cellsPerCpl)
  51.         col = (boxNumber - 1) Mod cellsAcross + 1
  52.         For i = (row - 1) * cellHeight To row * cellHeight
  53.             If Point((col - 1) * cellWidth, i) <> surroundColor Then Exit Function ' False Baox not surrounded
  54.             If Point(col * cellWidth, i) <> surroundColor Then Exit Function
  55.         Next
  56.         For i = (col - 1) * cellWidth To col * cellWidth
  57.             If Point(i, (row - 1) * cellHeight) <> surroundColor Then Exit Function ' False Baox not surrounded
  58.             If Point(i, row * cellHeight) <> surroundColor Then Exit Function
  59.         Next
  60.         BoxSurrounded% = -1 'all OK
  61.     Else
  62.         BoxSurrounded = -1
  63.     End If
  64.  
  65.  
  66.  

 
BoxSurrounded Test.PNG


EDIT: Sorry, as soon as I posted this I realized a couple of variable names might be confusing and so I substituted better choices, I hope.
« Last Edit: November 12, 2021, 04:26:33 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to check if a box has been completed?
« Reply #2 on: November 12, 2021, 04:37:53 pm »
Easy to change levels on this code:
Code: QB64: [Select]
  1. _Title "Box Surrounded Test" ' b+ 2021-11-12
  2.  
  3. Screen _NewImage(800, 600, 32) '
  4. _Delay .25
  5. Dim Shared cellsDown, cellsAcross, nCells, cellWidth, cellHeight, surroundColor As _Unsigned Long
  6.  
  7.  
  8. ' piece of cake to modify this code for different levels just change these 2 lines!!!
  9. cellsAcross = 10
  10. cellsDown = 8
  11. ' ====================================================================================
  12.  
  13. ' ...
  14. ' ...
  15.  

Changing levels is a snap!.PNG
* Changing levels is a snap!.PNG (Filesize: 23.06 KB, Dimensions: 804x633, Views: 182)

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: How to check if a box has been completed?
« Reply #3 on: November 13, 2021, 02:33:44 pm »
in this line

  row = INT((boxNumber - 1) / cellsPerCpl)

what is cellsPerCpl? its not used anywhere else and has a value of 0 as far as I can tell.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to check if a box has been completed?
« Reply #4 on: November 13, 2021, 05:47:10 pm »
in this line

  row = INT((boxNumber - 1) / cellsPerCpl)

what is cellsPerCpl? its not used anywhere else and has a value of 0 as far as I can tell.

Wow! I missed that typo. It's supposed to be cellsAcross. Like it is in the fillBox routine and the SurroundBox% routine. I wonder why it's not causing problems in my runs of the program, everything works with that crazy typo!?

Originally I had variable cellsPerRow and cellsPerCol which I changed to cellsAcross and cellsDown because I thought that was better wording. cellsPerCpl is a typo p instead of o, so it didn't get changed when I changed the other variable names.

Well now I've a mystery, that typo should cause problems!


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to check if a box has been completed?
« Reply #5 on: November 13, 2021, 08:12:48 pm »
Yes, I fixed the typo and a host of errors popped up. I should have them all fixed now here:
Code: QB64: [Select]
  1. _Title "Box Surrounded Test" ' b+ 2021-11-12
  2. Screen _NewImage(800, 600, 32) '
  3. _Delay .25
  4. Dim Shared cellsDown, cellsAcross, nCells, cellWidth, cellHeight, surroundColor As _Unsigned Long
  5.  
  6.     ' piece of cake to modify this code for different levels just change these 2 lines!!!
  7.     'cellsAcross = Int(Rnd * 13) + 3 ' 3 min, 15 max
  8.     'cellsDown = Int(Rnd * 8) + 3 ' 3 min, 10 max
  9.     ' ====================================================================================
  10.  
  11.     cellsAcross = 3 ' debug    maybe better for building confidence in code
  12.     cellsDown = 3 ' debug
  13.  
  14.  
  15.     nCells = cellsAcross * cellsDown
  16.     cellWidth = Int(_Width / cellsAcross - 1) 'subtract 1 to get borders around all cells on screen
  17.     cellHeight = Int(_Height / cellsDown - 1)
  18.     surroundColor = _RGB32(255, 0, 255) ' purple as I recall
  19.     ReDim Shared BoxesSurrounded(1 To nCells) As Integer
  20.     _Title "Box Surrounded Test: To demo, we need to start with a grid... zzz = Sleep, press any to continue."
  21.     Sleep
  22.     'drawGrid
  23.     For I = 0 To _Width Step cellWidth
  24.         Line (I, 0)-(I, cellsDown * cellHeight)
  25.     Next
  26.     For I = 0 To _Height Step cellHeight
  27.         Line (0, I)-(cellsAcross * cellWidth, I)
  28.     Next
  29.     'label cells (boxes) with id number
  30.     I = 1
  31.     For r = 1 To cellsDown
  32.         For c = 1 To cellsAcross
  33.             _PrintString ((c - 1) * cellWidth + 2, (r - 1) * cellHeight + 2), _Trim$(Str$(I))
  34.             I = I + 1
  35.         Next
  36.     Next
  37.     _Title "Demo BoxSurrounded, next we need to randomly surround 3 boxes... zzz"
  38.     Sleep
  39.     boxes$ = ""
  40.     For I = 1 To 3
  41.         tryAgain:
  42.         B = Int(Rnd * nCells) + 1
  43.         If BoxesSurrounded(B) = 0 Then surroundBox B: BoxesSurrounded(B) = -1: boxes$ = boxes$ + Str$(B) Else GoTo tryAgain
  44.     Next
  45.     _Title "OK now find the boxes (" + boxes$ + ") surrounded with the BoxSurrounded%() Function and light them up!... zzz"
  46.     Sleep
  47.  
  48.     'OK now if a box is surrounded then fill it in
  49.     For I = 1 To nCells
  50.         If BoxSurrounded%(I) Then fillBox I
  51.     Next
  52.     _Title "Escape to quit, any other for another round... zzz"
  53.     Sleep
  54.     Cls
  55.  
  56. Sub fillBox (boxNumber)
  57.     row = Int((boxNumber - 1) / cellsAcross)
  58.     col = (boxNumber - 1) Mod cellsAcross + 1
  59.     Line ((col - 1) * cellWidth + 3, row * cellHeight + 3)-Step(cellWidth - 6, cellHeight - 6), &HFFFFFF00, BF
  60.  
  61. Sub surroundBox (boxNumber)
  62.     row = Int((boxNumber - 1) / cellsAcross)
  63.     col = (boxNumber - 1) Mod cellsAcross + 1
  64.     Line ((col - 1) * cellWidth, row * cellHeight)-Step(cellWidth, cellHeight), surroundColor, B
  65.  
  66. Function BoxSurrounded% (boxNumber As Integer) 'save already tested boxes in shared array BoxesSurrounded()
  67.     If BoxesSurrounded(boxNumber) = 0 Then
  68.         row = Int((boxNumber - 1) / cellsAcross)
  69.         col = (boxNumber - 1) Mod cellsAcross + 1
  70.         For i = row * cellHeight To row * cellHeight + row
  71.             If Point((col - 1) * cellWidth, i) <> surroundColor Then Exit Function ' False Box not surrounded
  72.             If Point(col * cellWidth, i) <> surroundColor Then Exit Function
  73.         Next
  74.         For i = (col - 1) * cellWidth To col * cellWidth
  75.             If Point(i, row * cellHeight) <> surroundColor Then Exit Function ' False Box not surrounded
  76.             If Point(i, row * cellHeight + row) <> surroundColor Then Exit Function
  77.         Next
  78.         BoxSurrounded% = -1 'all OK
  79.     Else
  80.         BoxSurrounded = -1
  81.     End If
  82.  
  83.  
« Last Edit: November 13, 2021, 08:18:28 pm by bplus »