Mouse-click game

Game development specific discussions.
Post Reply
hhr
Posts: 254
Joined: Nov 29, 2019 10:41

Mouse-click game

Post by hhr »

A very old and simple mouse-click game, level 1 only.
Change the color with 4 clicks.

Code: Select all

#cmdline "-s gui"
screen 0 'Useful in Linux with a slow terminal
windowtitle "MouseFlip"

dim as long x,y,buttons,result,windowsize,imagesize

windowsize = 400
windowsize -= (windowsize mod 2)
imagesize = (windowsize\2)-1

screenres windowsize,windowsize,32

dim as byte ptr image(1 to 2)
image(1) = imagecreate(imagesize,imagesize,rgb(127,255,0))
image(2) = imagecreate(imagesize,imagesize,rgb(255,127,0))
line image(1),(0,0)-(imagesize,imagesize),0,B
line image(2),(0,0)-(imagesize,imagesize),0,B

dim as byte c(0 to 3)

do
   put (0,0),image(c(0)+2),pset
   put (0,imagesize+1),image(c(1)+2),pset
   put (imagesize+1,0),image(c(2)+2),pset
   put (imagesize+1,imagesize+1),image(c(3)+2),pset
   
   result = getmouse (x,y,,buttons)
   if (result = 0) and (buttons <> 0) then
      if (x < imagesize+1) and (y < imagesize+1) then c(0) = not(c(0)) : c(1) = not(c(1)) : c(2) = not(c(2))
      if (x > imagesize+1) and (y < imagesize+1) then c(0) = not(c(0)) : c(2) = not(c(2)) : c(3) = not(c(3))
      if (x < imagesize+1) and (y > imagesize+1) then c(0) = not(c(0)) : c(1) = not(c(1)) : c(3) = not(c(3))
      if (x > imagesize+1) and (y > imagesize+1) then c(1) = not(c(1)) : c(2) = not(c(2)) : c(3) = not(c(3))
   end if
   
   do
      result = getmouse (x,y,,buttons)
      sleep 10,1
   loop until (result <> 0) or (buttons = 0)
   
loop until len(inkey)

imagedestroy image(1)
imagedestroy image(2)
end
hhr
Posts: 254
Joined: Nov 29, 2019 10:41

Re: Mouse-click game

Post by hhr »

You can specify the level in line 7. Levels 1 to 3 are easy. I cannot solve any other levels, maybe that's not possible.

Code: Select all

#cmdline "-s gui -exx"
screen 0 'Useful in Linux with a slow terminal
windowtitle "MouseFlip"

dim as long level,x,y,buttons,result,windowsize,imagesize

level = 2

windowsize = 400
windowsize -= (windowsize mod (level+1))
imagesize = (windowsize\(level+1))-1

screenres windowsize,windowsize+50,32
draw string (windowsize/2-21,windowsize+25),"Reset"

dim as byte ptr image(1 to 2)
image(1) = imagecreate(imagesize,imagesize,rgb(127,255,0))
image(2) = imagecreate(imagesize,imagesize,rgb(255,127,0))
line image(1),(0,0)-(imagesize,imagesize),0,B
line image(2),(0,0)-(imagesize,imagesize),0,B

dim as byte c(1 to level+1,1 to level+1)
dim as long i,j

do
   for i = 0 to level
      for j = 0 to level
         put (i*(imagesize+1),j*(imagesize+1)),image(c(i+1,j+1)+2),pset
      next j
   next i
   
   result = getmouse (x,y,,buttons)
   if (result = 0) and (buttons <> 0) then
      if y >= windowsize then 'Reset
         for i = 1 to level+1
            for j = 1 to level+1
               c(i,j) = 0
            next j
         next i
      else 'Change colors
         i = ((level+1)*x\windowsize)+1
         j = ((level+1)*y\windowsize)+1
         c(i,j) = not(c(i,j))
         if i <= level then c(i+1,j) = not(c(i+1,j))
         if i > 1 then c(i-1,j) = not(c(i-1,j))
         if j <= level then c(i,j+1) = not(c(i,j+1))
         if j > 1 then c(i,j-1) = not(c(i,j-1))
      end if
   end if
   
   do
      result = getmouse (x,y,,buttons)
      sleep 10,1
   loop until (result <> 0) or (buttons = 0)
   
loop until len(inkey)

imagedestroy image(1)
imagedestroy image(2)
end
Löwenherz
Posts: 219
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Mouse-click game

Post by Löwenherz »

nice little game hhr :-)

start: orange.. all 9 fields default setup
then clicking..

1) top right
2) bottom left
3) bottom right
4) top left

5) then click in center field
all 9 fields are changing to green ok
But I need 5 clicks didnt read your Post before
hhr
Posts: 254
Joined: Nov 29, 2019 10:41

Re: Mouse-click game

Post by hhr »

25 years ago the game was available as Flip.exe in a booklet CD.

I found it here a few days ago:
https://www.matheprisma.uni-wuppertal.d ... node13.htm

Level 1 (2x2): 4 clicks
Level 2 (3x3): 5 clicks
Level 3 (4x4): 4 clicks
Level 4 (5x5), click on the fields marked with 1 in any order:
01101
01110
00111
11011
11000
But this may not be an optimal solution.

Here is an example that shows which fields you have clicked on.
Because two clicks on the same field cancel each other out, you can remove two identical entries.

Code: Select all

#cmdline "-exx"

windowtitle "MouseFlip"

open cons for output as #1

dim as long level,x,y,buttons,result,windowsize,imagesize

level = 3

windowsize = 400
windowsize -= (windowsize mod (level+1))
imagesize = (windowsize\(level+1))-1

screenres windowsize,windowsize+50,32
draw string(windowsize/2-21,windowsize+25),"Reset"

dim as byte ptr image(1 to 2)
image(1) = imagecreate(imagesize,imagesize,rgb(127,255,0))
image(2) = imagecreate(imagesize,imagesize,rgb(255,127,0))
line image(1),(0,0)-(imagesize,imagesize),0,B
line image(2),(0,0)-(imagesize,imagesize),0,B

dim as byte c(1 to level+1,1 to level+1)
dim as boolean clicked(1 to level+1,1 to level+1)
dim as long i,j

do
   for i = 0 to level
      for j = 0 to level
         put (i*(imagesize+1),j*(imagesize+1)),image(c(i+1,j+1)+2),pset
         if clicked(i+1,j+1) = true then draw string(i*(imagesize+1)+5,j*(imagesize+1)+5),"C",rgb(0,0,0)
      next j
   next i
   
   result = getmouse(x,y,,buttons)
   if (result = 0) and (buttons <> 0) then
      if y >= windowsize then 'Reset
         for i = 1 to level+1
            for j = 1 to level+1
               c(i,j) = 0
               clicked(i,j) = false
            next j
         next i
         print #1,
         print #1,
      else 'Change colors
         i = ((level+1)*x\windowsize)+1
         j = ((level+1)*y\windowsize)+1
         print #1,"x";str(i);"y";str(j);"|";
         c(i,j) = not(c(i,j))
         clicked(i,j) = true
         if i <= level then c(i+1,j) = not(c(i+1,j))
         if i > 1 then c(i-1,j) = not(c(i-1,j))
         if j <= level then c(i,j+1) = not(c(i,j+1))
         if j > 1 then c(i,j-1) = not(c(i,j-1))
      end if
   end if
   
   do
      result = getmouse(x,y,,buttons)
      sleep 20,1
   loop until (result <> 0) or (buttons = 0)
   
loop until len(inkey)

imagedestroy image(1)
imagedestroy image(2)
end
Translated with DeepL.com (free version)
hhr
Posts: 254
Joined: Nov 29, 2019 10:41

Re: Mouse-click game

Post by hhr »

This example finds solutions:

Code: Select all

dim as ulong level = 4

'=========================================================
'Some segments are clicked on according to a given number.
'Then further segments are systematically clicked on and
'then a check is made to see if the game works.

'Example for a 5x5 field:
'n = 3, bin(n) = 11, corresponds to 00011
'In the first line, click on the two segments on the right.
'Then, starting in the second line, click on segments line by line,
'if the segment above is to be switched.

'---CC
'CC-CC
'CCC--
'-CCC-
'C-CC-
'=========================================================

declare sub click(s2() as string, x as ulong, y as ulong, level as ulong)
declare function test(s2() as string, level as ulong) as boolean

dim as ulong x, y, clicks
dim as ulongint n
dim as string s0, s1(level), s2(level), s3(level)

do
   clicks = 0
   
   's0 as a binary representation of n with leading zeros:
   s0 = bin(n, (level+1)*(level+1))
   
   'enter s0 into the array s1():
   for y = 0 to level
      s1(y) = mid(s0, len(s0)-(y+1)*(level)-y, level+1)
   next y
   
   'Prepare the array s2():
   for y = 0 to level
      s2(y) = string(level+1, "0")
   next y
   
   'Click in the array s2() as specified by the array s1():
   for y = 0 to level
      for x = 0 to level
         if s1(y)[x] = asc("1") then
            click(s2(), x, y, level)
            clicks += 1
         end if
      next x
   next y
   
   'Copy s2() to s3().
   'Work continues with s2(), s3() serves as a backup for display.
   for y = 0 to level
      s3(y) = s2(y)
   next y
   
   'Pursuing strategy:
   'Starting with the first line look for the first segment to be switched.
   'Click on the segment below it.
   'Then find the next segment to toggle, click the segment below it, etc.
   for y = 1 to level
      for x = 0 to level
         if s2(y-1)[x] = asc("0") then
            click(s2(), x, y, level)
            clicks += 1
         end if
      next x
   next y
   
   'Testing whether the strategy worked with this number n.
   'If so, then show results:
   if test(s2(), level) = true then
      print "Level = "; level
      print "bin(n) = "; bin(n); spc(3); "clicks = "; clicks
      print
      print "click:", "result:"
      
      for y = 0 to level
         print s1(y), s3(y)
      next y
      
      print string(20, "-")
      sleep
   end if
   
   'Increase n by 1, start new run.
   n += 1
   
loop

'=============================================================

sub click(s2() as string, x as ulong, y as ulong, level as ulong)
   
   if s2(y)[x] = asc("0") then
      s2(y)[x] = asc("1")
   else
      s2(y)[x] = asc("0")
   end if
   
   if x < level then
      if s2(y)[x+1] = asc("0") then
         s2(y)[x+1] = asc("1")
      else
         s2(y)[x+1] = asc("0")
      end if
   end if
   
   if x > 0 then
      if s2(y)[x-1] = asc("0") then
         s2(y)[x-1] = asc("1")
      else
         s2(y)[x-1] = asc("0")
      end if
   end if
   
   if y < level then
      if s2(y+1)[x] = asc("0") then
         s2(y+1)[x] = asc("1")
      else
         s2(y+1)[x] = asc("0")
      end if
   end if
   
   if y > 0 then
      if s2(y-1)[x] = asc("0") then
         s2(y-1)[x] = asc("1")
      else
         s2(y-1)[x] = asc("0")
      end if
   end if
end sub

function test(s2() as string, level as ulong) as boolean
   dim as ulong x, solved
   
   for x = 0 to level
      solved += valint(chr(s2(level)[x]))
   next x
   
   if solved = level+1 then return true else return false
end function
hhr
Posts: 254
Joined: Nov 29, 2019 10:41

Re: Mouse-click game

Post by hhr »

Just an exercise:

Code: Select all

#cmdline "-s gui -exx"

windowtitle "MouseFlip"

open cons for output as #1

dim as long level,x,y,mousebuttons,mouseresult,windowsize,imagesize,buttonwidth,button,maxlevel

level = 0

windowsize = 420 '' = 2*2*3*5*7, divisible by 1,2,3,4,5,6,7
maxlevel = 6 '' = greatest divisor - 1

buttonwidth = 60

screenres windowsize,windowsize+50,32,2
screenset 1,0

dim as byte ptr image(1 to 2)

#macro refresh
   imagesize = (windowsize\(level+1))-1
   image(1) = imagecreate(imagesize,imagesize,rgb(127,255,0))
   image(2) = imagecreate(imagesize,imagesize,rgb(255,127,0))
   line image(1),(0,0)-(imagesize,imagesize),0,B
   line image(2),(0,0)-(imagesize,imagesize),0,B
   
   line (1,windowsize)-(buttonwidth,windowsize+24),rgb(255,255,255),BF
   draw string (5,windowsize+8),"Level+",rgb(0,0,0)
   
   line (1,windowsize+26)-(buttonwidth,windowsize+48),rgb(255,255,255),BF
   draw string (5,windowsize+34),"Level-",rgb(0,0,0)
   
   line (2*buttonwidth,windowsize)-(windowsize-2,windowsize+48),rgb(255,255,255),BF
   draw string (4*buttonwidth,windowsize+21),"Reset",rgb(0,0,0)
   
   redim as byte c(1 to level+1,1 to level+1)
   redim as boolean clicked(1 to level+1,1 to level+1)
#endmacro

refresh

dim as long i,j

do
   
   for i = 0 to level
      for j = 0 to level
         put (i*(imagesize+1),j*(imagesize+1)),image(c(i+1,j+1)+2),pset
         if clicked(i+1,j+1) = true then draw string(i*(imagesize+1)+5,j*(imagesize+1)+5),"C",rgb(0,0,0)
      next j
   next i
   
   draw string (buttonwidth+11,windowsize+5),"Level "
   draw string (buttonwidth+27,windowsize+20),str(level)
   draw string (buttonwidth+11,windowsize+35),"(" & str(level+1) & "x" & str(level+1) & ")"
   
   screencopy
   
   mouseresult = getmouse(x,y,,mousebuttons)
   if (mouseresult = 0) and (mousebuttons <> 0) then
      if y >= windowsize then
         
         button = x\buttonwidth
         
         if button = 0 then
            
            if y > windowsize+25 then
               if level > 0 then level -= 1
            else
               if level < maxlevel then level += 1
            end if
            
            imagedestroy image(1)
            imagedestroy image(2)
            cls
            refresh
         end if
         
         if button > 1 then ''Reset
            for i = 1 to level+1
               for j = 1 to level+1
                  c(i,j) = 0
                  clicked(i,j) = false
               next j
            next i
         end if
         
         print #1,
         print #1,
         
      else ''Change colors
         i = ((level+1)*x\windowsize)+1
         j = ((level+1)*y\windowsize)+1
         print #1,"x";str(i);"y";str(j);"|";
         c(i,j) = not(c(i,j))
         clicked(i,j) = true
         if i <= level then c(i+1,j) = not(c(i+1,j))
         if i > 1 then c(i-1,j) = not(c(i-1,j))
         if j <= level then c(i,j+1) = not(c(i,j+1))
         if j > 1 then c(i,j-1) = not(c(i,j-1))
      end if
   end if
   
   do
      mouseresult = getmouse(x,y,,mousebuttons)
      sleep 20,1
   loop until (mouseresult <> 0) or (mousebuttons = 0)
   
loop until len(inkey)

end
Post Reply