#include #include #include "oware.h" int value1(int side[]) { int v=side[houses]; if (v>24) /* win */ return 100; if (v==24) /* draw or better */ return 50; return v; } int value(int thisside[], int otherside[]) { return value1(thisside)-value1(otherside); } /* best move when looking ahead from the position thisside/otherside for depth levels, and finally using value(); the return value is the best move (range 0-5), the value is passed back through vp; this function implements the negamax() algorithm */ int bestmove(int depth, int thisside[], int otherside[], int *vp) { int best=-1; *vp=-1000; for (int i=0; i0 && -50<=v && v<=50) { (void)bestmove(depth-1,os1,ts1,&v); v = -v; } #if 0 printf("%*c%d %d%c\n",20-depth,' ',i+1,v,(v>*vp)?'*':' '); #endif if (v>*vp) { *vp=v; best=i; } } } return best; } int main(int argc, char *argv[]) { int side1[houses+1]; int side2[houses+1]; int depth; int best,value; if (argc != 2*houses+4) { fprintf(stderr,"%s ... ... \n",argv[0]); exit(1); } depth=atoi(argv[1]); if (depth<0) { fprintf(stderr,"depth must be positive\n"); exit(1); } if (cmdline2pos(argc-2,argv+2,side1,side2)==0) { fprintf(stderr,"this should not happen\n"); exit(1); } printf("Position:\n"); printboard(side1,side2); printf("\n"); best=bestmove(depth, side1, side2, &value); printf("Best move (1-6): %d Value: %d\n", best+1, value); return 0; }