/ / T i C - T A C - M A X 
 
 / / V e r s i o n   1 . 0 
 
 / / 1 0 / 3 1 / 2 0 1 6 
 
 / / B y   D a n i e l   M a u p i n 
 
 / / H P   P r i m e   P r o g r a m m i n g   L a n g u a g e 
 
 / / 
 
 / / T I C - T A C - T O E   g a m e   w i t h   m i n i M a x   A I   a l g o r i t h m . 
 
 
 
 / / d e f i n e   s u b r o u t i n e s . 
 
 d r a w B o a r d ( ) ; 
 
 d r a w W i n n e r ( ) ; 
 
 m o v e I n d i c a t o r ( ) ; 
 
 c h e c k F o r W i n ( ) ; 
 
 d i s p l a y W i n ( ) ; 
 
 p l a y G a m e ( ) ; 
 
 n e x t P l a y e r ( ) ; 
 
 m i n i M a x ( ) ; 
 
 c o m p u t e r M o v e ( ) ; 
 
 s e l e c t D e p t h ( ) ; 
 
 r a n d D e p t h ( ) ; 
 
 n u m P l a y e r ( ) ; 
 
 s h o w S c o r e s ( ) ; 
 
 s e l e c t A n i m a t e ( ) ; 
 
 
 
 
 
 / / d e f i n e   w i n n i n g   b o a r d   p o s i t i o n s . 
 
 w i n C o m b o : = { [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] , [ 1 , 4 , 7 ] , [ 2 , 5 , 8 ] , [ 3 , 6 , 9 ] , [ 1 , 5 , 9 ] , [ 3 , 5 , 7 ] } ; 
 
 / / d e f i n e   t o u c h   s c r e e n   c o o r d i n a t e s   f o r   b o a r d . 
 
 g r i d L o c : = { [ 2 0 , 7 0 , 2 0 , 7 0 ] , [ 7 0 , 1 2 0 , 2 0 , 7 0 ] , [ 1 2 0 , 1 7 0 , 2 0 , 7 0 ] , 
 
   [ 2 0 , 7 0 , 7 0 , 1 2 0 ] , [ 7 0 , 1 2 0 , 7 0 , 1 2 0 ] , [ 1 2 0 , 1 7 0 , 7 0 , 1 2 0 ] , 
 
   [ 2 0 , 7 0 , 1 2 0 , 1 7 0 ] , [ 7 0 , 1 2 0 , 1 2 0 , 1 7 0 ] , [ 1 2 0 , 1 7 0 , 1 2 0 , 1 7 0 ] } ; 
 
 / / d e f i n e   g r i d   l o c a t i o n s   t o   d r a w   X s   a n d   O s . 
 
 x C o : = { [ 2 4 , 2 4 , 6 6 , 6 6 , 6 6 , 2 4 , 2 4 , 6 6 ] , [ 7 4 , 2 4 , 1 1 6 , 6 6 , 1 1 6 , 2 4 , 7 4 , 6 6 ] , 
 
   [ 1 2 4 , 2 4 , 1 6 6 , 6 6 , 1 6 6 , 2 4 , 1 2 4 , 6 6 ] , [ 2 4 , 7 4 , 6 6 , 1 1 6 , 6 6 , 7 4 , 2 4 , 1 1 6 ] , 
 
   [ 7 4 , 7 4 , 1 1 6 , 1 1 6 , 1 1 6 , 7 4 , 7 4 , 1 1 6 ] , [ 1 2 4 , 7 4 , 1 6 6 , 1 1 6 , 1 6 6 , 7 4 , 1 2 4 , 1 1 6 ] , 
 
   [ 2 4 , 1 2 4 , 6 6 , 1 6 6 , 6 6 , 1 2 4 , 2 4 , 1 6 6 ] , [ 7 4 , 1 2 3 , 1 1 6 , 1 6 5 , 1 1 6 , 1 2 3 , 7 4 , 1 6 5 ] , 
 
   [ 1 2 4 , 1 2 4 , 1 6 6 , 1 6 6 , 1 6 6 , 1 2 4 , 1 2 4 , 1 6 6 ] } ; 
 
 o C o : = { [ 4 5 , 4 5 ] , [ 9 5 , 4 5 ] , [ 1 4 5 , 4 5 ] , [ 4 5 , 9 5 ] , [ 9 5 , 9 5 ] , [ 1 4 5 , 9 5 ] , 
 
   [ 4 5 , 1 4 5 ] , [ 9 5 , 1 4 5 ] , [ 1 4 5 , 1 4 5 ] } ; 
 
 r a n d C o r n e r = { 1 , 3 , 7 , 9 } ;   / / d e f i n e   c o r n e r   s q u a r e s   f o r   c o m p u t e r ' s   s e c o n d   m o v e . 
 
 s c o r e = { 0 , 0 , 0 } ;   / / s c o r e   f o r   { P 1 , P 2 , C a t } . 
 
 b o a r d : = { } ;   / / g a m e   b o a r d ,   0   f o r   e m p t y   s q u a r e , - 1   f o r   X , 1   f o r   O , 
 
 w i n M a t r i x : = { } ;   / / l i s t   c o n t a i n i n g   w i n n i n g   l i n e   o n   b o a r d . 
 
 P T = - 1 ;   / / p l a y e r   t u r n ,   - 1   f o r   P 1 ,   1   f o r   P 2 . 
 
 T N ;   / / t u r n   n u m b e r . 
 
 m a x D e p t h ;   / / m a x i m u m   d e p t h   t o   s e a r c h .   U s e d   t o   l i m i t   A I   s t r e n g t h . 
 
 p l a y e r s : = 1 ;   / / n u m b e r   o f   p l a y e r s ,   1 = p l a y   c o m p u t e r ,   2 = p l a y   h u m a n . 
 
 d e l a y : = 0 ;   / / s l o w s   d o w n   A I   ( u s e d   f o r   t e s t i n g   o n l y   -   s h o u l d   n o r m a l l y   b e   0 ) 
 
 a n i m a t e : = 1 ;   / / s h o w   a n i m a t i o n s ,   1   f o r   y e s ,   - 1   f o r   n o . 
 
 l e v e l : = 1 ;   / / L e v e l ,   1 = e a s y , 2 = f a i r , 3 = h a r d . 
 
 
 
 E X P O R T   t i c T a c T o e ( ) 
 
   B E G I N     
 
     W H I L E   1 = 1   D O   / / l o o p   u n t i l   p l a y e r   e x i t s   g a m e . 
 
     b o a r d : = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ;   / / i n i t   b o a r d   t o   e m p t y . 
 
     T N : = 1 ;     
 
     p l a y G a m e ( ) ; 
 
     E N D ; 
 
   E N D ; 
 
 
 
 p l a y G a m e ( ) 
 
   B E G I N 
 
     I F   a n i m a t e = = 1   T H E N   
 
       D I M G R O B _ P ( G 1 , 3 2 0 , 2 4 0 ) ;     
 
       B L I T _ P ( G 1 , 0 , 0 , 3 2 0 , 2 4 0 , G 0 ) ; 
 
       F O R   Z   F R O M   2   T O   1 6 0   S T E P   8   D O 
 
       B L I T _ P ( G 0 , 0 , 0 , 3 2 0 , 2 4 0 , G 1 , 0 + Z , 0 + Z * . 7 5 , 3 2 0 - Z , 2 4 0 - Z * . 7 5 ) ; 
 
       E N D ; 
 
     E N D ; 
 
     
 
     L O C A L   t o u c h ; 
 
     L O C A L   X , Y , J , L 1 , T S ; 
 
         
 
     R E C T ( ) ;   / / c l e a r   s c r e e n . 
 
     R E C T _ P ( 0 , 0 , 3 1 9 , 2 3 9 , # 0 0 0 0 0 0 h ) ;   / / d r a w   g r i d . 
 
     R E C T _ P ( 6 9 , 2 0 , 7 1 , 1 7 0 , # F F B F 0 0 h ) ; 
 
     R E C T _ P ( 1 1 9 , 2 0 , 1 2 1 , 1 7 0 , # F F B F 0 0 h ) ; 
 
     R E C T _ P ( 2 0 , 6 9 , 1 7 0 , 7 1 , # F F B F 0 0 h ) ; 
 
     R E C T _ P ( 2 0 , 1 1 9 , 1 7 0 , 1 2 1 , # F F B F 0 0 h ) ; 
 
 
 
     F O R   I   F R O M   0   T O   2   S T E P   1   D O 
 
       T E X T O U T _ P ( " T I C " , 4 9 + I , 1 8 0 + I , 3 , 6 7 1 6 8 0 - ( I * 9 0 0 0 ) ) ; 
 
       T E X T O U T _ P ( " T A C " , 7 9 + I , 1 8 0 + I , 3 , 6 7 1 6 8 0 - ( I * 9 0 0 0 ) ) ; 
 
       T E X T O U T _ P ( " M A X " , 1 1 4 + I , 1 8 0 + I , 3 , 6 7 1 6 8 0 - ( I * 9 0 0 0 ) ) ; 
 
       T E X T O U T _ P ( " B y   D a n i e l   M a u p i n " , 2 0 5 + I , 2 0 5 + I , 3 , 1 1 3 0 0 + ( I * 2 0 0 0 0 ) ) ; 
 
     E N D ; 
 
     
 
     R E C T _ P ( 2 8 8 , 0 , 3 1 9 , 1 5 , # F F 1 E 1 E h ) ;   / / d r a w   e x i t   c o n t r o l 
 
     T E X T O U T _ P ( " E X I T " , 2 9 0 , 0 , 3 , # 0 0 0 0 0 0 h ) ; 
 
     s e l e c t A n i m a t e ( a n i m a t e ) ;   / / d r a w   a n i m a t i o n   c o n t r o l 
 
     
 
     R E C T _ P ( 2 6 0 , 5 0 , 3 1 9 , 6 5 , # 3 2 8 1 F F h ) ; 
 
     T E X T O U T _ P ( " < < R E S E T " , 2 6 2 , 5 0 , 3 , # 0 0 0 0 0 0 h ) ; 
 
     T E X T O U T _ P ( " D I F F I C U L T Y :   " , 5 0 , 0 , 3 , # F F B F 0 0 h ) ; 
 
 
 
     s h o w S c o r e s ( ) ;   / / d i s p l a y   s c o r e s 
 
     s e l e c t D e p t h ( l e v e l ) ;   / / s e t   m i n i M a x   d e p t h   o f   s e a r c h   b a s e d   o n   l e v e l 
 
     n u m P l a y e r ( p l a y e r s ) ;   / / s e t   n u m b e r   o f   p l a y e r s 
 
     m o v e I n d i c a t o r ( ) ;   / / h i g h l i g h t   c u r r e n t   p l a y e r 
 
     
 
     W H I L E   T N < 1 0   D O   / / l o o p   u n t i l   l a s t   t u r n . 
 
       W H I L E   P T = = - 1   O R   p l a y e r s = = 2   D O   / / l o o p   w a i t i n g   f o r   p l a y e r   i n p u t   u n l e s s   e s c   k e y   i s   p r e s s e d .         
 
         t o u c h : = m o u s e ( ) ; 
 
         I F   I S K E Y D O W N ( 4 ) = = 1   t h e n   K I L L ;   E N D ;   / / e x i t   i f   e s c a p e   k e y   i s   p r e s s e d . 
 
         I F   s i z e ( t o u c h ( 1 ) )   T H E N           
 
           I F   t o u c h ( 1 , 5 ) = = 0   T H E N 
 
 
 
             X : = B !R ( t o u c h ( 1 , 1 ) ) ;   / / X , Y   c o o r d i n a t e s   o f   t o u c h   s c r e e n   i n p u t . 
 
             Y : = B !R ( t o u c h ( 1 , 2 ) ) ; 
 
 
 
             I F   ( 2 8 7 < X < 3 2 0   A N D   0 < Y < 1 6 )   T H E N   K I L L ;   E N D ;   / / e x i t   b u t t o n   p r e s s e d . 
 
             I F   1 3 4 < X < 1 7 6   A N D   0 < Y < 1 6   T H E N   s e l e c t D e p t h ( 1 ) ;   E N D ;   / / d i f f i c u l t y   b u t t o n s . 
 
             I F   1 7 9 < X < 2 2 1   A N D   0 < Y < 1 6   T H E N   s e l e c t D e p t h ( 2 ) ;   E N D ; 
 
             I F   2 2 4 < X < 2 6 6   A N D   0 < Y < 1 6   T H E N   s e l e c t D e p t h ( 3 ) ;   E N D ; 
 
             I F   0 < X < 8 1   A N D   2 0 4 < Y < 2 2 1   T H E N   n u m P l a y e r ( 1 ) ;   E N D ;   / / 1 - 2   p l a y e r   b u t t o n s 
 
             I F   9 9 < X < 1 7 9   A N D   2 0 4 < Y < 2 2 1   T H E N   n u m P l a y e r ( 2 ) ;   E N D ; 
 
             I F   2 6 1 < X < 3 2 0   A N D   4 9 < Y < 6 6   T H E N   s c o r e : = { 0 , 0 , 0 } ;   s h o w S c o r e s ( ) ;   W A I T ( . 2 ) ;   E N D ;   / / r e s e t   s c o r e   b u t t o n 
 
             I F   0 < X < 3 0   A N D   0 < Y < 1 6   T H E N   s e l e c t A n i m a t e ( a n i m a t e * - 1 ) ;   E N D ;   / / t o g g l e   a n i m a t i o n   b u t t o n 
 
             
 
             F O R   Z   F R O M   1   T O   9   S T E P   1   D O   / / w a s   t o u c h   o n   a   s q u a r e ?     
 
               I F   g r i d L o c ( Z , 1 ) < X < g r i d L o c ( Z , 2 )   A N D   g r i d L o c ( Z , 3 ) < Y < g r i d L o c ( Z , 4 )   A N D   b o a r d ( Z ) = = 0   T H E N 
 
                 I F   P T = = - 1   T H E N   / / i f   p l a y e r   1   t h e n   d r a w   X , 
 
                   b o a r d ( Z ) : = - 1 ;   d r a w B o a r d ( b o a r d ) ;   n e x t P l a y e r ( b o a r d ) ;   B R E A K ; 
 
                 E L S E   / / o t h e r w i s e   d r a w   O . 
 
                   b o a r d ( Z ) : = 1 ;   d r a w B o a r d ( b o a r d ) ;   n e x t P l a y e r ( b o a r d ) ;   B R E A K ; 
 
                 E N D ; 
 
               E N D ; 
 
             E N D ;             
 
             I F   d i s p l a y W i n ( b o a r d )   T H E N   R E T U R N ;   E N D ;   / / a f t e r   p l a y e r   1   m o v e ,   c h e c k   f o r   w i n . 
 
           E N D ; 
 
         E N D ; 
 
       E N D ; 
 
       I F   p l a y e r s = = 1   T H E N   / / i f   s i n g l e   p l a y e r ,   m a k e   c o m p u t e r   m o v e . 
 
       b o a r d ( c o m p u t e r M o v e ( b o a r d ) ) : = 1 ; 
 
       d r a w B o a r d ( b o a r d ) ; 
 
       n e x t P l a y e r ( b o a r d ) ; 
 
       R E C T _ P ( 1 8 5 , 1 2 2 , 3 2 0 , 1 3 6 , # 0 0 0 0 0 0 h ) ;   / / m o v e   o v e r ,   c l e a r   " T H I N K I N G "   m e s s a g e .     
 
       E N D ; 
 
       I F   d i s p l a y W i n ( b o a r d )   T H E N   R E T U R N ;   E N D ;   / / a f t e r   p l a y e r   2   m o v e ,   c h e c k   f o r   w i n . 
 
     E N D ; 
 
   E N D ; 
 
 
 
 d r a w B o a r d ( b o a r d )   / / d r a w   X s   a n d   O s   f o r   { b o a r d } . 
 
   B E G I N     
 
     F O R   I   F R O M   1   T O   9   S T E P   1   D O 
 
       I F   b o a r d ( I ) = = - 1   T H E N 
 
         F O R   Z   F R O M   - 1   T O   1   S T E P   1   D O 
 
           L I N E _ P ( x C o ( I , 1 ) , ( x C o ( I , 2 ) ) + Z , x C o ( I , 3 ) , ( x C o ( I , 4 ) ) + Z , # F F 0 0 0 0 h ) ; 
 
           L I N E _ P ( x C o ( I , 5 ) , ( x C o ( I , 6 ) ) + Z , x C o ( I , 7 ) , ( x C o ( I , 8 ) ) + Z , # F F 0 0 0 0 h ) ;           
 
         E N D ;         
 
       E N D ; 
 
       I F   b o a r d ( I ) = = 1   T H E N 
 
         F O R   Z   F R O M   - 1   T O   1   S T E P   1   D O 
 
           A R C _ P ( o C o ( I , 1 ) , o C o ( I , 2 ) , 2 0 + Z , 3 6 0 , 1 , # 0 0 C 2 0 E h ) ; 
 
         E N D ;         
 
       E N D ;         
 
     E N D ; 
 
     R E T U R N ; 
 
   E N D ; 
 
   
 
   d r a w W i n n e r ( w i n M a t r i x , w i n n e r )   / / i f   w i n n e r ,   h i g h l i g h t   w i n n i n g   r o w . 
 
   B E G I N 
 
     F O R   J   F R O M   1   T O   3   S T E P   1   D O   
 
       F O R   I   F R O M   1   T O   9   S T E P   1   D O 
 
         I F   I = = w i n M a t r i x ( J )   A N D   w i n n e r = = - 1   T H E N   / / i f   X   w o n ,   h i g h l i g h t   X s , 
 
           F O R   Z   F R O M   - 1   T O   1   S T E P   1   D O 
 
             L I N E _ P ( x C o ( I , 1 ) , ( x C o ( I , 2 ) ) + Z , x C o ( I , 3 ) , ( x C o ( I , 4 ) ) + Z , # A 0 F 7 F F h ) ; 
 
             L I N E _ P ( x C o ( I , 5 ) , ( x C o ( I , 6 ) ) + Z , x C o ( I , 7 ) , ( x C o ( I , 8 ) ) + Z , # A 0 F 7 F F h ) ;           
 
           E N D ;         
 
         E N D ; 
 
         I F   I = = w i n M a t r i x ( J )   A N D   w i n n e r = = 1   T H E N   / / i f   O   w o n ,   h i g h l i g h t   O s . 
 
           F O R   Z   F R O M   - 1   T O   1   S T E P   1   D O 
 
             A R C _ P ( o C o ( I , 1 ) , o C o ( I , 2 ) , 2 0 + Z , 3 6 0 , 1 , # A 0 F 7 F F ) ; 
 
           E N D ;         
 
         E N D ;         
 
       E N D ; 
 
     E N D ; 
 
     R E T U R N ; 
 
   E N D ; 
 
   
 
 s h o w S c o r e s ( ) 
 
 / / s h o w s   p l a y e r   s c o r e s   a n d   h i g h l i g h t s   c u r r e n t   p l a y e r   t o   m o v e . 
 
   B E G I N 
 
     R E C T _ P ( 2 3 5 , 6 5 , 2 6 0 , 1 1 0 , # 0 0 0 0 0 0 h ) ; 
 
     T E X T O U T _ P ( " S C O R E S : " , 1 9 5 , 5 0 , 3 , # 3 2 8 1 F F h ) ; 
 
     I F   P T = = - 1   T H E N 
 
       T E X T O U T _ P ( " P 1 ( X s ) :   " + s c o r e ( 1 ) , 1 9 5 , 6 5 , 3 , # A 0 F 7 F F h ) ; 
 
       T E X T O U T _ P ( " P 2 ( 0 s ) :   " + s c o r e ( 2 ) , 1 9 5 , 8 0 , 3 , # 3 2 B E F F h ) ; 
 
     E L S E 
 
       T E X T O U T _ P ( " P 1 ( X s ) :   " + s c o r e ( 1 ) , 1 9 5 , 6 5 , 3 , # 3 2 B E F F h ) ; 
 
       T E X T O U T _ P ( " P 2 ( 0 s ) :   " + s c o r e ( 2 ) , 1 9 5 , 8 0 , 3 , # A 0 F 7 F F h ) ; 
 
     E N D ; 
 
       T E X T O U T _ P ( " C a t :             " + s c o r e ( 3 ) , 1 9 5 , 9 5 , 3 , # 3 2 B E F F h ) ;       
 
     R E T U R N ; 
 
   E N D ; 
 
   
 
 m o v e I n d i c a t o r ( ) 
 
 / / d r a w s   m o v e   i n d i c a t o r . 
 
   B E G I N 
 
     R E C T _ P ( 1 8 7 , 6 5 , 1 9 4 , 9 5 , # 0 0 0 0 0 0 h ) ; 
 
     I F   P T = = - 1   T H E N   T E X T O U T _ P ( " %" , 1 8 7 , 6 5 , 3 , # A 0 F 7 F F h ) ;   E N D ; 
 
     I F   P T = = 1   T H E N   T E X T O U T _ P ( " %" , 1 8 7 , 8 0 , 3 , # A 0 F 7 F F h ) ;   E N D ; 
 
     s h o w S c o r e s ( ) ; 
 
     R E T U R N ; 
 
   E N D ; 
 
   
 
 c h e c k F o r W i n ( b o a r d ) 
 
 / / d e t e r m i n e s   i f   a   p l a y e r   h a s   w o n ,   r e t u r n s   0   o t h e r w i s e . 
 
   B E G I N 
 
     F O R   I   F R O M   1   T O   8   D O 
 
       I F   b o a r d ( w i n C o m b o ( I , 1 ) ) = = - 1   A N D   b o a r d ( w i n C o m b o ( I , 2 ) ) = = - 1   A N D   b o a r d ( w i n C o m b o ( I , 3 ) ) = = - 1   T H E N 
 
         w i n M a t r i x : = w i n C o m b o ( I ) ;   / / a l l o w s   d r a w W i n n e r ( )   t o   h i g h l i g h t   w i n n i n g   l i n e   f o r   X . 
 
         R E T U R N   - 1 ; 
 
       E N D ; 
 
       I F   b o a r d ( w i n C o m b o ( I , 1 ) ) = = 1   A N D   b o a r d ( w i n C o m b o ( I , 2 ) ) = = 1   A N D   b o a r d ( w i n C o m b o ( I , 3 ) ) = = 1   T H E N 
 
         w i n M a t r i x : = w i n C o m b o ( I ) ;   / / a l l o w s   d r a w W i n n e r ( )   t o   h i g h l i g h t   w i n n i n g   l i n e   f o r   O . 
 
         R E T U R N   1 ; 
 
       E N D ; 
 
     E N D ; 
 
     R E T U R N   0 ;     
 
   E N D ; 
 
 
 
 d i s p l a y W i n ( b o a r d ) 
 
 / / c h e c k   f o r   w i n n e r   o r   d r a w   a n d   d i s p l a y   m e s s a g e . 
 
 / / r e t u r n s   1   f o r   w i n n e r   o r   d r a w ,   r e t u r n s   0   o t h e r w i s e . 
 
   B E G I N 
 
     I F   c h e c k F o r W i n ( b o a r d ) = = - 1   T H E N 
 
       T E X T O U T _ P ( " P l a y e r   1   w i n s " , 1 9 5 , 3 0 , 4 , # E C F F B C h ) ; 
 
       d r a w W i n n e r ( w i n M a t r i x , - 1 ) ; 
 
       s c o r e ( 1 ) : = s c o r e ( 1 ) + 1 ; 
 
       W A I T ( 3 ) ; 
 
       R E T U R N   1 ; 
 
     E N D ; 
 
     I F   c h e c k F o r W i n ( b o a r d ) = = 1   T H E N 
 
       T E X T O U T _ P ( " P l a y e r   2   w i n s " , 1 9 5 , 3 0 , 4 , # E C F F B C h ) ; 
 
       d r a w W i n n e r ( w i n M a t r i x , 1 ) ; 
 
       s c o r e ( 2 ) : = s c o r e ( 2 ) + 1 ; 
 
       W A I T ( 3 ) ; 
 
       R E T U R N   1 ; 
 
     E N D ;         
 
     I F   T N > 9   A N D   c h e c k F o r W i n ( b o a r d ) = = 0   T H E N 
 
       T E X T O U T _ P ( " D r a w " , 1 9 5 , 3 0 , 4 , # E C F F B C h ) ; 
 
       s c o r e ( 3 ) : = s c o r e ( 3 ) + 1 ; 
 
       W A I T ( 3 ) ; 
 
       R E T U R N   1 ; 
 
     E N D ; 
 
     R E T U R N   0 ; 
 
   E N D ; 
 
   
 
 n e x t P l a y e r ( b o a r d ) 
 
 / / a l t e r n a t e s   p l a y e r   t u r n . 
 
   B E G I N     
 
     I F   P T = = - 1   T H E N   P T : = 1 ; 
 
       E L S E   P T : = - 1 ; 
 
     E N D ;     
 
     T N : = T N + 1 ; 
 
     m o v e I n d i c a t o r ( ) ; 
 
     R E T U R N ; 
 
   E N D ; 
 
   
 
 n u m P l a y e r ( p l y r ) 
 
 / / s e t   n u m b e r   o f   p l a y e r s .   1   p l a y e r   a l l o w s   A I   a l g o r i t h m   t o   c o m p u t e   O s   m o v e s . 
 
   B E G I N 
 
     p l a y e r s : = p l y r ; 
 
     I F   p l y r = = 1   T H E N       
 
       R E C T _ P ( 0 , 2 0 5 , 8 0 , 2 2 0 , # A A 0 0 F F h ) ; 
 
       T E X T O U T _ P ( " 1   P L A Y E R " , 1 0 , 2 0 5 , 3 , # 0 0 0 0 0 0 h ) ; 
 
       R E C T _ P ( 1 0 0 , 2 0 5 , 1 8 0 , 2 2 0 , # A A 0 0 F F h , # 0 0 0 0 0 0 h ) ; 
 
       T E X T O U T _ P ( " 2   P L A Y E R S " , 1 0 8 , 2 0 5 , 3 , # A A 0 0 F F h ) ; 
 
     E N D ; 
 
     
 
     I F   p l y r = = 2   T H E N 
 
       R E C T _ P ( 0 , 2 0 5 , 8 0 , 2 2 0 , # A A 0 0 F F h , # 0 0 0 0 0 0 h ) ; 
 
       T E X T O U T _ P ( " 1   P L A Y E R " , 1 0 , 2 0 5 , 3 , # A A 0 0 F F h ) ; 
 
       R E C T _ P ( 1 0 0 , 2 0 5 , 1 8 0 , 2 2 0 , # A A 0 0 F F h ) ; 
 
       T E X T O U T _ P ( " 2   P L A Y E R S " , 1 0 8 , 2 0 5 , 3 , # 0 0 0 0 0 0 h ) ;     
 
     E N D ; 
 
   W A I T ( . 2 ) ; 
 
   R E T U R N ; 
 
   E N D ;   
 
   
 
 m i n i M a x ( b o a r d , p l a y e r , d e p t h ) 
 
 / / h o w   i s   t h e   p o s i t i o n   l i k e   f o r   p l a y e r   ( t h e i r   t u r n )   o n   { b o a r d } ? 
 
   B E G I N 
 
     F O R   C   F R O M   1   T O   d e l a y   D O   E N D ;   / / T h i s   i s   a   d e l a y   l o o p   t o   s l o w   d o w n   p r o c e s s i n g   f o r   t e s t i n g . 
 
     L O C A L   s c o r e , m o v e , w i n n e r , t h i s S c o r e , X X ;     
 
     w i n n e r : = c h e c k F o r W i n ( b o a r d ) ; 
 
     I F   w i n n e r < > 0   T H E N   R E T U R N   w i n n e r * p l a y e r ;   E N D ;   / / I f   t h e r e   i s   a   w i n n e r   r e t u r n   w i n n e r . 
 
     I F   d e p t h > m a x D e p t h   T H E N   R E T U R N   0 ;   E N D ;   / / I f   m a x D e p t h   r e a c h e d   t h e n   w o r k   b a c k   u p   t h e   t r e e . 
 
     m o v e : = - 1 ; 
 
     s c o r e : = - 2 ; 
 
     F O R   X X   F R O M   1   T O   9   S T E P   1   D O   / / f o r   a l l   m o v e s       
 
       I F   b o a r d ( X X ) = = 0   T H E N   / / i f   l e g a l , 
 
         b o a r d ( X X ) : = p l a y e r ;   / / t r y   t h e   m o v e . 
 
         t h i s S c o r e : = - m i n i M a x ( b o a r d , p l a y e r * - 1 , d e p t h + 1 ) ; 
 
         I F   t h i s S c o r e > s c o r e   T H E N 
 
           s c o r e : = t h i s S c o r e ;           
 
           m o v e : = X X ;   / / p i c k   t h e   o n e   t h a t ' s   w o r s t   f o r   t h e   o p p o n e n t . 
 
         E N D ; 
 
         b o a r d ( X X ) : = 0 ;   / / r e s e t   b o a r d   a f t e r   t r y . 
 
         / / c l e a r   a n d   d i s p l a y   s e a r c h   d e p t h   o n   s c r e e n . 
 
         R E C T _ P ( 2 3 6 , 1 4 0 , 2 5 5 , 1 8 5 , # 0 0 0 0 0 0 h ) ; 
 
         T E X T O U T _ P ( " M o v e :   " + m o v e , 1 9 5 , 1 4 0 , 3 , # F F B F 0 0 h ) ; 
 
         T E X T O U T _ P ( " S c o r e :   " + s c o r e , 1 9 5 , 1 5 5 , 3 , # F F B F 0 0 h ) ; 
 
         T E X T O U T _ P ( " D e p t h :   " + d e p t h , 1 9 5 , 1 7 0 , 3 , # F F B F 0 0 h ) ;           
 
       E N D ; 
 
     E N D ; 
 
     I F   m o v e = = - 1   T H E N   R E T U R N   0 ;   E N D ;   / / n o   m o r e   m o v e s ,   w o r k   b a c k   u p   t r e e . 
 
     R E T U R N   s c o r e ; 
 
   E N D ; 
 
   
 
 c o m p u t e r M o v e ( b o a r d ) 
 
 / / a l l o w s   c o m p u t e r   t o   c o m p u t e   a n d   m a k e   m o v e 
 
   B E G I N 
 
     L O C A L   m o v e , s c o r e , t e m p S c o r e , Y Y ; 
 
     T E X T O U T _ P ( " T H I N K I N G . . . " , 1 9 5 , 1 2 2 , 3 , # F F F 0 C C h ) ; 
 
     I F   l e v e l = = 3   T H E N   / / o n   h i g h   d e p t h   s e a r c h e s ,   1 s t   t w o   m o v e s   f i x e d   t o   r e d u c e   p e r m u t a t i o n s . 
 
       I F   T N = = 1   T H E N   R E T U R N   5 ;   E N D ;   / / i f   n o t   t a k e n ,   c h o o s e   c e n t e r   s q u a r e   o n   1 s t   m o v e . 
 
       I F   T N = = 2   T H E N 
 
         I F   b o a r d ( 5 ) = = 0   T H E N   R E T U R N   5 ;   / / i f   n o t   t a k e n ,   c h o o s e   c e n t e r   s q u a r e   o n   2 n d   m o v e , 
 
           E L S E   R E T U R N   r a n d C o r n e r ( R A N D I N T ( 1 , 4 ) ) ;   / / o t h e r w i s e   c h o o s e   r a n d o m   c o r n e r . 
 
         E N D ; 
 
       E N D ; 
 
     E N D ; 
 
     I F   l e v e l = = 2   A N D   T N < 4   T H E N   / / o n   f a i r   l e v e l   m a k e   r a n d o m   m o v e s   i n   o p e n i n g     
 
       W H I L E   1 = 1   D O 
 
         m o v e : = R A N D I N T ( 1 , 9 ) ; 
 
         I F   b o a r d ( m o v e ) = = 0   T H E N   R E T U R N   m o v e ;   E N D ; 
 
       E N D ; 
 
     E N D ;     
 
     m o v e : = - 1 ; 
 
     s c o r e : = - 2 ;     
 
     F O R   Y Y   F R O M   1   T O   9   S T E P   1   D O   / / c h e c k   e a c h   s q u a r e   f o r   b e s t   s c o r e . 
 
       I F   b o a r d ( Y Y ) = = 0   T H E N 
 
         b o a r d ( Y Y ) : = 1 ; 
 
         t e m p S c o r e : = - m i n i M a x ( b o a r d , - 1 , 1 ) ; 
 
         b o a r d ( Y Y ) : = 0 ; 
 
         I F   t e m p S c o r e > s c o r e   T H E N   / / I s   t h i s   m o v e   t h e   b e s t   m o v e ? 
 
           s c o r e : = t e m p S c o r e ;   / / I f   y e s , 
 
           m o v e : = Y Y ;   / / r e t u r n   t h i s   m o v e . 
 
         E N D ; 
 
       E N D ; 
 
     E N D ; 
 
     / / r e t u r n s   t h e   m o v e   b a s e d   o n   m i n i m a x   t r e e   s c o r e   o f   a l l   m o v e s   t o   m a x D e p t h . 
 
     R E T U R N   m o v e ; 
 
   E N D ; 
 
 
 
 s e l e c t D e p t h ( l v l ) 
 
 / / w e a k e n s   A I   b y   d e c r e a s i n g   d e p t h   o f   s e a r c h   f o r   e a s y / f a i r   l e v e l s . 
 
   B E G I N     
 
     I F   l v l = = 1   T H E N 
 
       l e v e l : = 1 ; 
 
       m a x D e p t h : = 1 ;   / / l i m i t   s e a r c h   d e p t h   t o   o n e   p l y . 
 
       R E C T _ P ( 1 3 5 , 0 , 1 7 5 , 1 5 , # 4 2 F 4 4 8 h ) ; 
 
       T E X T O U T _ P ( " E A S Y " , 1 3 9 , 0 , 3 , # 0 0 0 0 0 0 h ) ; 
 
       
 
       R E C T _ P ( 1 8 0 , 0 , 2 2 0 , 1 5 , # F 4 D C 4 2 h , # 0 0 0 0 0 0 h ) ; 
 
       T E X T O U T _ P ( " F A I R " , 1 8 5 , 0 , 3 , # F 4 D C 4 2 h ) ; 
 
     
 
       R E C T _ P ( 2 2 5 , 0 , 2 6 5 , 1 5 , # F F 4 3 0 0 h , # 0 0 0 0 0 0 h ) ; 
 
       T E X T O U T _ P ( " H A R D " , 2 2 9 , 0 , 3 , # F F 4 3 0 0 h ) ; 
 
     E N D ; 
 
     
 
     I F   l v l = = 2   T H E N   / / l i m i t   s e a r c h   d e p t h   t o   s i x   p l y . 
 
       l e v e l : = 2 ; 
 
       m a x D e p t h : = 6 ; 
 
       R E C T _ P ( 1 3 5 , 0 , 1 7 5 , 1 5 , # 4 2 F 4 4 8 h , # 0 0 0 0 0 0 h ) ; 
 
       T E X T O U T _ P ( " E A S Y " , 1 3 9 , 0 , 3 , # 4 2 F 4 4 8 h ) ; 
 
       
 
       R E C T _ P ( 1 8 0 , 0 , 2 2 0 , 1 5 , # F 4 D C 4 2 h ) ; 
 
       T E X T O U T _ P ( " F A I R " , 1 8 5 , 0 , 3 , # 0 0 0 0 0 0 h ) ; 
 
   
 
       R E C T _ P ( 2 2 5 , 0 , 2 6 5 , 1 5 , # F F 4 3 0 0 h , # 0 0 0 0 0 0 h ) ; 
 
       T E X T O U T _ P ( " H A R D " , 2 2 9 , 0 , 3 , # F F 4 3 0 0 h ) ; 
 
     E N D ; 
 
     
 
     I F   l v l = = 3   T H E N   / / m a x   s e a r c h   d e p t h   -   a t   t h i s   l e v e l   c o m p u t e r   w i l l   o n l y   w i n   o r   t i e . 
 
       l e v e l : = 3 ; 
 
       m a x D e p t h : = 9 ; 
 
       R E C T _ P ( 1 3 5 , 0 , 1 7 5 , 1 5 , # 4 2 F 4 4 8 h , # 0 0 0 0 0 0 h ) ; 
 
       T E X T O U T _ P ( " E A S Y " , 1 3 9 , 0 , 3 , # 4 2 F 4 4 8 h ) ; 
 
       
 
       R E C T _ P ( 1 8 0 , 0 , 2 2 0 , 1 5 , # F 4 D C 4 2 h , # 0 0 0 0 0 0 h ) ; 
 
       T E X T O U T _ P ( " F A I R " , 1 8 5 , 0 , 3 , # F 4 D C 4 2 h ) ; 
 
   
 
       R E C T _ P ( 2 2 5 , 0 , 2 6 5 , 1 5 , # F F 4 3 0 0 h ) ; 
 
       T E X T O U T _ P ( " H A R D " , 2 2 9 , 0 , 3 , # 0 0 0 0 0 0 h ) ; 
 
     E N D ; 
 
     W A I T ( . 2 ) ; 
 
     R E T U R N ; 
 
   E N D ; 
 
   
 
 s e l e c t A n i m a t e ( a n i m ) 
 
   B E G I N 
 
     I F   a n i m = = 1   T H E N       
 
       R E C T _ P ( 0 , 0 , 3 0 , 1 5 , # 4 2 F 4 4 8 h ) ; 
 
       T E X T O U T _ P ( "   O N   " , 0 , 0 , 3 , # 0 0 0 0 0 0 h ) ; 
 
       a n i m a t e : = 1   
 
     E L S E       
 
       R E C T _ P ( 0 , 0 , 3 0 , 1 5 , # 4 2 F 4 4 8 h , # 0 0 0 0 0 0 h ) ; 
 
       T E X T O U T _ P ( "   O F F " , 0 , 0 , 3 , # 4 2 F 4 4 8 h ) ; 
 
       a n i m a t e : = - 1 
 
     E N D ; 
 
   W A I T ( . 2 ) ;     
 
   R E T U R N ; 
 
   E N D ; 