Author Topic: Unscramble the picture puzzle  (Read 6159 times)

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Unscramble the picture puzzle
« on: January 24, 2021, 11:18:16 pm »
Had some extra time today to play around with this.  It's a simple picture unscramble puzzle.  It's coded so that you can use your own image instead easily.  Also, you can easily change the number of puzzle pieces to make it harder, just change row & col values.

Code uses the image attached.

- Dav

EDIT: New picture.  Added press SPACE key for a quick cheat look...

Code: QB64: [Select]
  1. '=================
  2. 'UNSCRAMBLE.BAS
  3. '=================
  4. 'Unscramble the picture puzzle
  5. 'Coded by Dav, JAN/2021
  6.  
  7. 'Use mouse, click on squares to swap them.
  8. 'SPACE key gives a little half second cheat look.
  9.  
  10. 'korea.jpg picture was taken by Dav at an outdoor
  11. 'market located in Nonsan, South Korea.
  12.  
  13.  
  14. SCREEN _LOADIMAGE("korea.jpg", 32) 'You can use your own picture
  15.  
  16. SLEEP 2 'show picture couple of secs
  17.  
  18. row = 6: col = 6 '6x6 grid of pieces - increase for more pieces
  19. xsize = _WIDTH / row: ysize = _HEIGHT / col
  20. DIM tile&(row * col), tile.x(row * col), tile.y(row * col)
  21. DIM tilepuz(row * col)
  22.  
  23. '=== split picture up into tile pieces
  24. bc = 1
  25. FOR c = 1 TO col
  26.     FOR r = 1 TO row
  27.         x1 = (r * xsize) - xsize: x2 = x1 + xsize
  28.         y1 = (c * ysize) - ysize: y2 = y1 + ysize
  29.         tile.x(bc) = x1: tile.y(bc) = y1
  30.         'make pieces images
  31.         tile&(bc) = _NEWIMAGE(ABS(x2 - x1) + 1, ABS(y2 - y1) + 1, 32)
  32.         _PUTIMAGE (0, 0), _DEST, tile&(bc), (x1, y1)-(x2, y2)
  33.  
  34.         tilepuz(bc) = bc
  35.         bc = bc + 1
  36.     NEXT
  37.  
  38. '=== swap the tilepuz array (shuffle)
  39. FOR p = 1 TO UBOUND(tilepuz)
  40.     new = INT(RND * p) + 1
  41.     IF new <> p THEN SWAP tilepuz(new), tilepuz(p)
  42.  
  43.  
  44. '=====
  45. main:
  46. '=====
  47.  
  48. '=== show current puzzle
  49. FOR t = 1 TO row * col
  50.     _PUTIMAGE (tile.x(t), tile.y(t)), tile&(tilepuz(t))
  51.     'LINE (tile.x(t), tile.y(t))-(tile.x(t) + xsize, tile.y(t) + ysize), _RGB(100, 100, 100), B
  52.  
  53. '=== check if puzzle done
  54. done = 1
  55. FOR t = 1 TO row * col
  56.     IF tilepuz(t) <> t THEN done = 0
  57. IF done = 1 THEN
  58.     PLAY "mbl16cdefgfedc": _DELAY 4
  59.     END 'Solved!
  60.  
  61.  
  62. selected = 0
  63.  
  64. second:
  65.  
  66.     'wait until mouse button up to continue
  67.     WHILE _MOUSEBUTTON(1) <> 0: n = _MOUSEINPUT: WEND
  68.  
  69.     trap = _MOUSEINPUT 'Poll mouse data
  70.  
  71.     'If user clicked mouse
  72.         'see where they clicked
  73.         mx = _MOUSEX: my = _MOUSEY
  74.  
  75.         FOR t = 1 TO row * col
  76.             tx = tile.x(t): tx2 = tile.x(t) + xsize
  77.             ty = tile.y(t): ty2 = tile.y(t) + ysize
  78.  
  79.             IF mx >= tx AND mx <= tx2 THEN
  80.                 IF my >= ty AND my <= ty2 THEN
  81.  
  82.                     IF selected = 0 THEN
  83.                         selected = 1
  84.                         'highlight piece
  85.                         LINE (tile.x(t), tile.y(t))-(tile.x(t) + xsize, tile.y(t) + ysize), _RGB(255, 255, 255), B
  86.                         SOUND 5000, .1
  87.                         sel = t 'save picked piece
  88.                         GOTO second 'now get second choice
  89.                     ELSE
  90.                         'swap pieces
  91.                         SWAP tilepuz(t), tilepuz(sel)
  92.                         GOTO main
  93.                     END IF
  94.  
  95.                 END IF
  96.             END IF
  97.  
  98.         NEXT
  99.     END IF
  100.  
  101.     'Space give a little cheat look
  102.     IF INKEY$ = CHR$(32) THEN
  103.         FOR t = 1 TO row * col
  104.             _PUTIMAGE (tile.x(t), tile.y(t)), tile&(t)
  105.         NEXT
  106.         _DELAY .5: _KEYCLEAR
  107.         GOTO main
  108.     END IF
  109.  
  110.  
  111.  

korea.jpg
* korea.jpg (Filesize: 161.01 KB, Dimensions: 800x600, Views: 255)
« Last Edit: January 25, 2021, 11:29:39 am by Dav »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Unscramble the picture puzzle
« Reply #1 on: January 24, 2021, 11:55:52 pm »
I had to cheat.

This could also be a picture viewer.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Unscramble the picture puzzle
« Reply #2 on: January 25, 2021, 12:40:55 am »
I don't have a foggy idea, which is probably why I completed it.

Nice, but I like TheBOB's puzzle a bit more... because it's got boobs. I think there's a face that goes with it, but I didn't up to that level.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Unscramble the picture puzzle
« Reply #3 on: January 25, 2021, 11:32:28 am »
Thanks for trying it out, @NOVARSEG.  I had to take a look or two myself at first.

Lol, @Pete.  I didn't know TheBob used TheBoob.  I had a picture of a big egg at first, thinking it would be fun to unscamble an egg, but there where too many all white sguares that make it too hard to get in the right places.  Pictures need to be kind of busy, large space of solid colors don't work very well.

Updated the code and picture.  I added SPACE for a half sec cheat look at the picture.  Also, I replaced the image with one I took in South Korea a number of years ago.

- Dav
« Last Edit: January 25, 2021, 11:36:05 am by Dav »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Unscramble the picture puzzle
« Reply #4 on: January 25, 2021, 12:03:38 pm »
Oh, cheat means you had to go back to look at the original pic. Yeah, I did that when I was about 60% finished. I worked the cent out, first, but that fog was hard to put together without seeing the original photo.

TheBob hadn't coded anything in what, maybe 5 years? All of a sudden, he popped this up on the forum: https://www.tapatalk.com/groups/qbasic/anna-chlumsky-jigsaw-puzzle-t39631-s10.html#p213399

I'm not sure what it would take to make those puzzle pieces randomly rotate, but that would definitely up the level of difficulty.

Fun stuff, for as little code that can produce it. I say that because it takes thousands of lines of code to get any bells and whistles out of text apps. No instant gratification there. Dammit, now I want pudding.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Unscramble the picture puzzle
« Reply #5 on: January 26, 2021, 07:33:50 pm »
I'm not sure what it would take to make those puzzle pieces randomly rotate, but that would definitely up the level of difficulty.

I liked that idea, @Pete, so I made a new version that does that using the ultra-cool RotoZoom SUB (who made that, btw?  @bplus?).  With the right picture, it could be kind of difficult.

This new version you click on the pieces to rotate them in place.  It's animated.  This requires a squared screen resolution/image to work well, so I've used a new image of another picture I took in South Korea.  I was walking up a mountain somewhere and this beautiful gazebo was sitting by a stream. (photography is kind of a hobby sometimes...).

- Dav

Code: QB64: [Select]
  1. '==================
  2. 'UNSCRAMBLE-ROT.BAS
  3. '==================
  4. 'Unscramble the picture puzzle
  5. 'Rotate pieces until they fit.
  6. 'Coded by Dav, JAN/2021
  7.  
  8. 'Use mouse, click on squares to rotate them.
  9. 'SPACE key gives a little half second cheat look.
  10.  
  11. 'korea2.jpg picture was taken by Dav in South Korea
  12.  
  13. 'CREDITS: Thanks to whoever made and shared the RotoZoom SUB.
  14.  
  15.  
  16. SCREEN _NEWIMAGE(650, 650, 32)
  17.  
  18. 'Use your own image here....
  19. 'NOTE: USE A SQUARED IMAGE (ie: 600x600, 700x700, etc...)
  20. pic& = _LOADIMAGE("korea2.jpg", 32)
  21. _PUTIMAGE (0, 0)-(_WIDTH, _HEIGHT), pic&
  22.  
  23. SLEEP 2 'show picture couple of secs
  24.  
  25. row = 6: col = 6 '6x6 grid of pieces - increase for more pieces
  26. xsize = _WIDTH / row: ysize = _HEIGHT / col
  27. DIM SHARED tile&(row * col), tile.x(row * col), tile.y(row * col)
  28. DIM tilepuz(row * col)
  29.  
  30. '=== split picture up into tile pieces
  31. bc = 1
  32. FOR c = 1 TO col
  33.     FOR r = 1 TO row
  34.         x1 = (r * xsize) - xsize: x2 = x1 + xsize
  35.         y1 = (c * ysize) - ysize: y2 = y1 + ysize
  36.         tile.x(bc) = x1: tile.y(bc) = y1
  37.         'make pieces images
  38.         tile&(bc) = _NEWIMAGE(ABS(x2 - x1) + 1, ABS(y2 - y1) + 1, 32)
  39.         _PUTIMAGE (0, 0), _DEST, tile&(bc), (x1, y1)-(x2, y2)
  40.         tilepuz(bc) = bc
  41.         bc = bc + 1
  42.     NEXT
  43.  
  44. FOR p = 1 TO UBOUND(tilepuz)
  45.     tilepuz(p) = INT(RND * 4) + 1
  46.  
  47.  
  48. '=====
  49. main:
  50. '=====
  51.  
  52. '=== show current puzzle
  53. FOR t = 1 TO row * col
  54.     SELECT CASE tilepuz(t)
  55.         CASE 1: angle = 0
  56.         CASE 2: angle = 90
  57.         CASE 3: angle = 180
  58.         CASE 4: angle = 270
  59.     END SELECT
  60.     RotoZoom tile.x(t) + (xsize / 2), tile.y(t) + (ysize / 2), tile&(t), 1, angle
  61.  
  62.  
  63.  
  64. '=== check if puzzle done
  65. done = 1
  66. FOR t = 1 TO row * col
  67.     IF tilepuz(t) <> 1 THEN done = 0
  68. IF done = 1 THEN
  69.     PLAY "mbl16cdefgfedc": _DELAY 4
  70.     END 'Solved!
  71.  
  72.  
  73.  
  74.     'wait until mouse button up to continue
  75.     WHILE _MOUSEBUTTON(1) <> 0: n = _MOUSEINPUT: WEND
  76.  
  77.     trap = _MOUSEINPUT 'Poll mouse data
  78.  
  79.     'If user clicked mouse
  80.         'see where they clicked
  81.         mx = _MOUSEX: my = _MOUSEY
  82.  
  83.         FOR t = 1 TO row * col
  84.             tx = tile.x(t): tx2 = tile.x(t) + xsize
  85.             ty = tile.y(t): ty2 = tile.y(t) + ysize
  86.  
  87.             IF mx >= tx AND mx <= tx2 THEN
  88.                 IF my >= ty AND my <= ty2 THEN
  89.                     LINE (tx, ty)-(tx2, ty2), _RGB(0, 0, 0), BF
  90.                     temp& = _COPYIMAGE(_DISPLAY)
  91.                     SELECT CASE tilepuz(t)
  92.                         CASE 1: tilepuz(t) = 2
  93.                             FOR angle = 0 TO 90
  94.                                 _PUTIMAGE (0, 0), temp&
  95.                                 RotoZoom tile.x(t) + (xsize / 2), tile.y(t) + (ysize / 2), tile&(t), 1, angle
  96.                                 _DELAY .005
  97.                                 _DISPLAY
  98.                             NEXT
  99.                         CASE 2: tilepuz(t) = 3
  100.                             FOR angle = 90 TO 180
  101.                                 _PUTIMAGE (0, 0), temp&
  102.                                 RotoZoom tile.x(t) + (xsize / 2), tile.y(t) + (ysize / 2), tile&(t), 1, angle
  103.                                 _DELAY .005
  104.                                 _DISPLAY
  105.                             NEXT
  106.                         CASE 3: tilepuz(t) = 4
  107.                             FOR angle = 180 TO 270
  108.                                 _PUTIMAGE (0, 0), temp&
  109.                                 RotoZoom tile.x(t) + (xsize / 2), tile.y(t) + (ysize / 2), tile&(t), 1, angle
  110.                                 _DELAY .005
  111.                                 _DISPLAY
  112.                             NEXT
  113.                         CASE 4: tilepuz(t) = 1
  114.                             FOR angle = 270 TO 360
  115.                                 _PUTIMAGE (0, 0), temp&
  116.                                 RotoZoom tile.x(t) + (xsize / 2), tile.y(t) + (ysize / 2), tile&(t), 1, angle
  117.                                 _DELAY .005
  118.                                 _DISPLAY
  119.                             NEXT
  120.                     END SELECT
  121.                     _DISPLAY
  122.                     _FREEIMAGE temp&
  123.                     GOTO main
  124.                 END IF
  125.             END IF
  126.  
  127.         NEXT
  128.     END IF
  129.  
  130.     'Space give a little cheat look
  131.     IF INKEY$ = CHR$(32) THEN
  132.         FOR t = 1 TO row * col
  133.             _PUTIMAGE (tile.x(t), tile.y(t)), tile&(t)
  134.         NEXT
  135.         _DISPLAY
  136.         _DELAY .5: _KEYCLEAR
  137.         GOTO main
  138.     END IF
  139.  
  140.  
  141.  
  142.  
  143. SUB RotoZoom (X AS LONG, Y AS LONG, Image AS LONG, Scale AS SINGLE, Rotation AS SINGLE)
  144.     DIM px(3) AS SINGLE: DIM py(3) AS SINGLE
  145.     W& = _WIDTH(Image&): H& = _HEIGHT(Image&)
  146.     px(0) = -W& / 2: py(0) = -H& / 2: px(1) = -W& / 2: py(1) = H& / 2
  147.     px(2) = W& / 2: py(2) = H& / 2: px(3) = W& / 2: py(3) = -H& / 2
  148.     sinr! = SIN(-Rotation / 57.2957795131): cosr! = COS(-Rotation / 57.2957795131)
  149.     FOR i& = 0 TO 3
  150.         x2& = (px(i&) * cosr! + sinr! * py(i&)) * Scale + X: y2& = (py(i&) * cosr! - px(i&) * sinr!) * Scale + Y
  151.         px(i&) = x2&: py(i&) = y2&
  152.     NEXT
  153.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, H& - 1)-(W& - 1, H& - 1), Image& TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  154.     _MAPTRIANGLE _SEAMLESS(0, 0)-(W& - 1, 0)-(W& - 1, H& - 1), Image& TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
  155.  

korea2.jpg
* korea2.jpg (Filesize: 160.77 KB, Dimensions: 650x650, Views: 181)
« Last Edit: January 26, 2021, 07:35:50 pm by Dav »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Unscramble the picture puzzle
« Reply #6 on: January 26, 2021, 08:08:51 pm »
Quote
the ultra-cool RotoZoom SUB (who made that, btw?  @bplus?).

I wish, I did a mod that made the x scale independent of the y scale but Rotozoom can be found in Wiki possibly under _MAPTRIANGLE.  Confirmed, example #1

@Dav did you happen to catch MasterGy's puzzle maker?
 https://www.qb64.org/forum/index.php?topic=3261.msg125613#msg125613

But I know very well, more fun to roll your own :)
« Last Edit: January 26, 2021, 08:20:33 pm by bplus »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Unscramble the picture puzzle
« Reply #7 on: January 26, 2021, 08:38:12 pm »
@Dav did you happen to catch MasterGy's puzzle maker?
 https://www.qb64.org/forum/index.php?topic=3261.msg125613#msg125613

I think so, sounds familiar. Im trying to search for it, but the forum search function doesn't seem to be working for me at the moment.  I'll find it...

Edit: Woah! Found it. That is really cool.

Edit#2: i see now you gave a link! Sorry i didn't notice it at first.

- Dav
« Last Edit: January 26, 2021, 08:47:18 pm by Dav »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Unscramble the picture puzzle
« Reply #8 on: January 26, 2021, 11:00:35 pm »
... I did a mod that made the x scale independent of the y scale ...

Yes, I think I found it - RotoZoom2. Well done fiuguring out RotoZoom.

RotoZoom is such a handy thing.  I'm having fun making little screen effects with it.  Here's a desktop blender...

Code: QB64: [Select]
  1. IF INSTR(1, _OS$, "LINUX") THEN
  2.     PRINT "Sorry, Windows only.": END
  3.  
  4. SCREEN _NEWIMAGE(600, 600, 32)
  5.  
  6.     s& = _SCREENIMAGE
  7.     RotoZoom _WIDTH / 2, _HEIGHT / 2, s&, .89, a
  8.     a = a + 1: IF a >= 360 THEN a = a - 360
  9.     _FREEIMAGE s&
  10.     _DISPLAY: _LIMIT 60
  11.  
  12.  
  13. SUB RotoZoom (X AS LONG, Y AS LONG, Image AS LONG, Scale AS SINGLE, Rotation AS SINGLE)
  14.     DIM px(3) AS SINGLE: DIM py(3) AS SINGLE
  15.     W& = _WIDTH(Image&): H& = _HEIGHT(Image&)
  16.     px(0) = -W& / 2: py(0) = -H& / 2: px(1) = -W& / 2: py(1) = H& / 2
  17.     px(2) = W& / 2: py(2) = H& / 2: px(3) = W& / 2: py(3) = -H& / 2
  18.     sinr! = SIN(-Rotation / 57.2957795131): cosr! = COS(-Rotation / 57.2957795131)
  19.     FOR i& = 0 TO 3
  20.         x2& = (px(i&) * cosr! + sinr! * py(i&)) * Scale + X: y2& = (py(i&) * cosr! - px(i&) * sinr!) * Scale + Y
  21.         px(i&) = x2&: py(i&) = y2&
  22.     NEXT
  23.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, H& - 1)-(W& - 1, H& - 1), Image& TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  24.     _MAPTRIANGLE _SEAMLESS(0, 0)-(W& - 1, 0)-(W& - 1, H& - 1), Image& TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
  25.  
« Last Edit: January 26, 2021, 11:03:41 pm by Dav »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Unscramble the picture puzzle
« Reply #9 on: January 26, 2021, 11:10:21 pm »
Quote
RotoZoom is such a handy thing.

Indeed! Check this out, in demo has several demos ends with dancing spikes just follow directions.
* Another RotoZoom Demo.zip (Filesize: 11.24 KB, Downloads: 141)
« Last Edit: January 26, 2021, 11:14:59 pm by bplus »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Unscramble the picture puzzle
« Reply #10 on: January 26, 2021, 11:17:19 pm »
Wow!  The possibilities with this is great.  I'm gonna play with that for a while!

- Dav
« Last Edit: January 26, 2021, 11:31:57 pm by Dav »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Unscramble the picture puzzle
« Reply #11 on: January 27, 2021, 03:50:48 pm »
I've worked up an unscrambler for you:

Code: QB64: [Select]
  1. SCREEN _LOADIMAGE("korea.jpg", 32)
  2.  
  3.  
  4. m = _MEMIMAGE(0)
  5. Sort m
  6.  
  7. SUB Sort (m AS _MEM)
  8.     $IF 64BIT THEN
  9.         DIM ES AS _INTEGER64, EC AS _INTEGER64
  10.     $ELSE
  11.         DIM ES AS LONG, EC AS LONG
  12.     $END IF
  13.  
  14.     'IF NOT m.TYPE AND 65536 THEN EXIT SUB 'We won't work without an array
  15.     IF m.TYPE AND 1024 THEN DataType = 10
  16.     IF m.TYPE AND 1 THEN DataType = DataType + 1
  17.     IF m.TYPE AND 2 THEN DataType = DataType + 2
  18.     IF m.TYPE AND 4 THEN IF m.TYPE AND 128 THEN DataType = DataType + 4 ELSE DataType = 3
  19.     IF m.TYPE AND 8 THEN IF m.TYPE AND 128 THEN DataType = DataType + 8 ELSE DataType = 5
  20.     IF m.TYPE AND 32 THEN DataType = 6
  21.     IF m.TYPE AND 512 THEN DataType = 7
  22.  
  23.     'Convert our offset data over to something we can work with
  24.     DIM m1 AS _MEM: m1 = _MEMNEW(LEN(ES))
  25.     _MEMPUT m1, m1.OFFSET, m.ELEMENTSIZE: _MEMGET m1, m1.OFFSET, ES 'Element Size
  26.     _MEMPUT m1, m1.OFFSET, m.SIZE: _MEMGET m1, m1.OFFSET, EC 'Element Count will temporily hold the WHOLE array size
  27.     _MEMFREE m1
  28.  
  29.     EC = EC / ES - 1 'Now we take the whole element size / the size of the elements and get our actual element count.  We subtract 1 so our arrays start at 0 and not 1.
  30.     'And work with it!
  31.     DIM o AS _OFFSET, o1 AS _OFFSET, counter AS _UNSIGNED LONG
  32.  
  33.     SELECT CASE DataType
  34.         CASE 1 'BYTE
  35.             DIM temp1(-128 TO 127) AS _UNSIGNED LONG
  36.             DIM t1 AS _BYTE
  37.             i = 0
  38.             DO
  39.                 _MEMGET m, m.OFFSET + i, t1
  40.                 temp1(t1) = temp1(t1) + 1
  41.                 i = i + 1
  42.             LOOP UNTIL i > EC
  43.             i1 = -128
  44.             DO
  45.                 DO UNTIL temp1(i1) = 0
  46.                     _MEMPUT m, m.OFFSET + counter, i1 AS _BYTE
  47.                     counter = counter + 1
  48.                     temp1(i1) = temp1(i1) - 1
  49.                     IF counter > EC THEN EXIT SUB
  50.                 LOOP
  51.                 i1 = i1 + 1
  52.             LOOP UNTIL i1 > 127
  53.         CASE 2: 'INTEGER
  54.             DIM temp2(-32768 TO 32767) AS _UNSIGNED LONG
  55.             DIM t2 AS INTEGER
  56.             i = 0
  57.             DO
  58.                 _MEMGET m, m.OFFSET + i * 2, t2
  59.                 temp2(t2) = temp2(t2) + 1
  60.                 i = i + 1
  61.             LOOP UNTIL i > EC
  62.             i1 = -32768
  63.             DO
  64.                 DO UNTIL temp2(i1) = 0
  65.                     _MEMPUT m, m.OFFSET + counter * 2, i1 AS INTEGER
  66.                     counter = counter + 1
  67.                     temp2(i1) = temp2(i1) - 1
  68.                     IF counter > EC THEN EXIT SUB
  69.                 LOOP
  70.                 i1 = i1 + 1
  71.             LOOP UNTIL i1 > 32767
  72.         CASE 3 'SINGLE
  73.             DIM T3a AS SINGLE, T3b AS SINGLE
  74.             gap = EC
  75.             DO
  76.                 gap = 10 * gap \ 13
  77.                 IF gap < 1 THEN gap = 1
  78.                 i = 0
  79.                 swapped = 0
  80.                 DO
  81.                     o = m.OFFSET + i * 4
  82.                     o1 = m.OFFSET + (i + gap) * 4
  83.                     IF _MEMGET(m, o, SINGLE) > _MEMGET(m, o1, SINGLE) THEN
  84.                         _MEMGET m, o1, T3a
  85.                         _MEMGET m, o, T3b
  86.                         _MEMPUT m, o1, T3b
  87.                         _MEMPUT m, o, T3a
  88.                         swapped = -1
  89.                     END IF
  90.                     i = i + 1
  91.                 LOOP UNTIL i + gap > EC
  92.             LOOP UNTIL gap = 1 AND swapped = 0
  93.         CASE 4 'LONG
  94.             DIM T4a AS LONG, T4b AS LONG
  95.             gap = EC
  96.             DO
  97.                 gap = 10 * gap \ 13
  98.                 IF gap < 1 THEN gap = 1
  99.                 i = 0
  100.                 swapped = 0
  101.                 DO
  102.                     o = m.OFFSET + i * 4
  103.                     o1 = m.OFFSET + (i + gap) * 4
  104.                     IF _MEMGET(m, o, LONG) > _MEMGET(m, o1, LONG) THEN
  105.                         _MEMGET m, o1, T4a
  106.                         _MEMGET m, o, T4b
  107.                         _MEMPUT m, o1, T4b
  108.                         _MEMPUT m, o, T4a
  109.                         swapped = -1
  110.                     END IF
  111.                     i = i + 1
  112.                 LOOP UNTIL i + gap > EC
  113.             LOOP UNTIL gap = 1 AND swapped = 0
  114.         CASE 5 'DOUBLE
  115.             DIM T5a AS DOUBLE, T5b AS DOUBLE
  116.             gap = EC
  117.             DO
  118.                 gap = 10 * gap \ 13
  119.                 IF gap < 1 THEN gap = 1
  120.                 i = 0
  121.                 swapped = 0
  122.                 DO
  123.                     o = m.OFFSET + i * 8
  124.                     o1 = m.OFFSET + (i + gap) * 8
  125.                     IF _MEMGET(m, o, DOUBLE) > _MEMGET(m, o1, DOUBLE) THEN
  126.                         _MEMGET m, o1, T5a
  127.                         _MEMGET m, o, T5b
  128.                         _MEMPUT m, o1, T5b
  129.                         _MEMPUT m, o, T5a
  130.                         swapped = -1
  131.                     END IF
  132.                     i = i + 1
  133.                 LOOP UNTIL i + gap > EC
  134.             LOOP UNTIL gap = 1 AND swapped = 0
  135.         CASE 6 ' _FLOAT
  136.             DIM T6a AS _FLOAT, T6b AS _FLOAT
  137.             gap = EC
  138.             DO
  139.                 gap = 10 * gap \ 13
  140.                 IF gap < 1 THEN gap = 1
  141.                 i = 0
  142.                 swapped = 0
  143.                 DO
  144.                     o = m.OFFSET + i * 32
  145.                     o1 = m.OFFSET + (i + gap) * 32
  146.                     IF _MEMGET(m, o, _FLOAT) > _MEMGET(m, o1, _FLOAT) THEN
  147.                         _MEMGET m, o1, T6a
  148.                         _MEMGET m, o, T6b
  149.                         _MEMPUT m, o1, T6b
  150.                         _MEMPUT m, o, T6a
  151.                         swapped = -1
  152.                     END IF
  153.                     i = i + 1
  154.                 LOOP UNTIL i + gap > EC
  155.             LOOP UNTIL gap = 1 AND swapped = 0
  156.         CASE 7 'String
  157.             DIM T7a AS STRING, T7b AS STRING, T7c AS STRING
  158.             T7a = SPACE$(ES): T7b = SPACE$(ES): T7c = SPACE$(ES)
  159.             gap = EC
  160.             DO
  161.                 gap = INT(gap / 1.247330950103979)
  162.                 IF gap < 1 THEN gap = 1
  163.                 i = 0
  164.                 swapped = 0
  165.                 DO
  166.                     o = m.OFFSET + i * ES
  167.                     o1 = m.OFFSET + (i + gap) * ES
  168.                     _MEMGET m, o, T7a
  169.                     _MEMGET m, o1, T7b
  170.                     IF T7a > T7b THEN
  171.                         T7c = T7b
  172.                         _MEMPUT m, o1, T7a
  173.                         _MEMPUT m, o, T7c
  174.                         swapped = -1
  175.                     END IF
  176.                     i = i + 1
  177.                 LOOP UNTIL i + gap > EC
  178.             LOOP UNTIL gap = 1 AND swapped = false
  179.         CASE 8 '_INTEGER64
  180.             DIM T8a AS _INTEGER64, T8b AS _INTEGER64
  181.             gap = EC
  182.             DO
  183.                 gap = 10 * gap \ 13
  184.                 IF gap < 1 THEN gap = 1
  185.                 i = 0
  186.                 swapped = 0
  187.                 DO
  188.                     o = m.OFFSET + i * 8
  189.                     o1 = m.OFFSET + (i + gap) * 8
  190.                     IF _MEMGET(m, o, _INTEGER64) > _MEMGET(m, o1, _INTEGER64) THEN
  191.                         _MEMGET m, o1, T8a
  192.                         _MEMGET m, o, T8b
  193.                         _MEMPUT m, o1, T8b
  194.                         _MEMPUT m, o, T8a
  195.                         swapped = -1
  196.                     END IF
  197.                     i = i + 1
  198.                 LOOP UNTIL i + gap > EC
  199.             LOOP UNTIL gap = 1 AND swapped = 0
  200.         CASE 11: '_UNSIGNED _BYTE
  201.             DIM temp11(0 TO 255) AS _UNSIGNED LONG
  202.             DIM t11 AS _UNSIGNED _BYTE
  203.             i = 0
  204.             DO
  205.                 _MEMGET m, m.OFFSET + i, t11
  206.                 temp11(t11) = temp11(t11) + 1
  207.                 i = i + 1
  208.             LOOP UNTIL i > EC
  209.             i1 = 0
  210.             DO
  211.                 DO UNTIL temp11(i1) = 0
  212.                     _MEMPUT m, m.OFFSET + counter, i1 AS _UNSIGNED _BYTE
  213.                     counter = counter + 1
  214.                     temp11(i1) = temp11(i1) - 1
  215.                     IF counter > EC THEN EXIT SUB
  216.                 LOOP
  217.                 i1 = i1 + 1
  218.             LOOP UNTIL i1 > 255
  219.         CASE 12 '_UNSIGNED INTEGER
  220.             DIM temp12(0 TO 65535) AS _UNSIGNED LONG
  221.             DIM t12 AS _UNSIGNED INTEGER
  222.             i = 0
  223.             DO
  224.                 _MEMGET m, m.OFFSET + i * 2, t12
  225.                 temp12(t12) = temp12(t12) + 1
  226.                 i = i + 1
  227.             LOOP UNTIL i > EC
  228.             i1 = 0
  229.             DO
  230.                 DO UNTIL temp12(i1) = 0
  231.                     _MEMPUT m, m.OFFSET + counter * 2, i1 AS _UNSIGNED INTEGER
  232.                     counter = counter + 1
  233.                     temp12(i1) = temp12(i1) - 1
  234.                     IF counter > EC THEN EXIT SUB
  235.                 LOOP
  236.                 i1 = i1 + 1
  237.             LOOP UNTIL i1 > 65535
  238.         CASE 14 '_UNSIGNED LONG
  239.             DIM T14a AS _UNSIGNED LONG, T14b AS _UNSIGNED LONG
  240.             gap = EC
  241.             DO
  242.                 gap = 10 * gap \ 13
  243.                 IF gap < 1 THEN gap = 1
  244.                 i = 0
  245.                 swapped = 0
  246.                 DO
  247.                     o = m.OFFSET + i * 4
  248.                     o1 = m.OFFSET + (i + gap) * 4
  249.                     IF _MEMGET(m, o, _UNSIGNED LONG) > _MEMGET(m, o1, _UNSIGNED LONG) THEN
  250.                         _MEMGET m, o1, T14a
  251.                         _MEMGET m, o, T14b
  252.                         _MEMPUT m, o1, T14b
  253.                         _MEMPUT m, o, T14a
  254.                         swapped = -1
  255.                     END IF
  256.                     i = i + 1
  257.                 LOOP UNTIL i + gap > EC
  258.             LOOP UNTIL gap = 1 AND swapped = 0
  259.         CASE 18: '_UNSIGNED _INTEGER64
  260.             DIM T18a AS _UNSIGNED _INTEGER64, T18b AS _UNSIGNED _INTEGER64
  261.             gap = EC
  262.             DO
  263.                 gap = 10 * gap \ 13
  264.                 IF gap < 1 THEN gap = 1
  265.                 i = 0
  266.                 swapped = 0
  267.                 DO
  268.                     o = m.OFFSET + i * 8
  269.                     o1 = m.OFFSET + (i + gap) * 8
  270.                     IF _MEMGET(m, o, _UNSIGNED _INTEGER64) > _MEMGET(m, o1, _UNSIGNED _INTEGER64) THEN
  271.                         _MEMGET m, o1, T18a
  272.                         _MEMGET m, o, T18b
  273.                         _MEMPUT m, o1, T18b
  274.                         _MEMPUT m, o, T18a
  275.                         swapped = -1
  276.                     END IF
  277.                     i = i + 1
  278.                 LOOP UNTIL i + gap > EC
  279.             LOOP UNTIL gap = 1 AND swapped = 0
  280.     END SELECT
  281.  

Use with the "korea.jpg" attached to the first post.
Compile and run.
Hit a key when ready...
...and watch it unscramble all of your colors and arrange them in order from smallest RGB value to largest!

(Works in SCREEN 0 and 256-color mode as well!)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Unscramble the picture puzzle
« Reply #12 on: January 27, 2021, 06:36:15 pm »
Wow, Steve, you put a lot of work into that obviously. Thanks for sharing it with me.

The code is beyond me, ill have to put on a pot of coffee and study it (the code, not coffee).

I was thinking what a cool effect it would be to reverse that, and assemble the picture from the color order.

Thanks for posting

- Dav

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Unscramble the picture puzzle
« Reply #13 on: January 27, 2021, 06:42:04 pm »
Wow, Steve, you put a lot of work into that obviously. Thanks for sharing it with me.

The code is beyond me, ill have to put on a pot of coffee and study it (the code, not coffee).

I was thinking what a cool effect it would be to reverse that, and assemble the picture from the color order.

Thanks for posting

- Dav

Actually, it took about 2 minutes to do.

Sort is my _MEMSORT routine which has been on the forums here forever: https://www.qb64.org/forum/index.php?topic=1601.0

I just commented out one line in it that error checks for non-array types, and then pasted it.  Here’s the actual I just wrote:

SCREEN _LOADIMAGE("korea.jpg", 32)
DIM m AS _MEM
SLEEP
 
m = _MEMIMAGE(0)
Sort m
SLEEP

************

Print some stuff on a SCREEN 0 text screen and try it.  It’s easier there to comprehend the results when it “unscrambles” the letters on the screen.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Unscramble the picture puzzle
« Reply #14 on: January 27, 2021, 07:24:00 pm »
SCREEN ZERO rides again!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/