Author Topic: Simple marble game  (Read 4187 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Simple marble game
« on: March 01, 2020, 12:27:05 pm »
Enter on marble. arrow for direction.  Objective: only 1 remaining marble.

Code: QB64: [Select]
  1. DEFINT A-Z '                                                            Mar94, with minor updates for QB64
  2. DIM board(7, 7)
  3. xc = 320: yc = 240 '                                                    center
  4. xs = 56: ys = 56 '                                                      marble placement
  5. SCREEN 12 '                                                             640*480*16
  6. b = 63: g = 63: r = 63
  7. p& = CDBL(b) * 65536 + CDBL(g) * 256 + r: PALETTE 1, p& '               cursor
  8. b = 10: g = 63: r = 10
  9. p& = CDBL(b) * 65536 + CDBL(g) * 256 + r: PALETTE 2, p& '               highlight
  10. b = 23: g = 0: r = 43
  11. FOR i = 3 TO 14
  12.     p& = CDBL(b) * 65536 + CDBL(g) * 256 + r
  13.     PALETTE i, p&
  14.     r = r - 4: IF r < 0 THEN r = 0
  15.     b = b + 4: IF b > 63 THEN b = 63
  16. x1 = xc - 4 * xs: x2 = xc + 4 * xs: y1 = yc - 4 * ys: y2 = yc + 4 * ys
  17. FOR q = -5 TO 5
  18.     LINE (x1 - q, y1 + q)-(x2 + q, y2 - q), 8 + q, B
  19. m$ = "0011100001110011111111112111111111100111000011100"
  20. begin:
  21. FOR y = 1 TO 7
  22.     FOR x = 1 TO 7
  23.         board(x, y) = VAL(MID$(m$, (y - 1) * 7 + x, 1))
  24.         IF board(x, y) = 2 THEN board(x, y) = 0
  25.         GOSUB plot
  26.     NEXT x
  27. x = 4: y = 2 '                                                          initial cursor
  28. Moving = 1 '                                                            1 cursor, 2 piece
  29. gone = 0
  30.     cc = Moving: GOSUB plot '                                           cc = cursor color
  31.     DO: _LIMIT 10 '                                                     wait for a key
  32.         i$ = INKEY$
  33.     LOOP UNTIL i$ > ""
  34.     cc = 10: GOSUB plot: cc = 0 '                                       erase cursor
  35.     xd = 0: yd = 0 '                                                    direction
  36.     IF LEN(i$) = 1 THEN
  37.         IF i$ = CHR$(27) THEN END '                                     Esc
  38.         IF i$ = CHR$(32) THEN GOTO begin '                              spacebar
  39.     ELSEIF LEN(i$) = 2 THEN '                                           arrow
  40.         a$ = RIGHT$(i$, 1)
  41.         xd = ((a$ = "K") - (a$ = "M")) * Moving '                       left / right
  42.         yd = ((a$ = "H") - (a$ = "P")) * Moving '                       up / down
  43.     END IF
  44.     sx = x: x = x + xd
  45.     sy = y: y = y + yd
  46.     IF (x < 1) OR (y < 1) OR (x > 7) OR (y > 7) THEN x = sx: y = sy
  47.     IF MID$(m$, (y - 1) * 7 + x, 1) = "0" THEN x = sx: y = sy
  48.     IF (Moving = 1) AND (i$ = CHR$(13)) THEN '                          Enter
  49.         IF board(x, y) = 1 THEN Moving = 2 '                            toggle to moving piece
  50.     ELSEIF Moving = 2 THEN
  51.         Moving = 1 '                                                    toggle back to cursor
  52.         MoveError = 0 '                                                 assume no error
  53.         IF board(sx, sy) = 0 THEN MoveError = 1 '                       no ball to move
  54.         IF board(x, y) = 1 THEN MoveError = 2 '                         target space not blank
  55.         dx = SGN(x - sx) '                                              direction x
  56.         dy = SGN(y - sy)
  57.         IF board(sx + dx, sy + dy) = 0 THEN MoveError = 3 '             no capture
  58.         IF MoveError THEN '                                             got an error
  59.             SOUND 200, 3 '                                              error beep
  60.             x = sx: y = sy '                                            restore xy pos
  61.         ELSE
  62.             gone = gone + 1
  63.             board(x, y) = 1: GOSUB plot '                               to
  64.             x = sx: y = sy: board(x, y) = 0: GOSUB plot '               from
  65.             x = sx + dx: y = sy + dy: board(x, y) = 0: GOSUB plot '     jumped
  66.             x = x + dx: y = y + dy '                                    cursor to "to"
  67.         END IF
  68.     END IF
  69.  
  70. plot:
  71. IF MID$(m$, (y - 1) * 7 + x, 1) = "0" THEN RETURN
  72. px = xc + (x - 4) * xs
  73. py = yc + (y - 4) * ys
  74. CIRCLE (px, py), 26, cc
  75. IF cc THEN RETURN '                                                     just cursor
  76. CIRCLE (px, py), 26, 10
  77. PAINT (px, py), 0 '                                                     erase this space
  78. IF board(x, y) THEN '                                                   is a marble
  79.     zx = INT(RND * 99) MOD 3 - 1 '                                      -1, 0, 1
  80.     zy = INT(RND * 99) MOD 3 - 1
  81.     FOR c = 3 TO 15 '                                                   color
  82.         s = 16 - c '                                                    size
  83.         CIRCLE (px, py), s, c
  84.         CIRCLE (px + 1, py), s, c
  85.         CIRCLE (px, py), s + 1, c
  86.         CIRCLE (px + 1, py), s + 1, c
  87.         px = px + (c MOD 2) * zx '                                      move center
  88.         py = py + (c MOD 2) * zy
  89.     NEXT c
  90.  
  91.  
It works better if you plug it in.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Simple marble game
« Reply #1 on: March 01, 2020, 12:31:53 pm »
Dang I got it down to 2. Next time, next time...

ss.png
* ss.png (Filesize: 8.29 KB, Dimensions: 474x472, Views: 185)
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simple marble game
« Reply #2 on: March 01, 2020, 02:51:10 pm »
Ha! this reminds me of Dav's Peg Solitaire:  https://www.qb64.org/forum/index.php?topic=602.0

except Richard Frost gets the job done < 100 lines with "hand" drawn version

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Simple marble game
« Reply #3 on: March 01, 2020, 03:08:46 pm »
279 lines in that other one (non- blank, not comment).  Looks nicer than mine.

Which causes me to ask - is there a way to get a listing of all programs on this site?
Say title, author, and size?  Or must one click and follow every thread?

It works better if you plug it in.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simple marble game
« Reply #4 on: March 01, 2020, 03:38:58 pm »
279 lines in that other one (non- blank, not comment).  Looks nicer than mine.

Which causes me to ask - is there a way to get a listing of all programs on this site?
Say title, author, and size?  Or must one click and follow every thread?

We might have same taste in games, I save all Dav's so I happened to be able to find it when I saw yours.

Thing was when I searched for it his title here was Peg Solitaire but it was pegsoltaire in code so Search Engine couldn't find it. I had to go by date of post and we are up to 38 pages of Programs since 2017.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Simple marble game
« Reply #5 on: March 02, 2020, 05:58:12 am »
IDK how to play this...Please give the instruction.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Simple marble game
« Reply #6 on: March 02, 2020, 07:41:15 am »
Using the arrow keys, move the cursor (barely visible, I know) over a piece and press Enter.
Circumnavigate the globe, and on the return select the direction you traveled and press Enter.
If the the start marble and end direction results in one other marble intervening between the
start marble and a blank space, the selected marble will hop to the blank space, annhilitating
the marble hopped over. 

The objective of the game is to promote air travel.  Ideally, only one marble will remain.  If
more than one marble remains and there are no legal moves, luggage in the quantity of remaining
marbles will be seized and auctioned off to homeless people in Walla Walla, Washington.
It works better if you plug it in.