Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; tries to evaluate the quality of a playfield , small numbers (variable c) mean few holes and a nice surface
- ; field is a 18 x 10 matrix, field[1,j] notes the bottom row, field[i,1] notes the left-most column
- ; field[i,j] = 0 -> empty cell in row i and column j , field[i,j] = 1 -> filled cell in row i and column j
- penalty2( field )
- {
- ; surf is an array of length 10, a[i] represents the height of the i-th line, a[i]=0 means bottom
- ; otherwise field(a[j],j) = 1 and field(a[j]+i,j) = 0 for every i > 0
- surf := getSurface( field )
- ; x1,y1 are the coordinates of the highest hole, x2,y2 are the coordinates of the second highest hole
- ; hole = filled cell with empty cell below , holes corresponds to the number of holes
- holes := 0, x1 := 0, y1 := 0, x2:= 0, y2 := 0
- recalculate(field, surf, holes, x1, y1, x2, y2 )
- ;MsgBox % holes . " " . x1 . " " . y1 . " " . x2 . " " . y2
- return penalty( surf, holes, x1, y1, x2, y2 )
- }
- ; calculates the number of holes and the coordinates of the 2 highest holes
- recalculate( field, surf, ByRef holes, ByRef x1, ByRef y1, ByRef x2, ByRef y2 )
- {
- ; hole = filled cell with empty cell below
- ;surf := getSurface( field )
- ;x1 := 0, y1 := 0 ; x,y coordinate of highest hole
- ;x2 := 0, y2 := 0 ; x,y coordinate of second highest hole
- ;holes := 0
- j := 0
- while ( ++j <= 10 )
- {
- i := 0
- while ( ++i < surf[j] )
- {
- if ( field[i,j] = 0 )
- {
- ; hole found, count it as 2 if connected to a hole on the left and right, 3 if unconnected
- ; add 0.5 if 2 fields deep, or deepness-1.5 if deeper
- connected := 0
- deepness := 0
- while ( field[i,j] = 0 )
- {
- deepness += 1
- if ( j > 1 && i < surf[j-1] && field[i,j-1] = 0 )
- connected += 0.5 ; every connection is counted double, thus halve it
- if ( j < 10 && i < surf[j+1] && field[i,j+1] = 0 ) ; or else if?
- connected += 0.5
- i := i + 1
- }
- holes += 3
- holes -= connected
- if ( deepness > 2 )
- holes += ( deepness - 1.5 )
- else if ( deepness = 2 )
- holes += 0.5
- ; highest 2 holes are stored in x1,y1 resp x2,y2
- if ( i > y1 )
- {
- x2 := x1, y2 := y1
- x1 := j, y1 := i
- }
- else if ( i > y2 )
- {
- x2 := j, y2 := i
- }
- }
- }
- }
- }
- ; a is an array which stands for the height of the surface in the corresponding column, columns are numbered from 1 to 10
- penalty( a, holes, x1, y1, x2, y2 )
- {
- ;if ( holes - (holes * 10) // 10 != 0 )
- ;{
- ; MsgBox % "#holes not integer: " . holes . " " . x1 . " " . y1 . " " . x2 . " " . y2 . "`n" . ArrayToStr(a)
- ;}
- c := 0 ; returned penalty
- ;MsgBox % "surface: " . arrayToStr(a) , q := 0
- ; ###################### punish holes #######################
- ; hole = filled cell with empty cell below
- ; punish number of holes
- c += 4 * holes
- ;MsgBox % c . " (+" . c-q . ") after counting holes" , q := c
- ; punish if highest hole is deeply covered
- if ( y2 > 0 )
- {
- if ( y1 > y2 )
- c += 3 * min(a[x1]-y1,3) + 1 * min( max(a[x2],a[x1])-y1 , 4 )
- else
- c += 2 * min(a[x1]-y1,3) + 2 * min(a[x2]-y2,3)
- }
- else if ( y1 > 0 )
- {
- c += 4 * min(a[x1]-y1,3)
- }
- ;MsgBox % c . " (+" . c-q . ") after knowing how deep to dig" , q := c
- ; preperation: punish if surface near highest hole isn't flat
- c1 := 0
- if ( y1 > 0 )
- {
- if ( x1 = 1 )
- {
- d := ABS( a[x1+1] - a[x1] )
- if ( a[x1] > a[x1+1] )
- d -= 1
- if ( d > 1 )
- c1 := 6
- else if ( d = 1 )
- c1 := 2
- }
- else if ( x1 = 10 )
- {
- d := ABS( a[x1-1] - a[x1] )
- if ( a[x1] > a[x1-1] )
- d -= 1
- if ( d > 1 )
- c1 := 6
- else if ( d = 1 )
- c1 := 2
- }
- else
- {
- d1 := ABS( a[x1-1] - a[x1] )
- d2 := ABS( a[x1+1] - a[x1] )
- if ( d1 < d2 )
- {
- ; assure that d1 >= d2
- d := d1
- d1 := d2
- d2 := d
- }
- if ( d2 > 1 )
- c1 := 6
- else if ( d1 > 1 && d2 = 1 )
- c1 := 4
- else if ( d1 > 1 && d2 = 0 )
- c1 := 2
- else if ( d2 = 1 )
- {
- if ( a[x1-1] < a[x1] && a[x1+1] < a[x1] )
- c2 := 3
- else
- c2 := 2
- }
- else if ( d1 = 1 )
- c1 := 1
- }
- }
- c2 := 0
- if ( y2 > 0 )
- {
- ; same code as above
- if ( x2 = 1 )
- {
- d := ABS( a[x2+1] - a[x2] )
- if ( d > 1 )
- c2 := 6
- else if ( d = 1 )
- c2 := 2
- }
- else if ( x2 = 10 )
- {
- d := ABS( a[x2-1] - a[x2] )
- if ( d > 1 )
- c2 := 6
- else if ( d = 1 )
- c2 := 2
- }
- else
- {
- d1 := ABS( a[x2-1] - a[x2] )
- d2 := ABS( a[x2+1] - a[x2] )
- if ( d1 < d2 )
- {
- ; assure that d1 >= d2
- d := d1
- d1 := d2
- d2 := d
- }
- if ( d2 > 1 )
- c2 := 6
- else if ( d1 > 1 && d2 = 1 )
- c2 := 4
- else if ( d1 > 1 && d2 = 0 )
- c2 := 2
- else if ( d2 = 1 )
- {
- if ( a[x2-1] < a[x2] && a[x2+1] < a[x2] )
- c2 := 3
- else
- c2 := 2
- }
- else if ( d1 = 1 )
- c2 := 1
- }
- }
- ; execution: punish if surface near highest hole isn't flat
- if ( y2 > 0 )
- {
- if ( y1 > y2 )
- c += 0.8 * c1 + 0.2 * c2
- else
- c += 0.5 * c1 + 0.5 * c2
- }
- else if ( y1 > 0 )
- {
- c += c1
- }
- ;MsgBox % c . " (+" . c-q . ") after checking surface near holes" , q := c
- ; ###################### evaluate surface #######################
- o := 0, s := 0, z := 0 ; number of spots for O, S, Z pieces
- min := 18, max := 0, sum := 0
- i:= 0
- while ( ++i <= 10 )
- {
- sum += a[i]
- if ( min > a[i] )
- min := a[i]
- if ( max < a[i] )
- max := a[i]
- }
- c += 0.1 * sum ; too many filled cells penalty
- if ( max = 18 )
- c += 10
- else if ( max = 17 )
- c += 5 ; too high penalty
- if ( max - min > 7 )
- c += 0.1*( max - min - 7 )*( max - min - 6 ) ; too big global height difference penalty
- 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
- d := 0 ; number of wells
- if ( a[1] < a[2]+2 )
- d += 1
- if ( a[9]+2 > a[10] )
- d += 1
- ;if ( a[1] < a[2]+2 && a[9]+2 > a[10] )
- ; c += 3 ; 2 wells penalty
- ;else if ( a[1] <= a[2]+2 && a[9]+2 >= a[10] )
- ; c += 1
- i := 0
- while ( ++i <= 8 )
- {
- if ( Abs( a[i+1] - a[i] ) > 2 && Abs( a[i+2] - a[i+1] ) > 2 )
- {
- if ( a[i] < a[i+1] && a[i+1] > a[i+2] )
- c += 2 ; obelisc penalty
- else if ( a[i] > a[i+1] && a[i+1] < a[i+2] )
- {
- c += 1 ; trench penalty
- d += 1
- }
- else
- c += 1 ; cliff penalty
- }
- }
- if ( d = 1 )
- c += 2
- else if ( d = 2 )
- c += 5
- else if ( d > 2 )
- c += 5*d - 5 ; too many wells penalty
- i := 0
- while ( ++i <= 9 )
- {
- d := Abs( a[i+1] - a[i] )
- if ( d > 2 )
- c += d - 1 + (d-1)//2 ; local height difference penalty
- else if ( a[i+1] = a[i]-2 )
- c += 1
- else if ( a[i+1] = a[i]+2 )
- c += 1
- else if ( a[i+1] = a[i]-1 )
- s += 1
- else if ( a[i+1] = a[i]+1 )
- z += 1
- else ; a[i] = a[i+1]
- {
- o += 1
- if ( i+2 <= 10 && a[i+2] = a[i]+1 )
- s += 1
- if ( i-1 >= 1 && a[i-1] = a[i]+1 )
- z += 1
- }
- }
- if ( o = 0 )
- c += 5 ; no O spot penalty
- else if ( o = 1 )
- c += 2 ; just one O spot penalty
- else if ( o = 2 )
- c += 1 ; just two O spots penalty
- else if ( o = 3 )
- c += 0.5 ; just three O spots penalty
- if ( s = 0 )
- c += 4 ; no S spot penalty
- else if ( s = 1 )
- c += 1 ; just one S spot penalty
- if ( z = 0 )
- c += 4 ; no Z spot penalty
- else if ( z = 1 )
- c += 1 ; just one Z spot penalty
- w := 0 ; number of white chess fields
- i := 0
- while ( ++i <= 10 )
- {
- w += Mod( i+a[i] , 2 )
- }
- if ( Abs( w - 5 ) >= 2 )
- c += 1 + 7 * ( Abs( w - 5 ) - 2 ) ; bad T placement penalty
- i := 0
- while ( ++i <= 6 )
- {
- w := 0
- w += Mod( i + a[i] , 2 )
- w += Mod( i+1 + a[i+1] , 2 )
- w += Mod( i+2 + a[i+2] , 2 )
- w += Mod( i+3 + a[i+3] , 2 )
- w += Mod( i+4 + a[i+4] , 2 )
- if ( w = 0 || w = 5 )
- c += 2 ; local imbalance penalty
- else if ( w = 1 || w = 4 )
- c += 1
- }
- w := Mod( 1 + a[1] , 2 )
- w += Mod( 2 + a[2] , 2 )
- w += Mod( 3 + a[3] , 2 )
- w += Mod( 4 + a[4] , 2 )
- if ( w = 0 || w = 4 )
- c += 1 ; left side imbalance penalty
- w := Mod( 7 + a[7] , 2 )
- w += Mod( 8 + a[8] , 2 )
- w += Mod( 9 + a[9] , 2 )
- w += Mod( 10 + a[10] , 2 )
- if ( w = 0 || w = 4 )
- c += 1 ; right side imbalance penalty
- ;aaMsgBox % c . " (+" . c-q . ") after parity" , q := c
- return c
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement