ColorCount=100
Colors=ArrDimension(ColorCount+1)
FlashColorMasks="255|255|0 255|32|32 0|255|0 0|128|255"
DarkColorMasks ="180|180|0 180|0|0 0|128|0 0|32|192"



DC_InitDialog=0 
DC_TimerTick=1
DC_ButtonPressed= 2
DC_RadioPressed=3
DC_CheckBoxPressed=4 
DC_SETTITLE=4
Style_Add=3
Style_Remove=4
Style_Invisible=1
SET_BACK_COLOR=11

StateWho=0     ; 0 Waiting for a GO   1 Computer displaying   2 user playing
S_Wait=0
S_Computer=1
S_User=2
MoveMax=10     ; Max number in sequence to remember
MoveTurn=1     ; How many in sequence for this turn
MoveCurrent=0  ; where in sequence are we on this turn

TimerTickTime=250  ; 1/4 sec timer tick

GameTimeout=8*1000/TimerTickTime     ; how long when expecting a user move to call a loss
GameTimeCurrent=0  ; current user timeout delay

TimerFlashState=0
T_Init=0
T_DazzleON=1
T_DazzleOFF=2
T_ON=3
T_OFF=4
TimerDazzleONFlashTicks=2  ; siz ticks per dazzle ON flash
TimerDazzleOFFFlashTicks=1  ; siz ticks per dazzle ON flash
TimerONFlashTicks=3  ; three ticks per ON flash
TimerOFFFlashTicks=1  ; one ticks per OFF flash
TimerFlashCounter=0

ReverseGame=@False
Randomize=@FALSE
howhard=1
howmany=5

#DefineSubroutine ResetGame()
    for xx=0 to ColorCount
       Colors[xx]=Random(3)+1
       ;Colors[xx]= (xx mod 4) + 1  ; predictable for testing
    next
    ;set all buttons dark
    for xx=1 to 4
       DialogControlSet(MDH,xx,SET_BACK_COLOR,ItemExtract(xx,DarkColorMasks," "))
    next
    ;set go button test to GO and green
    DialogControlSet(MDH,BGO,DC_SETTITLE,"GO")
    DialogControlSet(MDH,BGO,SET_BACK_COLOR,"0|255|0")
    StateWho=S_Wait
    MoveTurn=1 
    MoveCurrent=0
    GameTimeCurrent=0
    TimerFlashState=T_INIT
;    DialogControlSet(MDH,SCount,DC_SETTITLE,"")
#EndSubroutine

#DEFINESUBROUTINE MemProc(MDH,MDM,MID,R4,R5)
   switch MDM

          case DC_InitDialog   ; Init_Dialog  (case number 0 subject to change on implementation)
               DialogProcOptions(MDH,DC_ButtonPressed,1)   ; enable buttonclicked messages
               DialogProcOptions(MDH,DC_RadioPressed,1)   ; enable radioclicked messages
               DialogProcOptions(MDH,DC_CheckboxPressed,1)   ; enable checkboxclicked messages
               DialogProcOptions(MDH,DC_TimerTick,TimerTickTime)
               ResetGame()
               return (-1)
   
          case DC_TimerTick
               switch StateWho
                  case S_WAIT
                     return(-1)
                  case S_Computer
                     switch TimerFlashState
                          case T_Init
                             MoveCurrent=0
                             DialogControlSet(MDH,1,SET_BACK_COLOR,ItemExtract(1,FlashColorMasks," "))
                             DialogControlSet(MDH,2,SET_BACK_COLOR,ItemExtract(2,FlashColorMasks," "))
                             DialogControlSet(MDH,3,SET_BACK_COLOR,ItemExtract(3,FlashColorMasks," "))
                             DialogControlSet(MDH,4,SET_BACK_COLOR,ItemExtract(4,FlashColorMasks," "))
                             TimerFlashState=T_DazzleON
                             TimerFlashCounter=0
;                             DialogControlSet(MDH,SCount,DC_SETTITLE,"")
                             return(-1)
                          case T_DazzleON
                             TimerFlashCounter=TimerFlashCounter+1
                             if TimerFlashCounter>=TimerDAZZLEONFlashTicks
                                TimerFlashState=T_DazzleOFF
                                TimerFlashCounter=0
                                DialogControlSet(MDH,1,SET_BACK_COLOR,ItemExtract(1,DarkColorMasks," "))
                                DialogControlSet(MDH,2,SET_BACK_COLOR,ItemExtract(2,DarkColorMasks," "))
                                DialogControlSet(MDH,3,SET_BACK_COLOR,ItemExtract(3,DarkColorMasks," "))
                                DialogControlSet(MDH,4,SET_BACK_COLOR,ItemExtract(4,DarkColorMasks," "))
                             endif
                             return(-1)
                          case T_DazzleOff
                             TimerFlashCounter=TimerFlashCounter+1
                             if TimerFlashCounter>=TimerDAZZLEOFFFlashTicks
                                DialogControlSet(MDH,Colors[MoveCurrent],SET_BACK_COLOR,ItemExtract(Colors[MoveCurrent],FlashColorMasks," "))
;                                DialogControlSet(MDH,SCount,DC_SETTITLE,MoveCurrent+1)
                                TimerFlashState=T_ON
                                TimerFlashCounter=0
                             endif
                             return(-1)
                          case T_ON
                             TimerFlashCounter=TimerFlashCounter+1
                             if TimerFlashCounter>=TimerONFlashTicks
                                TimerFlashState=T_OFF
                                TimerFlashCounter=0
                                DialogControlSet(MDH,Colors[MoveCurrent],SET_BACK_COLOR,ItemExtract(Colors[MoveCurrent],DarkColorMasks," "))
                             endif
                             return(-1)
                          case T_OFF
                             TimerFlashCounter=TimerFlashCounter+1
                             if TimerFlashCounter>=TimerOFFFlashTicks
                                MoveCurrent=MoveCurrent+1
                                if MoveCurrent==MoveTurn
                                   ;Its the users turn
                                   StateWho=S_USER
                                   TimerFlashState=T_Init
                                   if ReverseGame==@FALSE
                                      MoveCurrent=0
                                   else 
                                      MoveCurrent=MoveTurn-1
                                   endif
;                                   DialogControlSet(MDH,SCount,DC_SETTITLE,strcat(MoveCurrent+1,"?"))
                                else
                                   DialogControlSet(MDH,Colors[MoveCurrent],SET_BACK_COLOR,ItemExtract(Colors[MoveCurrent],FlashColorMasks," "))
;                                   DialogControlSet(MDH,SCount,DC_SETTITLE,MoveCurrent+1)
                                   TimerFlashState=T_ON
                                   TimerFlashCounter=0
                                endif
                             endif
                             return(-1)
                     endswitch
                     return(-1)
                  case S_User
                     GameTimeCurrent=GameTimeCurrent+1
                     if GameTimeCurrent>=GameTimeout
                        Message("Ooop","Took too long")
                        ResetGame()
                     endif
                     return(-1)
               endswitch
               return(-1)

          case DC_RadioPressed
               switch MID
                   case Reasy
                      howmany=5
                      break
                   case RMedium
                      howmany=10
                      break
                   case RHard
                      howmany=15
                      break
                   case RImpossible
                      howmany=100
                      break
               endswitch
               ResetGame()
               return(-1)

          case DC_CheckboxPressed
               switch MID
                  case CReverse
                    ReverseGame=!ReverseGame
                    break
                  case CRandom
                    Randomize=!Randomize
                    break
                endswitch
               ResetGame()
               Return(-1)

          case DC_ButtonPressed
              switch MID
                 case BEXIT
                      return (-1)
                 case BGO
                      if StateWho==0  ; waiting for a go
                          DialogControlSet(MDH,BGO,DC_SETTITLE,"STOP")
                          DialogControlSet(MDH,BGO,SET_BACK_COLOR,"255|0|0")
                          StateWho=S_COMPUTER
                          TimerFlashState=T_INIT
                      else
                         ResetGame()
                      endif
                      return(-2)

                 case MID  ; the buttons
                      if StateWho==S_USER
                         GameTimeCurrent=0
                         ;Flash Button
                         ;was that the correct button?
                         if MID==Colors[MoveCurrent]
                            ;yes
                            DialogControlSet(MDH,MID,SET_BACK_COLOR,ItemExtract(MID,FlashColorMasks," "))
                            TimeDelay(0.25)
                            DialogControlSet(MDH,MID,SET_BACK_COLOR,ItemExtract(MID,DarkColorMasks," "))
                            if ReverseGame==@FALSE
                                MoveCurrent=MoveCurrent+1
                                if MoveCurrent==MoveTurn
                                   ; and they made it thru the turn
                                   ;did they win yet
                                   if MoveTurn==howmany
                                      ;TimeDelay(0.5)
                                      Message("Ta-Da","You did it!")
                                      ResetGame()
                                      return(-2)
                                   endif
                                   
                                   MoveTurn=MoveTurn+1
                                   StateWho=S_Computer
                                   TimerFlashState=T_Init
                                   TimeDelay(1)
                    ;  DialogControlSet(MDH,SCount,DC_SETTITLE,MoveCurrent+1)
if Randomize
    for xx=0 to ColorCount
       Colors[xx]=Random(3)+1
       ;Colors[xx]= (xx mod 4) + 1  ; predictable for testing
    next
endif
                                   break ;return(-2)
                                endif
                            else
                                if MoveCurrent==0
                                   ; and they made it thru the turn
                                   ;did they win yet
                                   if MoveTurn==howmany
                                      ;TimeDelay(0.5)
                                      Message("Ta-Da","You did it!")
                                      ResetGame()
                                      return(-2)
                                   endif
                                   MoveTurn=MoveTurn+1
                                   StateWho=S_Computer
                                   TimerFlashState=T_Init
                                   TimeDelay(1)
if Randomize
    for xx=0 to ColorCount
       Colors[xx]=Random(3)+1
       ;Colors[xx]= (xx mod 4) + 1  ; predictable for testing
    next
endif
                                   break ;return(-2)
                                endif
                                MoveCurrent=MoveCurrent-1
                    ;  DialogControlSet(MDH,SCount,DC_SETTITLE,MoveCurrent+1)
                            endif

                         else
                            ;no bad move
                            Message("Ooop","Bad Move")
                            ResetGame()
                         endif
                      endif
;                      DialogControlSet(MDH,SCount,DC_SETTITLE,strcat(MoveCurrent+1,"?"))
                      return(-2)



              endswitch
               return(-2)  ;Don't Exit

   endswitch ; MyDialogMessage
   Return(-1) ; Do default processing#EndFunction
#ENDSUBROUTINE


B1=001
B2=002
B3=003
B4=004
BGO=005
BEXIT=006

Reasy=007
Rmedium=008
Rhard=009
RImpossible=010
;SCount=013
CReverse=012
CRandom=013

I1="Memory Mimic"
I2="In this game you simply repeat the patterns the computer shows you."
I3='For a variation, click the "Reverse" box and then you will have to enter the patterns in reverse order.'
I4='Or click the "Random" button to change the pattern each turn.'
Intro=Strcat(I1,@crlf,@crlf,I2,@crlf,@crlf,i3,@crlf,@crlf,i4)



MemFormat=`WWWDLGED,6.1`

MemCaption=`Memory Mimic`
MemX=030
MemY=079
MemWidth=259
MemHeight=159
MemNumControls=013
MemProcedure=`MemProc`
MemFont=`DEFAULT`
MemTextColor=`DEFAULT`
MemBackground=`DEFAULT,255|128|0`

Mem001=`046,009,030,030,PUSHBUTTON,DEFAULT,"",2,10,DEFAULT,DEFAULT,DEFAULT,"224|224|0"`
Mem002=`012,042,030,030,PUSHBUTTON,DEFAULT,"",3,11,DEFAULT,DEFAULT,DEFAULT,"224|0|0"`
Mem003=`081,042,030,030,PUSHBUTTON,DEFAULT,"",4,12,DEFAULT,DEFAULT,DEFAULT,"0|224|0"`
Mem004=`046,076,030,030,PUSHBUTTON,DEFAULT,"",5,13,DEFAULT,DEFAULT,DEFAULT,"0|0|224"`
Mem005=`153,132,046,024,PUSHBUTTON,DEFAULT,"Go",1,1,DEFAULT,"Microsoft Sans Serif|9421|70|34","0|0|0","0|255|0"`
Mem006=`228,148,027,008,PUSHBUTTON,DEFAULT,"Exit",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Mem007=`004,113,070,008,RADIOBUTTON,HowHard,"5-Easy",1,3,DEFAULT,"Microsoft Sans Serif|6963|70|34","0|0|0",DEFAULT`
Mem008=`004,123,070,008,RADIOBUTTON,HowHard,"10-Medium",2,4,DEFAULT,"Microsoft Sans Serif|6963|70|34","0|0|0",DEFAULT`
Mem009=`004,133,070,008,RADIOBUTTON,HowHard,"15-Hard",3,5,DEFAULT,"Microsoft Sans Serif|6963|70|34","0|0|0",DEFAULT`
Mem010=`004,143,070,008,RADIOBUTTON,HowHard,"100-Impossible",4,6,DEFAULT,"Microsoft Sans Serif|6963|70|34","0|0|0",DEFAULT`
Mem011=`127,006,123,113,VARYTEXT,Intro," Click around a lot",DEFAULT,9,DEFAULT,"Microsoft Sans Serif|6963|70|34","0|0|128","255|128|0"`
Mem012=`084,133,046,010,CHECKBOX,ReverseGame,"Reverse",1,7,DEFAULT,"Microsoft Sans Serif|6963|70|34","0|0|0",DEFAULT`
Mem013=`084,143,046,010,CHECKBOX,Randomize,"Random",1,8,DEFAULT,"Microsoft Sans Serif|6963|70|34","0|0|0",DEFAULT`

ButtonPushed=Dialog("Mem")






