Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. package abertay.xogame;
  2. import boardgameClasses.Board;
  3.  
  4.  
  5.  
  6. public class Intelligence {
  7.    
  8.    
  9. public int intel(Board board){
  10.    
  11. boolean loop=true;
  12.  //flag to allow skipping of code if a value has been returned
  13.    
  14. if (loop==true){   
  15.         //loop for columns
  16.         for (int a=0; a< 7; a++){
  17.             for (int b=0; b<3; b++){
  18.            
  19.                 if ((board.get_state(a,b) ==1) &&
  20.                 board.get_state(a,b)==board.get_state(a,b+1) &&
  21.                 board.get_state(a,b)==board.get_state(a,b+2) && board.get_state(a,b+3)==0 )
  22.                 {
  23.                     loop=false;
  24.                     return a;
  25.                 }
  26.             }
  27.         }
  28. }
  29.  
  30. //rows
  31. if (loop==true){
  32.         for (int a=0; a< 6; a++){
  33.             for (int b=0; b<4; b++){
  34.            
  35.                 if ((board.get_state(b,a) ==1) &&
  36.                 board.get_state(b,a)==board.get_state(b+1,a) &&
  37.                 board.get_state(b,a)==board.get_state(b+2,a)&&                  //MIGHT HAVE TO CHANGE TO EQUALS 1 OR 2
  38.                 ((board.get_state(b+3,a)==0 && board.get_state(b+3,a-1)!=0) | (board.get_state(b-1,a-1)!=0 && board.get_state(b-1,a)==0)))
  39.                 {
  40.                    
  41.                     if (board.get_state(b+3,a)==0 && board.get_state(b+3,a-1)!=0){
  42.                         loop=false;
  43.                         return (b+3);
  44.                     }
  45.                     else if (board.get_state(b-1,a-1)!=0 && board.get_state(b-1,a)==0){
  46.                         loop=false;
  47.                         return (b-1);
  48.                     }
  49.                 }
  50.             }
  51.         }
  52. }      
  53.        
  54. //diagonal to the right
  55. if (loop==true){
  56.     for (int a=0; a< 4; a++){
  57.         for (int b=0; b<3; b++){
  58.        
  59.             if ((board.get_state(a,b) ==1) &&
  60.             board.get_state(a,b)==board.get_state(a+1,b+1) &&
  61.             board.get_state(a,b)==board.get_state(a+2,b+2) &&
  62.             //checking if a piece can be put either side of the diagonal
  63.             ((board.get_state(a+3,b+3)==0 && (board.get_state(a+3,b+2)!=0) ) | (board.get_state(a-1,b-1)==0 && !(a<0)) && !(b<0))  )
  64.             {
  65.                 //top of diagonal
  66.                 if ((board.get_state(a+3,b+3)==0 && ((board.get_state(a+3,b+2)!=0)) )){
  67.                     loop=false;
  68.                     return (a+3);
  69.                 }
  70.                 //bottom of diagonal [MIGHT NEED TO FIX END OF IF STATEMENT TO EQUAL 1 OR 2]
  71.                 else if ((board.get_state(a-1,b-1)==0 && !(a<0)) && !(b<0) && (board.get_state(a-1,b-2)!=0)){
  72.                     loop=false;
  73.                     return (a-1);
  74.                 }
  75.             }
  76.         }
  77.     }
  78. }
  79. //diagonal to the left     
  80. if (loop==true){
  81.     for (int a=4; a< 7; a++){
  82.         for (int b=0; b<3; b++){
  83.        
  84.             if ((board.get_state(a,b) ==1) &&
  85.             board.get_state(a,b)==board.get_state(a-1,b+1) &&
  86.             board.get_state(a,b)==board.get_state(a-2,b+2) &&
  87.             ((board.get_state(a-3,b+3)==0) && (board.get_state(a-3, b+2)!=0) | (board.get_state(a+1,b-1) ==0 && !(a<0)) && !(b<0)) )
  88.             {
  89.                 if ((board.get_state(a-3,b+3)==0) && (board.get_state(a-3, b+2)!=0)){
  90.                     loop=false;
  91.                     return (a-3);
  92.                 }
  93.                 else if ((board.get_state(a+1,b-1) ==0 && !(a<0)) && !(b<0) && (board.get_state(a+1,b-2) !=0)){
  94.                     loop=false;
  95.                     return (a+1);
  96.                 }
  97.            
  98.             }
  99.         }
  100.     }      
  101. }      
  102.  
  103. //for anticipating 4 in a row, when there are only two in a row, with 1 space either side of them
  104. //rows
  105. /*if (loop==true){
  106.     for (int a=0; a< 6; a++){
  107.         for (int b=0; b<4; b++){
  108.        
  109.             if ((board.get_state(b,a) !=0) &&
  110.             board.get_state(b,a)==board.get_state(b+1,a) &&
  111.             ((board.get_state(b+2,a)==0) | (board.get_state(b-1,a)==0)))
  112.             {
  113.                
  114.                 if (board.get_state(b+2,a)==0){
  115.                     loop=false;
  116.                     return (b+3);
  117.                 }
  118.                 else if (board.get_state(b-1,a)==0){
  119.                     loop=false;
  120.                     return (b-1);
  121.                 }
  122.             }
  123.         }
  124.     }
  125. }      
  126. */
  127.  
  128.  
  129.  
  130. //columns
  131. if (loop==true){
  132.     for (int a=0; a< 7; a++){
  133.         for (int b=0; b<3; b++){
  134.        
  135.             if ((board.get_state(a,b) !=0) &&
  136.             board.get_state(a,b)==board.get_state(a,b+1) &&
  137.             board.get_state(a,b+2)==0)
  138.             {
  139.                 loop=false;
  140.                 return a;
  141.             }
  142.         }
  143.     }
  144.    
  145.    
  146.    
  147.    
  148.    
  149.    
  150.    
  151.    
  152. }
  153.        
  154.        
  155.        
  156.        
  157.        
  158.         return 0;
  159.     }
  160.    
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement