1 #include "addpiece.h"
2 
3 /* Adds piece at position pos to board.
4    Evaulates the no of free rows/cols/diags */
addpiece(int piece,int pos)5 void addpiece(int piece, int pos)
6 {
7   int i;
8   board[pos]=2*piece+1;	/* piece has no existence bit */
9   npiece[piece]--;
10   for (i=0; i<NPROPERTIES; i++)
11     npiece[NPROPPOS+2*i+((piece>>i)&1)]--;
12   board[NPIECEPOS]++;
13   board[VALPOS]=boardval();
14   return;
15 }
16 
17 
18 /* Removes piece at position pos to board.
19    Evaluates the no of free rows/cols/diags */
removepiece(int piece,int pos)20 void removepiece(int piece, int pos)
21 {
22   int i;
23   board[pos]=0;
24   npiece[piece]++;
25   for (i=0; i<NPROPERTIES; i++)
26     npiece[NPROPPOS+2*i+((piece>>i)&1)]++;
27   board[NPIECEPOS]--;
28   board[VALPOS]=boardval();
29   return;
30 }
31 
boardval()32 int boardval()
33 {
34   int val=0;
35   int i,j,prop,test1,test2,test3,test4,left1,left2;
36 
37   for (prop=1; prop<=NPROPERTIES; prop++) {
38     for (i=0; i<BOARDLENGTH; i++) { /* consider rows and cols */
39       test1=test2=test3=test4=0;
40       for (j=0; j<BOARDLENGTH; j++) {
41 	if (board[i*BOARDLENGTH+j]&1) {    /* row */
42 	  if ((board[i*BOARDLENGTH+j]&(1<<prop))>>prop) {
43 	    if (test1>=0)
44 	      test1++;
45 	    test2=-1;
46 	  } else {
47 	    if (test2>=0)
48 	      test2++;
49 	    test1=-1;
50 	  }
51 	}
52 	if (board[j*BOARDLENGTH+i]&1) {   /* col */
53 	  if ((board[j*BOARDLENGTH+i]&(1<<prop))>>prop) {
54 	    if (test3>=0)
55 	      test3++;
56 	    test4=-1;
57 	  } else {
58 	    if (test4>=0)
59 	      test4++;
60 	    test3=-1;
61 	  }
62 	}
63       }
64       left1=npiece[NPROPPOS+2*prop+1];
65       left2=npiece[NPROPPOS+2*prop];
66       if (test1>0 && test1+left1>=BOARDLENGTH)
67 	val+=(test1*left1); /* Take into     */
68       if (test2>0 && test2+left2>=BOARDLENGTH)
69 	val+=(test2*left2); /* Take into     */
70       if (test3>0 && test3+left1>=BOARDLENGTH)
71 	val+=(test3*left1); /* Take into     */
72       if (test4>0 && test4+left2>=BOARDLENGTH)
73 	val+=(test4*left2); /* Take into     */
74     }
75     switch (gametype) {
76     case NORMAL:
77       test1=test2=test3=test4=0;
78       for (i=0; i<BOARDLENGTH; i++) { /* consider diags */
79 	if (board[i*(BOARDLENGTH+1)]&1) {
80 	  if ((board[i*(BOARDLENGTH+1)]&(1<<prop))>>prop) {
81 	    if (test1>=0)
82 	      test1++;
83 	    test2=-1;
84 	  } else {
85 	    if (test2>=0)
86 	      test2++;
87 	    test1=-1;
88 	  }
89 	}
90 	if (board[(i+1)*(BOARDLENGTH-1)]&1) {
91 	  if ((board[(i+1)*(BOARDLENGTH-1)]&(1<<prop))>>prop) {
92 	    if (test3>=0)
93 	      test3++;
94 	    test4=-1;
95 	  } else {
96 	    if (test4>=0)
97 	      test4++;
98 	    test3=-1;
99 	  }
100 	}
101       }
102       if (test1>0) val+=(test1*npiece[NPROPPOS+2*prop+1]); /* Take into     */
103       if (test2>0) val+=(test2*npiece[NPROPPOS+2*prop]);   /* account how   */
104       if (test3>0) val+=(test3*npiece[NPROPPOS+2*prop+1]); /* many pieces   */
105       if (test4>0) val+=(test4*npiece[NPROPPOS+2*prop]);   /* there are left*/
106       break;
107     case NODIAGS:
108       break;
109     case TORUS:
110       for (i=0; i<BOARDLENGTH; i++) { /* consider ALL (torus) diags */
111 	test1=test2=test3=test4=0;
112 	for (j=0; j<BOARDLENGTH; j++) {
113 	  if (board[j*BOARDLENGTH+((i+j)%BOARDLENGTH)]&1) {
114 	    if ((board[j*BOARDLENGTH+((i+j)%BOARDLENGTH)]&(1<<prop))>>prop) {
115 	      if (test1>=0)
116 		test1++;
117 	      test2=-1;
118 	    } else {
119 	      if (test2>=0)
120 		test2++;
121 	      test1=-1;
122 	    }
123 	  }
124 
125 	  if (board[j*BOARDLENGTH+((i-j)%BOARDLENGTH)]&1) {
126 	    if ((board[j*BOARDLENGTH+((i-j)%BOARDLENGTH)]&(1<<prop))>>prop) {
127 	      if (test3>=0)
128 		test3++;
129 	      test4=-1;
130 	    } else {
131 	      if (test4>=0)
132 		test4++;
133 	      test3=-1;
134 	    }
135 	  }
136 	}
137 	if (test1>0)
138 	  val+=(test1*npiece[NPROPPOS+2*prop+1]); /* Take into     */
139 	if (test2>0)
140 	  val+=(test2*npiece[NPROPPOS+2*prop]);   /* account how   */
141 	if (test3>0)
142 	  val+=(test3*npiece[NPROPPOS+2*prop+1]); /* many pieces   */
143 	if (test4>0)
144 	  val+=(test4*npiece[NPROPPOS+2*prop]);   /* there are left*/
145       }
146       break;
147     }
148   }
149   return val;
150 }
151