checkers (microsoft word 97)

advertisement
Checkers
/* Global arrays */
/* End locations for red */
redwhite=0,1 /* redwhite=0  red, redwhite=1  white */
loop for x=1,…,32; {
End(x,redwhite)=0;
if (redwhite==0) {
if x=(29,30,31,32) End(x, redwhite)=1; }
else if (redwhite==1) {
if x=(1,2,3,4) End(x, redwhite)=1;}
}
/* Neighbors of x */
loop for x=1,…,32;
/* Transformation: 2x = 8*b+a +1+mod(b,2) */
b=floor((x-1)/4); /* b coordinate of a cartesian coordinate system, b=0,…,7 */
x’=2x-1-mod(b,2);
a=mod(x’,8); /* a coordinate of a cartesian coordinate system, a=0,…,7 */
/* Checkers Board and Coordinates */
/* Neighbors of x */
a
1
5
2
6
9
13
10
17
22
16
23
n1
x
b
b+1
n2
n4
n3
20
24
27
31
b-1
12
19
26
30
8
15
a-1 a a+1
4
11
18
25
29
7
14
b 21
3
28
32
/* In the Cartesian notation, we want [n1(x)=(a-1,b-1);n2(x)=(a+1,b1);n3(x)=(a+1,b+1);n4(a-1,b+1)] and the boundary to be inside the board (so the x
Cartesian coordinate and the y Cartesian coordinate must be inside [0,…,7]. */
n1(x)=( if (((a-1) ≥ 0) and ((b-1) ≥ 0)) (1+mod(b,2)+(a-1)+8*(b-1))/2
else 0);
n2(x)=( if (((a+1) ≤ 7) and ((b-1) ≥ 0)) (1+ mod(b,2)+( (a+1)+8*(b-1))/2
else 0);
n3(x)=( if (((a+1) ≤ 7) and ((b+1) ≤ 7)) (1+ mod(b,2)+( (a+1)+8*(b+1))/2
else 0);
n4(x)=( if (((a-1) ≥ 0) and ((b+1) ≤ 7)) (1+ mod(b,2)+( (a-1)+8*(b+1))/2
else 0);
N[x]=list(n1(x), n2(x), n3(x), n4(x));
C[x]=0,1,2,3,4,10; /* respectively empty, red, white, kingred, kingwhite, notallowed
*/
C(x=0)=10; /* if location x is not on the board, the value of C is 10 to indicate it */
main Expand (state, redwhite) /* redwhite=0 (red), redwhite=1 (white) */
initialize list-of-states, C, jump-list;
loop for x C(x)=state[x];
loop for x
jump[x]=0
list-of-states=Nil;
if ((C(x)=2-redwhite) or (C(x)=4-redwhite)) { /* is it my piece to move? */
if (C(x)=2-redwhite) { /* checker piece is single and red */
Jump(C,x,jump[x],redwhite)
if (jump[x]=1)
CheckJump(C,x,list-of-states,redwhite);
else
CheckMove(C,x,list-of-states,redwhite); }
else if (C(x)=4-redwhite) { /* checker piece is king and red */
KingJump (C,x,jump[x],redwhite)
if (jump[x]=1)
KingCheckJump(C,x,list-of-states,redwhite);
else
KingCheckMove(C,x,list-of-states,redwhite); }
list-of-states[x]=list-of-states;
}
KingJump(C,x,jump[x],redwhite) {
loop for ni(x)= n1(x),n2(x),n3(x),n4(x) in N[x]
if ( (C(ni(x))=1+redwhite or C(ni(x))=3+redwhite) and (C(ni(ni(x)))=0) ) {
jump[x]=1;
}
KingCheckJump(C,x, list-of-states, redwhite) {
test=0;
loop for ni(x)=n1(x),n2(x),n3(x),n4(x) in N[x]
x’= ni(ni(x));
if ( (C(ni(x))=1+redwhite or C(ni(x))=3+redwhite) and (C(x’)=0) ) {
test=1;
KingMakeJump(C,x, ni(x), x’), list-of-states, redwhite)
}
if (test=0) list-of-states=append(C, list-of-states);
}
KingMakeJump(C, x, y=ni(x), x’=ni(ni(x)), list-of-states, redwhite){
C(x)=0; /* no longer at x */
C(y)=0; /* jump over and take white piece at y */
C(x’)=4-redwhite; /* move to x’*/
RedCheckJump(C, x’, list-of-states, redwhite) };
}
KingCheckMove(C,x, list-of-states, redwhite) {
loop for ni(x)=n1(x),n2(x),n3(x),n4(x) in N[x]
if ( C(ni(x))=0) KingMakeMove(C, x, ni(x), list-of-states, redwhite)
}
KingMakeMove(C, x, y=ni(x), list-of-states, redwhite) {
C(x)=0;
/* no longer at x */
C(y)=4- redwhite; /* move to y*/
list-of-states=append(C,list-of-states);
}
NeighborForward(x,redwhite,d){ /* d is a diagonal, 0 or 1 */
if (d==0) n(x)= (redwhite? n1(x) : n3(x));
else if (d==1) n(x)= (redwhite? n2(x) : n4(x));
return(n(x));
}
Jump(C, x, jump-list[x], redwhite) {
loop for d=0,1 {/* try both diagonals jumps */
ni= NeighborForward(x,redwhite,d)
x’= NeighborForward(ni,redwhite,d)
if ( (C(ni)=1+redwhite or C(ni)=3+redwhite) and (C(x’)=0) )
jump[x]=1;}
}
CheckJump(C,x, list-of-states, redwhite) {
test=0;
loop for d=0,1 {/* try both diagonals jumps */
ni= NeighborForward(x,redwhite,d)
x’= NeighborForward(ni,redwhite,d)
if ( (C(ni)=1+redwhite or C(ni)=3+redwhite) and (C(x’)=0) ) {
test=1;
MakeJump(C, x, ni, x’, list-of-states, redwhite) }
else if (test=0) list-of-states=append(C, list-of-states);
}
MakeJump(C,x, y=ni(x), x’=ni(ni(x)), list-of-states, redwhite){
C(x)=0; /* no longer at x */
C(y)=0; /* jump over and take white piece at y */
if (End(x’,redwhite)) {
C(x’)=4- redwhite; /* move to x’ and king promotion */
list-of-states=append(C, list-of-states);}
else {
C(x’)=2- redwhite; /* move to x’*/
CheckJump(C,x’, list-of-states, redwhite) };
}
CheckMove(C,x, list-of-states, redwhite) {
loop for d=0,1 {/* try both diagonals jumps */
ni= NeighborForward(x,redwhite,d);
if ( C(ni)=0) MakeMove(C,x, ni, list-of-states);
}
MakeMove(C,x, y=ni(x), list-of-states, redwhite) {
C(x)=0;
/* no longer at x */
if (End(y,redwhite)) C(y)=4- redwhite; /* move to y and king promotion */
else C(y)=2- redwhite; /* move to y*/
list-of-states=append(C,list-of-states); }
Evaluate(list-of-states[x]);
Suggested functions, but they must be combined with some weights and you don’t
have to use all of them and you can choose others.
/* 1: sum total number of your pieces (subtract from adversary) */
/* 2: sum total number of your king pieces (subtract from opponent king pieces) */
/* 3: Offense: count how advanced are your pieces (how close to become a king)
(subtract from opponents advanced position?). How to count? For example, how
much is the sum of “b” component (depending if you are moving towards low b or
high b you want low or high b)*/
/* 4: Defense: how well defended are the pieces? sum total number of red previous
neighbors (n3,n4) to a red piece and subtract from total number of white previous
neighbors (n1, n2) to a white piece. Multiply the difference by alpha (1 criteria? +2
criteria?) */
/* 5: Defense against kings: sum total number of red post neighbors (n1,n2) to a red
piece and subtract from total number of white post neighbors (n1, n2) to a white
piece. Multiply the difference by beta criteria2. */
/* 6: Defense on sides: see if pieces are on the sides of the board, so that they are
defended. This criteria should be combined with 5.*/
/*7: Defense: Kings on corners are better defended. This is should be combined with
5 and 6 */
/* 8: Dyanmic position. Count number of moves by pieces. Count number of moves
by kings. Compare with opponents. */
Tip for the Cut-off depth criteria of the search:
If the final state where you apply evaluation allow for follow up jumps, then “don’t
stop the evaluation there, do more search steps in this path, until no jumps are left (or
stop at some cut off).
Download