1
0
Fork 0
forked from len0rd/rockbox

Patch #4736 for Chessbox from Miguel A. Arévalo - Added support for user interaction while thinking, you can force move now with PLAY and quit with OFF (iRiver example). Yield only on Search as any Evaluate will be preceded by a Search. Plus some minor code policing from me to remove tabs in both the patch and the original code

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8896 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Dave Chapman 2006-03-03 15:01:37 +00:00
parent 8dccb294a8
commit 7ceadd79db
3 changed files with 350 additions and 317 deletions

View file

@ -131,7 +131,7 @@ PLUGIN_HEADER
#define TILE_WIDTH 8
#define TILE_HEIGHT 8
#else
#error BEJEWELED: Unsupported LCD
#error CHESSBOX: Unsupported LCD
#endif
/* Calculate Offsets */
@ -139,12 +139,16 @@ PLUGIN_HEADER
#define YOFS ((LCD_HEIGHT-8*TILE_HEIGHT)/2)
/* commands enum */
#define COMMAND_NOP 0
#define COMMAND_MOVE 1
#define COMMAND_PLAY 2
#define COMMAND_LEVEL 3
/*#define COMMAND_RESTART 4*/
#define COMMAND_QUIT 5
/* "While thinking" command */
int wt_command = COMMAND_NOP;
/* GCC wants this to be present for some targets */
void* memcpy(void* dst, const void* src, size_t size)
{
@ -240,6 +244,24 @@ void cb_switch ( short x , short y ) {
rb->lcd_update();
}
/* ---- callback for capturing interaction while thinking ---- */
void cb_wt_callback ( void ) {
int button = BUTTON_NONE;
wt_command = COMMAND_NOP;
button = rb->button_get(false);
switch (button) {
case CB_QUIT:
wt_command = COMMAND_QUIT;
timeout = true;
break;
case CB_PLAY:
wt_command = COMMAND_PLAY;
timeout = true;
break;
}
}
/* ---- increase playing level ---- */
void cb_levelup ( void ) {
Level ++;
@ -317,9 +339,11 @@ struct cb_command cb_getcommand (void) {
case CB_QUIT:
result.type = COMMAND_QUIT;
return result;
/* case CB_RESTART:
#if 0
case CB_RESTART:
result.type = COMMAND_RESTART;
return result;*/
return result;
#endif
case CB_LEVEL:
result.type = COMMAND_LEVEL;
return result;
@ -469,27 +493,37 @@ enum plugin_status plugin_start(struct plugin_api* api, void* parameter) {
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
rb->cpu_boost ( true );
#endif
SelectMove ( computer , 0 );
SelectMove ( computer , 0 , cb_wt_callback );
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
rb->cpu_boost ( false );
#endif
if ( wt_command == COMMAND_QUIT ) {
exit = true;
break;
}
cb_drawboard();
}
break;
/* case COMMAND_RESTART:
#if 0
case COMMAND_RESTART:
GNUChess_Initialize();
cb_drawboard();
break;*/
break;
#endif
case COMMAND_PLAY:
opponent = !opponent; computer = !computer;
rb->splash ( 0 , true , "Thinking..." );
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
rb->cpu_boost ( true );
#endif
SelectMove ( computer , 0 );
SelectMove ( computer , 0 , cb_wt_callback );
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
rb->cpu_boost ( false );
#endif
if ( wt_command == COMMAND_QUIT ) {
exit = true;
break;
}
cb_drawboard();
break;
case COMMAND_LEVEL:

View file

@ -279,7 +279,8 @@ void CopyBoard( short a[64] , short b[64] );
void OpeningBook ( void );
int search ( short side, short ply, short depth,
short alpha, short beta,
unsigned short bstline[], short *rpt );
unsigned short bstline[], short *rpt,
void (*callback)(void) );
int evaluate ( short side, short xside, short ply, short depth,
short alpha, short beta );
int ProbeTTable ( short side, short depth,
@ -333,9 +334,6 @@ short pscore[3];
xside = otherside[side];
pscore[white] = pscore[black] = 0;
/* ok, I will yield here for lower levels */
rb->yield();
for (c1 = white; c1 <= black; c1++)
{
c2 = otherside[c1];
@ -1018,8 +1016,7 @@ register int sq;
/* ............ MOVE GENERATION & SEARCH ROUTINES .............. */
int SelectMove(side,iop)
short side,iop;
int SelectMove( short side, short iop , void (*callback)(void))
/*
Select a move by calling function search() at progressively deeper
@ -1078,21 +1075,21 @@ static short i,alpha,beta,score,tempb,tempc,tempsf,tempst,xside,rpt;
{
Sdepth++;
/*ShowDepth(' ');*/
score = search(side,1,Sdepth,alpha,beta,PrVar,&rpt);
score = search(side,1,Sdepth,alpha,beta,PrVar,&rpt,callback);
for (i = 1; i <= Sdepth; i++) killr0[i] = PrVar[i];
if (score < alpha && !timeout)
{
/*ShowDepth('-');*/
ExtraTime = 10*ResponseTime;
ZeroTTable();
score = search(side,1,Sdepth,-9000,beta,PrVar,&rpt);
score = search(side,1,Sdepth,-9000,beta,PrVar,&rpt,callback);
}
if (score > beta && !timeout && !(root->flags & exact))
{
/*ShowDepth('+');*/
ExtraTime = 0;
ZeroTTable();
score = search(side,1,Sdepth,alpha,9000,PrVar,&rpt);
score = search(side,1,Sdepth,alpha,9000,PrVar,&rpt,callback);
}
score = root->score;
if (!timeout)
@ -1209,9 +1206,9 @@ struct BookEntry *p;
}\
}
int search(side,ply,depth,alpha,beta,bstline,rpt)
short side,ply,depth,alpha,beta,*rpt;
unsigned short bstline[];
int search( short side, short ply, short depth,
short alpha, short beta, unsigned short bstline[], short *rpt,
void (*callback)(void) )
/*
Perform an alpha-beta search to determine the score for the current
@ -1238,8 +1235,10 @@ short xside,pbst,d,e,cf,score,rcnt;
unsigned short mv,nxtline[maxdepth];
struct leaf *node,tmp;
/* ok, I will yield here for higher levels */
/* this is the only place we need to yield */
rb->yield();
/* and check for user interaction */
callback();
NodeCnt++;
xside = otherside[side];
@ -1308,7 +1307,7 @@ struct leaf *node,tmp;
PawnThreat[ply] = (node->flags & pwnthrt);
Tscore[ply] = node->score;
node->score = -search(xside,ply+1,depth-1,-beta,-alpha,
nxtline,&rcnt);
nxtline,&rcnt,callback);
if (abs(node->score) > 9000) node->flags |= exact;
else if (rcnt == 1) node->score /= 2;
if (rcnt >= 2 || GameCnt-Game50 > 99 ||

View file

@ -26,6 +26,7 @@ extern short board[64];
extern short color[64];
extern long Level;
extern short TCflag,TCmoves,TCminutes;
extern short timeout;
/* ---- RockBox integration ---- */
extern struct plugin_api* rb;
@ -34,7 +35,6 @@ extern struct plugin_api* rb;
void SetTimeControl(void);
void GNUChess_Initialize(void);
int VerifyMove(char s[],short iop,unsigned short *mv);
int SelectMove ( short side, short iop);
int SelectMove ( short side, short iop , void (*callback)(void) );
#endif