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

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 »