Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 02:56 28 Jun 2022 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2: Game: Sort Digits by Reversing and Win

Author Message
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 883
Posted: 03:55am 18 Jul 2020
Copy link to clipboard 
Print this post

Here is a silly diversion.

Your goal is to sort a set of digits in ascending order. However, you can only manipulate the digits by reversing as many digits as you choose from the left side.

I was perusing my old copy of "The Best of Creative Computing" from 1976ish and remembered this one, although I had never before typed in the program. Below is my complete re-write, using only the original idea and polishing it to run nicely on the CMM2.

View the original article here, with source code on the next page.



'
'     Reverse
'  game concept from
' Best of Creative Computing Volume 1
'
' written by Vegipete for the Colour Maximite 2
' July, 2020

dim r(20),tmp(20)

basey = 400   ' where to put things on screen

do
 cls

 yp = 52
 text 110,basey+yp,"REVERSE","CT",3,1,rgb(magenta)
 text 111,basey+yp,"REVERSE","CT",3,1,rgb(magenta),-1
 blit 40,basey+yp,41,basey+yp,200,16
 blit 40,basey+yp,41,basey+yp,200,12
 blit 40,basey+yp,41,basey+yp,200,8
 blit 40,basey+yp,41,basey+yp,200,4

 text 185,basey+60,"Put the digits in order.","LT",1,1,rgb(magenta)
 print @(10,basey+80) " Use " chr$(149) " " chr$(148);
 print " to select how many to reverse, [SPACE] to reverse."
 print "   How many moves do you need?
 print
 print "   Original game from 'The Best of Creative Computing, Volume 1'"
 print "   Colour Maximite 2 version by vegipete"

 size = 9
 shuffle
 revp = 2
 count = 0
 'if count = 0 then text 135,505,"Start:","CT",1,1,rgb(cyan)
 text 135,basey+5,"Start:","CT",1,1,rgb(cyan)
 showcurrent(rgb(brown))
 drawpointer
 do
   k = asc(inkey$)   ' look for key presses
   select case k
     case 130    ' left arrow
       revp = revp - (revp>2)
       drawpointer
     case 131    ' right arrow
       revp = revp + (revp<size)
       drawpointer
     case 32     ' space bar
       reverse(revp)
       blit 70,20,70, 0,600,basey+0  ' scroll previous work up
       box 70,basey+0,700,20,1,0,0   ' erase bottom row
       count = count + 1
       showcurrent(rgb(green))       ' display new configuration
       if r(0) then
         text 135,basey+5,"Solved!","CT",1,1,rgb(cyan)
         exit do   ' drop out of game loop
       endif
       drawpointer
   end select
 loop
 print @(100,basey+40) "Press [ENTER] to go again."
 do : k = asc(inkey$) : loop until k <> 0  ' wait for a keypress
 if k <> 13 then print @(0,0) : end   ' quit for any press other than [ENTER]
loop
end

' reverse the desired number of elements, check if all are in order
sub reverse(cnt as integer)
 for i = 1 to cnt
   tmp(i) = r(i)
 next i

 for i = 1 to cnt
   r(i) = tmp(cnt + 1 - i)
 next i

 r(0) = 0  ' indicate not sorted
 for i = 1 to size
   if r(i) <> i then exit sub
 next i
 r(0) = 1  ' all in order!
end sub

' display the current digit order
sub showcurrent(c)
 text 80,basey+0,str$(count),"CT",3,1,rgb(yellow)
 for i = 1 to size
   text 150+20*i,basey+0,str$(r(i)),"CT",3,1,c
 next i
end sub

' draw an indicator under sub-set to be reversed
sub drawpointer
 box 160,basey+23,700,10,1,0,0 ' erase old one
 box 160,basey+23,20*revp,10,1,&hFFFFFF,&hFFFFFF
 box 163,basey+23,20*revp-6,4,1,0,0
end sub

' fill the array, then shuffle the elements
sub shuffle
 local i,w,tmp

 for i = 1 to size
   r(i) = i
 next i

 ' swap each element with a randomly chosen one
 for i = 1 to size
   w = int(rnd * size) + 1
   tmp = r(i)
   r(i) = r(w)
   r(w) = tmp
 next i
end sub


Visit Vegipete's *Mite Library for cool programs.
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2022