' Matches.ibas
{CREATORID "LDMA"}
{VERSION "3.4"}
{RESOURCEFILE "Matches.rsrc"}
{PARSER ON}

CONST X$="There are "
CONST Y$=" matches on the board."
CONST Z$="¶¶Do you want to be the first to remove matches ?"

BEGIN
GOSUB _SetupGame
N=LOADPREF(#1)
IF N=0 THEN GOSUB _InitGame
ELSE
   GOSUB _DrawMatches
   GOSUB _NbLeft
ENDIF
GOSUB _MainLoop
SAVEPREF #2,T
SAVEPREF #3,A
END

_SetupGame:
   S=SCREENMODES
   IF S<2 THEN SCREEN 0
   ELSE
      S=MAX(S,3)
      SCREEN S
   ENDIF
   S=COLOR(0)
   BUTTON #1,"1",16,145,36,12
   BUTTON #2,"2",60,145,36,12
   BUTTON #3,"3",104,145,36,12
   LABEL #4,"",50,115
   LABEL #5,"",35,125
   BUTTON #6,"$",149,1,10,10
   SETFONT 1
   BUTTON #7,"i",136,1,10,10
   SETFONT 0
   BUTTON #8,"Help",107,1,26,10
   BUTTON #9,"Options",66,1,38,10
   T=LOADPREF(#2)
   T=MAX(1,T)
   A=LOADPREF(#3)
   A=MAX(1,A)
RETURN

_ErasePlayer:
   COLOR S
   BOXFILLED 2,110 TO 31,139
RETURN

_DrawMatches:
   D=N-1
   FOR L=0 TO D
      X=20+12*(L-10*(L\10))
      Y=18+30*(L\10)
      IMAGE 3,X,Y
   NEXT
RETURN

_EraseMatches:
   D=N-1
   E=N-R
   FOR L=D DOWNTO E
      X=20+12*(L-10*(L\10))
      Y=18+30*(L\10)
      I=X+9
      J=Y+27
      IF A<3 THEN
         IF A=1 PLAYWAVE 1,1024,1
         FOR U=4 TO 8
            IMAGE U,X,Y
            V=TICKS
            REPEAT
               W=TICKS-V
            UNTIL W>20
            COLOR S
            BOXFILLED X,Y TO I,J
         NEXT
      ELSE
         COLOR S
         BOXFILLED X,Y TO I,J
      ENDIF
   NEXT
RETURN

_NbLeft:
   A$=STR$(N,0)+" match"
   IF N>1 LET A$=A$+"es"
   A$=A$+" left"
   UPDATELABEL #4,A$
RETURN

_NbRemoved:
   A$=A$+" remove "+STR$(R,0)+" match"
   IF R>1 LET A$=A$+"es"
   A$=A$+"..."
   UPDATELABEL #5,A$
   GOSUB _EraseMatches
   N=N-R
   U=T-1
   SLEEP U
   GOSUB _NbLeft
RETURN

_ComputerPlays:
   GOSUB _ErasePlayer : IMAGE 2,2,110
   M=(N-1) MOD 4
   IF M=0 THEN
      REPEAT : R=RND(3)+1 : UNTIL R<N
   ELSE R=M
   ENDIF
   A$="I" : GOSUB _NbRemoved
RETURN

_HumanPlays:
   GOSUB _ErasePlayer : IMAGE 1,2,110 
   UPDATELABEL #5,"Remove how many matches ?"
   REPEAT
      R=DOEVENTS
      IF R=6 LET R=31 : G=NOTICEBOX(2)
      IF R=7 LET R=31 : G=NOTICEBOX(1) : IF G=2 LET G=NOTICEBOX(2)
      IF R=8 THEN
         G=NOTICEBOX(5)+2
         IF G<>5 LET G=NOTICEBOX(G)
         R=31
      ENDIF
      IF R=9 THEN
         G=NOTICEBOX(6)
         IF G=1 THEN
            U=T+10
            T=NOTICEBOX(U)
         ELSE
            IF G=2 THEN
               U=A+20
               A=NOTICEBOX(U)
            ENDIF 
         ENDIF
         R=31
      ENDIF
      IF R=0 LET R=31
   UNTIL R<N
   IF R=-1 SAVEPREF #1,N : N=0
   IF R>0 LET A$="You" : GOSUB _NbRemoved	
RETURN

_InitGame:
   N=RND(29)+2
   UPDATELABEL #4,""
   UPDATELABEL #5,""
   GOSUB _DrawMatches
   GOSUB _NbLeft
   A$=X$+STR$(N,0)+Y$
   F=MESSAGEBOX(A$+Z$,3)
   IF F=2 GOSUB _ComputerPlays
RETURN

_PlayAgain:
   G=NOTICEBOX(7)
   IF G=2 THEN 
      N=0
      SAVEPREF #1,N
   ELSE
      GOSUB _ErasePlayer
      GOSUB _InitGame
   ENDIF
RETURN

_MainLoop:
   REPEAT
      IF N=1 THEN
         G=NOTICEBOX(8)
         GOSUB _PlayAgain
      ELSE
         GOSUB _HumanPlays
         IF N>0 THEN
            IF N=1 THEN
               G=NOTICEBOX(9)
               GOSUB _PlayAgain
            ELSE
               GOSUB _ComputerPlays
            ENDIF
         ENDIF
      ENDIF
   UNTIL N=0
RETURN