' ************************************************
' *                                              *
' *    NutHouse 9         Last rev. Sep 25, 1986 *
' * by:Don Moore                                 *
' *    P.O.Box 1405              AmigaBasic      *
' *    Coconut Grove, Fl      ie. Microsoft      *
' *                                              *
' ************************************************


' This program may be used with the keypad or mouse.
'
' Selected number and adjacent numbers change simultaneously.


GOSUB Initialize

                    
' Set up matrix number's and
' get new game parameters
               
Newgame:
     LOCATE 1,1
     COLOR 2,1
     PRINT PTAB(5);" All red to win   ";
        Total        = 0
          FOR Col    = 1 TO 3
            FOR Row  = 1 TO 3
              BlokColor(Row,Col) = INT(RND * 2)
              Total              = Total + BlokColor(Row,Col)
              Digit(Col,Row)     = Col + (3 - Row) * 3
            NEXT Row
          NEXT Col
            Choice   = 5     'initializes all 9 keys
        GOTO Display
      
Getnumber:
     WHILE Total   <> WinningTotal 
           Choice   = INSTR(" 123456789",INKEY$) -1
       IF  Choice   > 0 THEN Display
       IF  MOUSE(0) = 0 THEN Getnumber
       
GetMouseNumber:    
         x1   =     INT( (MOUSE(1) + 32) / 40)
         y1   = 3 - INT( (MOUSE(2) +  3) / 15)        
     IF MOUSE(0) <> 0 THEN GetMouseNumber     
       IF ((x1 > 0) AND (x1 < 4)) AND ((y1 > -1) AND (y1 < 3)) THEN
         Choice   = (( 3 * y1) +x1)
         GOTO Display
       END IF                          
     GOTO Getnumber
     
Display:       
      FOR Row   = (( Choice MOD 3) > 0 )+2 TO ((( Choice+2 ) MOD 3 ) = 0) +3
        FOR Col = ( Choice > 3 )+2 TO ( Choice > 6 ) + 3
           BlokColor(Row,Col) = (NOT BlokColor(Row,Col)) + 2   
           Newcolor           = BlokColor(Row,Col) + 2
           Total              = Total + BlokColor(Row,Col) * 2 -1
           HorPos             = Row * 40 -25
           VerPos             = Col * 16 -2
           LINE(HorPos,VerPos)-STEP(BlokWdth,BlokHgt),Newcolor,bf
           LOCATE Col * 2 +1, Row * 5 -2      
           COLOR BlokColor(Row,Col),Newcolor
           PRINT Digit(Row,Col);
        NEXT Col
      NEXT Row
    GOTO Getnumber
  WEND
  
  
  
' Win routine, i.e. Total red buttons = 9
     COLOR  3,1
     LOCATE 1,1
     PRINT PTAB(5);"You are a winner ";
       FOR i = 1 TO 7
        SOUND i*400+500 ,.5
       NEXT i
         FOR i = 1 TO 2000: NEXT i
     GOTO Newgame
     
     
Initialize:     
'Darken screen
     FOR j = 0 TO 3
       PALETTE j,0,0,0
     NEXT j
' Constants
     DEFINT a-z
     BlokWdth     = 26 
     BlokHgt      = 10
     WinningTotal = 9
     DigitColor   = 1
     RANDOMIZE TIMER
     
     WINDOW 1,"Nutty 9",(10,10)-(147,73),30    
     PALETTE 0,0,.4,.6
     PALETTE 1,1,1,1
     PALETTE 2,0,0,0
     PALETTE 3,.93,.2,0
           
                    
' Background frame
        LINE(0,0)-(137,63),1,bf
        LINE(1,8)-(136,62),2,bf
        LINE(7,11)-(130,59),1,bf
        LINE(4,8)-(135,8),0
        LINE(135,8)-(136,61),0,bf
        LINE(5,10)-(6,60),0,bf
        LINE-(132,60),0
       
    'Make keypad                 
         DIM b%(500)          
              GOSUB MakeKey         
              GET(10,12)-(47,26),b% 
                 FOR i= 0 TO 2
                   FOR j= 0 TO 2
                     PUT(j*40+10,i*16+12),b%,PSET
                   NEXT j
                 NEXT i
           RETURN  ' End of Initialize

MakeKey:
  LINE(10,12)-(47,26),2,bf
    LINE(12,12)-(45,12),0
    LINE-(45,25),0
      LINE(13,14)-(13,25),0
      LINE-(41,25),0
RETURN

