/* ECP: FILEname=fig11_24.c */ /* 1*/ /* Find The Best Move, And return Its Value */ /* 2*/ /* Assumes: Humanwin < Draw < Computerwin */ /* 3*/ int /* 4*/ ChooseMove( BoardType B, Side S, int *BestRow, int *BestColumn ) /* 5*/ { /* 6*/ Side Opp; /* The Opponent */ /* 7*/ int Reply; /* Value Of Opponent's Reply */ /* 8*/ int Value; /* Value Of Best Move, So Far */ /* 9*/ Piece Mypiece; /* Piece Corresponding To Side S */ /*10*/ int Row, Column; /* Counters */ /*11*/ int Z; /* Placeholder */ /*12*/ int SimpleEval; /* Result Of A Simple Evaluation */ /*13*/ if( ( SimpleEval = Terminal( B ) ) != Unclear ) /*14*/ return SimpleEval; /* Immediate Win, Lose, Or Draw */ /*15*/ /* Set Some Variables Depending On Who S Is */ /*16*/ if( S == Me ) /*17*/ { /*18*/ Opp = You; Value = HumanWin; Mypiece = Computer; /*19*/ } /*20*/ else /*21*/ { /*22*/ Opp = Me; Value = ComputerWin; Mypiece = Human; /*23*/ } /*24*/ /* Search for A Move */ /*25*/ for( Row = 0; Row < Bsize; Row++ ) /*26*/ for( Column = 0; Column < Bsize; Column++ ) /*27*/ if( IsEmpty( B, Row, Column ) ) /*28*/ { /*29*/ Place( B, Row, Column, Mypiece ); /*30*/ Reply = ChooseMove( B, Opp, &Z, &Z ); /*31*/ Unplace( B, Row, Column ); /*32*/ /* Test if S Gets A Better Position */ /*33*/ /* If So, Update */ /*34*/ if( S == Me && Reply > Value || /*35*/ S == You && Reply < Value ) /*36*/ { /*37*/ Value = Reply; /*38*/ *BestRow = Row; *BestColumn = Column; /*39*/ } /*40*/ } /*41*/ return Value; /*42*/ }