Page 1 of 1

Memory game version 2

Posted: Mon May 06, 2013 12:14 pm
by Henko
' Memory game using new touch statement (version 2)
'
option base 1
dim m(8,8),xy(2)
gosub init_prog             ' initialization
cls ! turns=0
tab_gen(m,t_size,sc)          ' generate table with values
disp(m,t_size,sc)             ' display the tableau
loop6:
get_xy(xy,t_size,sc)          ' get first touch
i1=xy(1) ! j1=xy(2)
file "test_mem" print i1,j1
if m(i1,j1)>0 then loop6    ' already been guessed
disp_vak(m,i1,j1,sc,1)      ' display in red (1)
pause .2                    ' needed to separate two touches
loop7:
get_xy(xy,t_size,sc)          ' get second touch
i2=xy(1) ! j2=xy(2)
if m(i2,j2)>0 then loop7    ' already been guessed
turns=turn(turns,t_size,sc)   ' count turns and print them
if m(i1,j1)=m(i2,j2) then   ' equal numbers!
  suc=suc+1
  m(i1,j1)=-m(i1,j1) ! m(i2,j2)=-m(i2,j2)
  disp_vak(m,i2,j2,sc,0) ! disp_vak(m,i1,j1,sc,0)
  else
  disp_vak(m,i2,j2,sc,1) ! pause 1
  fill_vak(i1,j1,sc,.5,.5,.5) ! fill_vak(i2,j2,sc,.5,.5,.5)  
  end if
if suc=t_size*t_size/2 then
  draw text "Congratulations!!" at 5*sc,(10+12*t_size)*sc
  pause 3 ! stop
  end if
goto loop6
end

def turn(turns,t_size,sc)
turns=turns+1 ! t$=turns
y1=(5+12*t_size)*sc ! y2=(15+12*t_size)*sc
fill rect 5*sc,y1 to 40*sc,y2
draw text "turns : " & t$ at 5*sc,(5+12*t_size)*sc
turn=turns
end def

def disp_vak(m(,),i,j,sc,ind)   
if ind=1 then
  n$=-m(i,j) ! draw color .7,0,0
  else 
  n$=m(i,j) ! draw color 0,.7,0
  end if
fill_vak(i,j,sc,.8,.8,.8)
draw text n$ at (12*i-6)*sc,(12*j-7)*sc
end def

def fill_vak(i,j,sc,r,g,b)
fill color r,g,b
fill rect (12*i-9)*sc,(12*j-9)*sc to (1+12*i)*sc,(1+12*j)*sc
end def

def get_xy(xy(),t_size,sc)
loop_xy:
x=touch_x(0) ! y=touch_y(0) ! If y<0 then y=0
if x=-1 then loop_xy
xy(1)=1+int(x/sc/12) ! xy(2)=1+int(y/sc/12)
if xy(1)>t_size then xy(1)=t_size
if xy(2)>t_size then xy(2)=t_size
end def

def tab_gen(m(,),t_size,sc)
for k=1 to t_size*t_size/2
  for d=1 to 2
    loop5: i=1+rnd(t_size) ! j=1+rnd(t_size)
      if m(i,j) then loop5 else m(i,j)=-k
    next d
  next k
draw size 3
for i=0 to t_size
  draw line (2+12*i)*sc,2*sc to (2+12*i)*sc,(2+12*t_size)*sc
  draw line 2*sc,(2+12*i)*sc to (2+12*t_size)*sc,(2+12*i)*sc
next i
end def

def disp(m(,),t_size,sc)
fill color .5,.5,.5
draw color 0,.5,0
draw font size 4*sc
for i=1 to t_size
  for j=1 to t_size
    if m(i,j)>0 then
      n$=m(i,j)
      draw text n$ at (12*i-6)*sc,(12*j-7)*sc
      else
      fill rect (12*i-9)*sc,(12*j-9)*sc to (1+12*i)*sc,(1+12*j)*sc
      end if
    next j
  next i
end def

def cls()
graphics clear .8,.8,.8
end def

init_prog:
randomize
graphics
cls
file "test_mem" delete
fill color .8,.8,.8 ! draw color 0,0,0
loop1:
maxx=screen_width() ! maxy=screen_height()
if maxx>maxy then
  draw text "Please rotate to portrait" at 10,100
  goto loop1
  end if
cls
sc=maxx/100
draw text "Select size of tableau" at sc*10,sc*5
button "4" title "4x4" at sc*10,sc*20 size sc*15,40
button "6" title "6x6" at sc*40,sc*20 size sc*15,40
button "8" title "8x8" at sc*70,sc*20 size sc*15,40
loop2:
for i=4 to 8 step 2
  i$=i
  if button_pressed(i$) then loop3
  next i
goto loop2
loop3:
t_size=i$
fill rect sc*10,sc*5 to sc*20,sc*8
button "4" delete ! button "6" delete ! button "8" delete !
return