Advertisement
Guest User

AutoHotKey Tetris Bot evaluation function

a guest
May 14th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; tries to evaluate the quality of a playfield , small numbers (variable c) mean few holes and a nice surface
  2. ; field is a 18 x 10 matrix, field[1,j] notes the bottom row, field[i,1] notes the left-most column
  3. ; field[i,j] = 0 -> empty cell in row i and column j , field[i,j] = 1 -> filled cell in row i and column j
  4. penalty2( field )
  5. {
  6.     ; surf is an array of length 10, a[i] represents the height of the i-th line, a[i]=0 means bottom
  7.     ; otherwise field(a[j],j) = 1 and field(a[j]+i,j) = 0 for every i > 0  
  8.     surf := getSurface( field )
  9.  
  10.     ; x1,y1 are the coordinates of the highest hole, x2,y2 are the coordinates of the second highest hole
  11.     ; hole = filled cell with empty cell below , holes corresponds to the number of holes
  12.     holes := 0, x1 := 0, y1 := 0, x2:= 0, y2 := 0
  13.     recalculate(field, surf, holes, x1, y1, x2, y2 )    
  14.     ;MsgBox % holes . " " . x1 . " " . y1 . " " . x2 . " " . y2
  15.  
  16.     return penalty( surf, holes, x1, y1, x2, y2 )  
  17. }
  18.  
  19.  
  20. ; calculates the number of holes and the coordinates of the 2 highest holes
  21. recalculate( field, surf, ByRef holes, ByRef x1, ByRef y1, ByRef x2, ByRef y2 )
  22. {
  23.     ; hole = filled cell with empty cell below 
  24.    
  25.     ;surf := getSurface( field )   
  26.     ;x1 := 0, y1 := 0 ; x,y coordinate of highest hole
  27.     ;x2 := 0, y2 := 0 ; x,y coordinate of second highest hole
  28.     ;holes := 0
  29.    
  30.     j := 0
  31.     while ( ++j <= 10 )
  32.     {  
  33.         i := 0
  34.         while ( ++i < surf[j] )
  35.         {
  36.             if ( field[i,j] = 0 )
  37.             {
  38.                 ; hole found, count it as 2 if connected to a hole on the left and right, 3 if unconnected
  39.                 ; add 0.5 if 2 fields deep, or deepness-1.5 if deeper
  40.                 connected := 0
  41.                 deepness := 0
  42.                 while ( field[i,j] = 0 )
  43.                 {
  44.                     deepness += 1              
  45.                     if ( j > 1 && i < surf[j-1] && field[i,j-1] = 0 )
  46.                             connected += 0.5 ; every connection is counted double, thus halve it
  47.                     if ( j < 10 && i < surf[j+1] && field[i,j+1] = 0 ) ; or else if?
  48.                         connected += 0.5
  49.                     i := i + 1 
  50.                 }
  51.                 holes += 3
  52.                 holes -= connected
  53.                 if ( deepness > 2 )
  54.                     holes += ( deepness - 1.5 )
  55.                 else if ( deepness = 2 )
  56.                     holes += 0.5           
  57.                 ; highest 2 holes are stored in x1,y1 resp x2,y2
  58.                 if ( i > y1 )
  59.                 {
  60.                     x2 := x1, y2 := y1
  61.                     x1 := j, y1 := i
  62.                 }
  63.                 else if ( i > y2 )
  64.                 {
  65.                     x2 := j, y2 := i
  66.                 }
  67.             }
  68.         }
  69.     }  
  70. }
  71.  
  72.  
  73. ; a is an array which stands for the height of the surface in the corresponding column, columns are numbered from 1 to 10
  74. penalty( a, holes, x1, y1, x2, y2 )
  75. {
  76.     ;if ( holes - (holes * 10) // 10 != 0 )
  77.     ;{
  78.     ;   MsgBox % "#holes not integer: " . holes . " " . x1 . " " . y1 . " " . x2 . " " . y2 . "`n" . ArrayToStr(a)
  79.     ;}
  80.    
  81.     c := 0 ; returned penalty  
  82.     ;MsgBox % "surface: " . arrayToStr(a) , q := 0
  83.    
  84.     ; ###################### punish holes #######################
  85.     ; hole = filled cell with empty cell below
  86.  
  87.     ; punish number of holes
  88.     c += 4 * holes
  89.     ;MsgBox % c . " (+" . c-q . ") after counting holes"    , q := c
  90.    
  91.     ; punish if highest hole is deeply covered
  92.     if ( y2 > 0 )
  93.     {
  94.         if ( y1 > y2 )
  95.             c += 3 * min(a[x1]-y1,3) + 1 * min( max(a[x2],a[x1])-y1 , 4 )
  96.         else
  97.             c += 2 * min(a[x1]-y1,3) + 2 * min(a[x2]-y2,3)
  98.     }
  99.     else if ( y1 > 0 )
  100.     {
  101.         c += 4 * min(a[x1]-y1,3)
  102.     }
  103.     ;MsgBox % c . " (+" . c-q . ") after knowing how deep to dig"   , q := c
  104.    
  105.     ; preperation: punish if surface near highest hole isn't flat
  106.     c1 := 0
  107.     if ( y1 > 0 )
  108.     {
  109.         if ( x1 = 1 )
  110.         {
  111.             d := ABS( a[x1+1] - a[x1] )
  112.             if ( a[x1] > a[x1+1] )
  113.                 d -= 1
  114.             if ( d > 1 )
  115.                 c1 := 6
  116.             else if ( d = 1 )
  117.                 c1 := 2
  118.         }
  119.         else if ( x1 = 10 )
  120.         {
  121.             d := ABS( a[x1-1] - a[x1] )
  122.             if ( a[x1] > a[x1-1] )
  123.                 d -= 1
  124.             if ( d > 1 )
  125.                 c1 := 6
  126.             else if ( d = 1 )
  127.                 c1 := 2            
  128.         }
  129.         else
  130.         {
  131.             d1 := ABS( a[x1-1] - a[x1] )               
  132.             d2 := ABS( a[x1+1] - a[x1] )
  133.             if ( d1 < d2 )
  134.             {
  135.                 ; assure that d1 >= d2
  136.                 d := d1
  137.                 d1 := d2
  138.                 d2 := d            
  139.             }
  140.             if ( d2 > 1 )
  141.                 c1 := 6
  142.             else if ( d1 > 1 && d2 = 1 )
  143.                 c1 := 4
  144.             else if ( d1 > 1 && d2 = 0 )
  145.                 c1 := 2
  146.             else if ( d2 = 1 )
  147.             {
  148.                 if ( a[x1-1] < a[x1] && a[x1+1] < a[x1] )
  149.                     c2 := 3
  150.                 else
  151.                     c2 := 2
  152.             }
  153.             else if ( d1 = 1 )
  154.                 c1 := 1
  155.         }  
  156.     }
  157.     c2 := 0
  158.     if ( y2 > 0 )
  159.     {
  160.         ; same code as above
  161.         if ( x2 = 1 )
  162.         {
  163.             d := ABS( a[x2+1] - a[x2] )
  164.             if ( d > 1 )
  165.                 c2 := 6
  166.             else if ( d = 1 )
  167.                 c2 := 2
  168.         }
  169.         else if ( x2 = 10 )
  170.         {
  171.             d := ABS( a[x2-1] - a[x2] )
  172.             if ( d > 1 )
  173.                 c2 := 6
  174.             else if ( d = 1 )
  175.                 c2 := 2            
  176.         }
  177.        
  178.         else
  179.         {
  180.             d1 := ABS( a[x2-1] - a[x2] )               
  181.             d2 := ABS( a[x2+1] - a[x2] )
  182.             if ( d1 < d2 )
  183.             {
  184.                 ; assure that d1 >= d2
  185.                 d := d1
  186.                 d1 := d2
  187.                 d2 := d            
  188.             }
  189.             if ( d2 > 1 )
  190.                 c2 := 6
  191.             else if ( d1 > 1 && d2 = 1 )
  192.                 c2 := 4
  193.             else if ( d1 > 1 && d2 = 0 )
  194.                 c2 := 2
  195.             else if ( d2 = 1 )
  196.             {
  197.                 if ( a[x2-1] < a[x2] && a[x2+1] < a[x2] )
  198.                     c2 := 3
  199.                 else
  200.                     c2 := 2
  201.             }
  202.             else if ( d1 = 1 )
  203.                 c2 := 1
  204.         }          
  205.     }
  206.     ; execution: punish if surface near highest hole isn't flat    
  207.     if ( y2 > 0 )
  208.     {
  209.         if ( y1 > y2 )
  210.             c += 0.8 * c1 + 0.2 * c2
  211.         else
  212.             c += 0.5 * c1 + 0.5 * c2
  213.     }
  214.     else if ( y1 > 0 )
  215.     {
  216.         c += c1
  217.     }  
  218.        
  219.     ;MsgBox % c . " (+" . c-q . ") after checking surface near holes"   , q := c
  220.     ; ###################### evaluate surface #######################
  221.  
  222.     o := 0, s := 0, z := 0 ; number of spots for O, S, Z pieces
  223.    
  224.     min := 18, max := 0, sum := 0
  225.     i:= 0
  226.     while ( ++i <= 10 )
  227.     {
  228.         sum += a[i]
  229.         if ( min > a[i] )
  230.             min := a[i]
  231.         if ( max < a[i] )
  232.             max := a[i]    
  233.     }
  234.     c += 0.1 * sum ; too many filled cells penalty
  235.    
  236.     if ( max = 18 )
  237.         c += 10
  238.     else if ( max = 17 )
  239.         c += 5  ; too high penalty
  240.    
  241.     if ( max - min > 7 )
  242.         c += 0.1*( max - min - 7 )*( max - min - 6 ) ; too big global height difference penalty
  243.    
  244.     c += ( 1.5*a[4] + 1.5*a[5] + 1.5*a[6] + 1.5*a[7] - 2*a[1] - 2*a[10] - a[2] - a[9] ) / 10 ; hill in the middle penalty
  245.    
  246.     d := 0 ; number of wells
  247.     if ( a[1] < a[2]+2 )   
  248.         d += 1
  249.     if ( a[9]+2 > a[10] )
  250.         d += 1
  251.    
  252.     ;if ( a[1] < a[2]+2 && a[9]+2 > a[10] )
  253.     ;   c += 3 ; 2 wells penalty
  254.     ;else if ( a[1] <= a[2]+2 && a[9]+2 >= a[10] )
  255.     ;   c += 1
  256.    
  257.     i := 0
  258.     while ( ++i <= 8 )
  259.     {
  260.         if ( Abs( a[i+1] - a[i] ) > 2 && Abs( a[i+2] - a[i+1] ) > 2 )
  261.         {
  262.             if ( a[i] < a[i+1] && a[i+1] > a[i+2] )
  263.                 c += 2 ; obelisc penalty
  264.             else if ( a[i] > a[i+1] && a[i+1] < a[i+2] )
  265.             {
  266.                 c += 1 ; trench penalty
  267.                 d += 1             
  268.             }
  269.             else
  270.                 c += 1 ; cliff penalty
  271.         }
  272.     }
  273.    
  274.     if ( d = 1 )
  275.         c += 2
  276.     else if ( d = 2 )
  277.         c += 5
  278.     else if ( d > 2 )
  279.         c += 5*d - 5 ; too many wells penalty
  280.  
  281.     i := 0
  282.     while ( ++i <= 9 )
  283.     {
  284.         d := Abs( a[i+1] - a[i] )
  285.         if ( d > 2 )
  286.             c += d - 1 + (d-1)//2 ; local height difference penalty
  287.         else if ( a[i+1] = a[i]-2 )
  288.             c += 1
  289.         else if ( a[i+1] = a[i]+2 )
  290.             c += 1 
  291.         else if ( a[i+1] = a[i]-1 )
  292.             s += 1
  293.         else if ( a[i+1] = a[i]+1 )
  294.             z += 1
  295.         else ; a[i] = a[i+1]
  296.         {
  297.             o += 1
  298.             if ( i+2 <= 10 && a[i+2] = a[i]+1 )
  299.                 s += 1
  300.             if ( i-1 >= 1 && a[i-1] = a[i]+1 )
  301.                 z += 1
  302.         }          
  303.     }
  304.        
  305.     if ( o = 0 )
  306.         c += 5 ; no O spot penalty
  307.     else if ( o = 1 )
  308.         c += 2 ; just one O spot penalty
  309.     else if ( o = 2 )
  310.         c += 1 ; just two O spots penalty
  311.     else if ( o = 3 )
  312.         c += 0.5 ; just three O spots penalty
  313.     if ( s = 0 )
  314.         c += 4 ; no S spot penalty
  315.     else if ( s = 1 )
  316.         c += 1 ; just one S spot penalty       
  317.     if ( z = 0 )
  318.         c += 4 ; no Z spot penalty
  319.     else if ( z = 1 )
  320.         c += 1 ; just one Z spot penalty
  321.  
  322.     w := 0 ; number of white chess fields
  323.    
  324.     i := 0
  325.     while ( ++i <= 10 )
  326.     {
  327.         w += Mod( i+a[i] , 2 )
  328.     }
  329.     if ( Abs( w - 5 ) >= 2 )
  330.         c += 1 + 7 * ( Abs( w - 5 ) - 2 ) ; bad T placement penalty
  331.    
  332.     i := 0
  333.     while ( ++i <= 6 )
  334.     {
  335.         w := 0
  336.         w += Mod( i + a[i] , 2 )
  337.         w += Mod( i+1 + a[i+1] , 2 )
  338.         w += Mod( i+2 + a[i+2] , 2 )       
  339.         w += Mod( i+3 + a[i+3] , 2 )       
  340.         w += Mod( i+4 + a[i+4] , 2 )
  341.         if ( w = 0 || w = 5 )
  342.             c += 2 ; local imbalance penalty
  343.         else if ( w = 1 || w = 4 )
  344.             c += 1
  345.     }
  346.     w := Mod( 1 + a[1] , 2 )
  347.     w += Mod( 2 + a[2] , 2 )
  348.     w += Mod( 3 + a[3] , 2 )
  349.     w += Mod( 4 + a[4] , 2 )
  350.     if ( w = 0 || w = 4 )
  351.         c += 1 ; left side imbalance penalty
  352.     w := Mod( 7 + a[7] , 2 )
  353.     w += Mod( 8 + a[8] , 2 )
  354.     w += Mod( 9 + a[9] , 2 )
  355.     w += Mod( 10 + a[10] , 2 )
  356.     if ( w = 0 || w = 4 )
  357.         c += 1 ; right side imbalance penalty
  358.  
  359.     ;aaMsgBox % c . " (+" . c-q . ") after parity"  , q := c
  360.     return c
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement