Author Topic: Square Elimination Game  (Read 715 times)

B+

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Square Elimination Game
« on: June 05, 2015 »
Finally I got a NaaLaa version of this game going! I had to figure out a different way to catch mouse down and mouse up events. It works very nicely with NaaLaa.

Code: [Select]
rem Square Elimination game.txt for NaaLaa 2015-06-05
procedure update()
set color 0,0,0
draw rect tlx-1,tly-1,sq*10+1,sq*10+1,true
   for x=1 to 10
      for y=1 to 10
         if board$[x][y]="R"
            set color 255,0,0
         elseif board$[x][y]="B"
            set color 0,0,255
         else
            set color 0,0,0
         endif
         draw rect tlx+sq*(x-1),tly+sq*(y-1),sq-1,sq-1,true
      next
   next
endproc
function bx(mx)
   rtn=(mx-tlx)/sq +1
   if rtn<11 and rtn>0
      return rtn
   else
      return 0
   endif
endfunc
function by(my)
   rtn=(my-tly)/sq +1
   if rtn<11 and rtn>0 then
      return rtn
   else
      return 0
   endif
endfunc
function moved(x1,y1,x2,y2)
   if x1 and y1 and x2 and y2
      if x1=x2
         if y1-y2=1
            return 1
         elseif y2-y1=1
            return 3
         else
            return 0
         endif
      elseif y1=y2
         if x1-x2=1
            return 4
         elseif x2-x1=1
            return 2
         else
            return 0
         endif
      else
         return 0
      endif
   else
      return 0
   endif
endfunc
rem =============================== main
randomize(time())
visible:
board$[11][11]
wside=700
th=18
tly=3*th
sq=(wside-5*th)/10
tlx=(wside-10*sq)/2
hidden:
set window (screenw()-wside)/2,20,wside,wside
set redraw off
set color 0, 220, 220
set caret wside/2, 0
center"SQUARE ELIMINATION: Mouse down and move over to like color square,"
center"release, this slides row or column leaving blank at end. Perfect game"
center"is 2 squares left on board. Good luck!  <esc> to quit with score."
rem setup board
for y=1 to 10
   for x=1 to 10
      board$[x][y]= mid$("RB",rnd(2),1)
   next
next
dx=0;dy=0;ux=0;uy=0
rem =========================================== main loop
do
mx=mousex()
my=mousey()
_update
if mx>tlx and mx<tlx+10*sq and my>tly and my<tly+10*sq
if mousebutton(0)
if dx<>0 and dy<>0 and board$[dx][dy]<>" "
set color 255,255,255
draw rect tlx+sq*(dx-1)-1,tly+sq*(dy-1)-1,sq+1,sq+1
endif
if dx=0 and dy=0
dx=bx(mx)
dy=by(my)
ux=0;uy=0
endif
else
if dx and dy
ux=bx(mx)
uy=by(my)
moov=moved(dx,dy,ux,uy)
    if moov and board$[dx][dy]=board$[ux][uy] and board$[dx][dy]<>" "
      if moov=1
          rem  Moved up
            for i=dy to 10
               board$[dx][i-1]=board$[dx][i]
            next
            board$[dx][10]=" "
      elseif moov=2
          rem Moved right
          for i=dx downto 1
               board$[i+1][dy]=board$[i][dy]
            next
            board$[1][dy]=" "
      elseif moov=3
rem Moved down
            for i=dy downto 1
               board$[dx][i+1]= board$[dx][i]
            next
            board$[dx][1]=" "
      elseif moov=4
          rem Moved left
            for i=dx to 10
               board$[i-1][dy]= board$[i][dy]
            next
            board$[10][dy]=" "
      endif
    endif

dx=0;dy=0
endif
endif
endif
redraw
wait 10
until keydown(27)
lcount=0
for i=1 to 10
   for j=1 to 10
      if board$[i][j]<>" " then lcount=lcount +1
   next
next
set color 0, 200, 200
set caret wside/2, th*3+sq*10
center"You have left "+str(lcount)+" squares of 100 on board."
center"Thanks for playing!  press any key..."
redraw
wait keydown
B+

johnno56

  • Guest
Re: Square Elimination Game
« Reply #1 on: June 06, 2015 »
Ran it on my Linux box via Wine and ran like a charm... still a cool game...  ;)

J

B+

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: Square Elimination Game
« Reply #2 on: June 06, 2015 »
Ran it on my Linux box via Wine and ran like a charm... still a cool game...  ;)

J

Thanks this is reassuring. I like the smaller size of the exe (comparing to sdlBasic) plus I think it runs a shade faster highliting the mouse down squares.

B+
B+

johnno56

  • Guest
Re: Square Elimination Game
« Reply #3 on: June 06, 2015 »
As long as it runs as quick as you anticipated.

J

Rick3137

  • Sr. Member
  • ****
  • Posts: 347
  • May the Force be with You
    • View Profile
    • Rick's Programs
Re: Square Elimination Game
« Reply #4 on: June 07, 2015 »
 Nice game. A little hard on the brain. I had about 7 left over. Some day, when I get my brain working again, I'll give it another try.
Can I put it on my website?

Rick3137

  • Sr. Member
  • ****
  • Posts: 347
  • May the Force be with You
    • View Profile
    • Rick's Programs
Re: Square Elimination Game
« Reply #5 on: June 07, 2015 »
   3 squares left, this time. This is one tricky little game.

B+

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: Square Elimination Game
« Reply #6 on: June 07, 2015 »
Hi Rick,

Yes, I am pleased you like it!

I like to think you can't win every time, like in Solitaire, it depends on what you are dealt. BUT if you get one side red and the other blue then you better win!

Wait I just realized you could get all one color, 2 ways red or blue, 2^100/2=2^99.

And I was wondering if more "points" should be awarded according to distance between the red square and blue at the end of a "perfect" game. In which case, having the red and blue at opposite corners would be the ultimate win (other than winning the "lottery" of all one color).
But this is after the game becomes automatic.

This is what I live for, freely sharing fun!

Mark
B+