summaryrefslogtreecommitdiff
path: root/osr.c
diff options
context:
space:
mode:
authorRuss Allbery <rra@debian.org>2009-12-06 13:28:36 -0800
committerRuss Allbery <rra@debian.org>2009-12-06 13:28:36 -0800
commit2dc6c06022f278f944c8b502fbe8ced1c73811e4 (patch)
treee6290c6509342ccbfca8004b4295b9b8604c4594 /osr.c
parentfe15fa62fee6fe5e5f09e2ad6156be97e2b4f357 (diff)
Imported Upstream version 0.90+20091206
Diffstat (limited to 'osr.c')
-rw-r--r--osr.c134
1 files changed, 67 insertions, 67 deletions
diff --git a/osr.c b/osr.c
index 7e24b64..840dd23 100644
--- a/osr.c
+++ b/osr.c
@@ -2,6 +2,8 @@
/*
* osr.c
*
+ * one-sided race rollouts
+ *
* by Jørn Thyssen <jthyssen@dk.ibm.com>, 2002.
* (after inspiration from osr.cc from fibs2html <fibs2html.sourceforge.net>
*
@@ -18,25 +20,20 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
- * $Id: osr.c,v 1.30 2008/02/28 14:15:51 c_anthon Exp $
+ * $Id: osr.c,v 1.34 2009/04/20 15:44:09 c_anthon Exp $
*/
-/*! \file osr.c
- \brief one-sided race rollouts
-*/
+/*lint -e514*/
+#include "config.h"
-#include <stdio.h>
#include <glib.h>
#include <string.h>
-
-#include "config.h"
-
-#include "backgammon.h"
+#include "eval.h"
#include "positionid.h"
-#include "osr.h"
#include "mt19937ar.h"
+#include "osr.h"
#define MAX_PROBS 32
#define MAX_GAMMON_PROBS 15
@@ -44,18 +41,16 @@
static unsigned long mt[ N ];
static int mti = N + 1;
-static int
-OSRQuasiRandomDice( const int iTurn, const int iGame, const int cGames,
- int anDice[ 2 ] ) {
-
+static unsigned int OSRQuasiRandomDice( const unsigned int iTurn, const unsigned int iGame, const unsigned int cGames, unsigned int anDice[ 2 ] )
+{
if( !iTurn && !( cGames % 36 ) ) {
anDice[ 0 ] = ( iGame % 6 ) + 1;
anDice[ 1 ] = ( ( iGame / 6 ) % 6 ) + 1;
- return 0;
+ return TRUE;
} else if( iTurn == 1 && !( cGames % 1296 ) ) {
anDice[ 0 ] = ( ( iGame / 36 ) % 6 ) + 1;
anDice[ 1 ] = ( ( iGame / 216 ) % 6 ) + 1;
- return 0;
+ return TRUE;
} else {
anDice[ 0 ] = ( genrand_int32(&mti,mt) % 6 ) + 1;
anDice[ 1 ] = ( genrand_int32(&mti,mt) % 6 ) + 1;
@@ -75,7 +70,7 @@ static inline void getBearoffProbs(const unsigned int n, unsigned short int aaPr
static int
-isCrossOver ( const int from, const int to ) {
+isCrossOver ( const unsigned int from, const unsigned int to ) {
return ( from / 6 ) != ( to / 6 );
}
@@ -116,12 +111,12 @@ static int checkboard(const unsigned int anBoard[25])
return 1;
}
-static void FindBestMoveOSR2 ( unsigned int anBoard[ 25 ], const int anDice[ 2 ], unsigned int *pnOut )
+static void FindBestMoveOSR2 ( unsigned int anBoard[ 25 ], const unsigned int anDice[ 2 ], unsigned int *pnOut )
{
- int ifar, inear, iboth;
- int iused = 0;
- int i, j, lc;
- int found;
+ unsigned int ifar, inear, iboth;
+ unsigned int iused = 0;
+ unsigned int i, j, lc;
+ unsigned int found;
ifar = 5 + anDice[ 0 ];
inear = 5 + anDice[ 1 ];
@@ -200,7 +195,7 @@ static void FindBestMoveOSR2 ( unsigned int anBoard[ 25 ], const int anDice[ 2 ]
/* try to make cross over from the back */
found = FALSE;
- for ( j = lc; j - anDice[ i ] >5; --j ) {
+ for ( j = lc; j - anDice[ i ] > 5; --j ) {
if ( anBoard[ j ] && isCrossOver ( j, j - anDice[ i ] ) ) {
@@ -267,8 +262,9 @@ static void FindBestMoveOSR2 ( unsigned int anBoard[ 25 ], const int anDice[ 2 ]
/* try filling rearest empty space */
- for ( j = 5; j - anDice[ 1 ] > -1; --j ) {
-
+ for (i = 0; i < 6 - anDice[ 1 ]; i++)
+ {
+ j = 5 - i;
if ( anBoard[ j ] && ! anBoard[ j - anDice[ 1 ] ] ) {
/* empty space found */
@@ -282,8 +278,9 @@ static void FindBestMoveOSR2 ( unsigned int anBoard[ 25 ], const int anDice[ 2 ]
}
/* move chequer from the rear */
-
- for ( j = 5; j > -1; --j ) {
+ for (i = 0; i < 6; i++)
+ {
+ j = 5 - i;
if ( anBoard[ j ] ) {
@@ -291,7 +288,7 @@ static void FindBestMoveOSR2 ( unsigned int anBoard[ 25 ], const int anDice[ 2 ]
--anBoard[ j ];
- if ( j - anDice[ 1 ] > -1 )
+ if ( j >= anDice[ 1 ] )
/* add chequer to point */
++anBoard[ j - anDice[ 1 ] ];
/* else */
@@ -325,12 +322,12 @@ static void FindBestMoveOSR2 ( unsigned int anBoard[ 25 ], const int anDice[ 2 ]
*
*/
-static void FindBestMoveOSR4(unsigned int anBoard[ 25 ], const int nDice, unsigned int *pnOut)
+static void FindBestMoveOSR4(unsigned int anBoard[ 25 ], const unsigned int nDice, unsigned int *pnOut)
{
- int nd = 4;
- int i, n = 0;
- int first, any;
- int lc;
+ unsigned int nd = 4;
+ unsigned int i, j, n = 0;
+ unsigned int first, any;
+ unsigned int lc;
/* check for exact bear-ins */
@@ -347,6 +344,9 @@ static void FindBestMoveOSR4(unsigned int anBoard[ 25 ], const int nDice, unsign
}
+#if 0
+ /* this is broken or very very slow when the player has chequers in the
+ * 3rd or 4th quadrant */
if (*pnOut > 0)
{
/* check for 4, 3, or 2 chequers move exactly into home quadrant */
@@ -370,6 +370,7 @@ static void FindBestMoveOSR4(unsigned int anBoard[ 25 ], const int nDice, unsign
}
}
}
+#endif
if ( *pnOut > 0 && nd > 0 )
{
@@ -494,17 +495,19 @@ static void FindBestMoveOSR4(unsigned int anBoard[ 25 ], const int nDice, unsign
/* FIXME: fill gaps? */
- for ( i = 5; nd && i > -1; --i ) {
+ for (i = 0; nd && i < 6; i++)
+ {
+ j = 5 - i;
- while ( anBoard[ i ] && nd ) {
+ while ( anBoard[ j ] && nd ) {
any = TRUE;
- --anBoard[ i ];
+ --anBoard[ j ];
--nd;
- if ( i - nDice > -1 )
- ++anBoard[ i - nDice ];
+ if ( j >= nDice )
+ ++anBoard[ j - nDice ];
g_assert ( checkboard ( anBoard ) );
}
@@ -533,7 +536,7 @@ static void FindBestMoveOSR4(unsigned int anBoard[ 25 ], const int nDice, unsign
*
*/
-static void FindBestMoveOSR(unsigned int anBoard[ 25 ], const int anDice[ 2 ], unsigned int *pnOut)
+static void FindBestMoveOSR(unsigned int anBoard[ 25 ], const unsigned int anDice[ 2 ], unsigned int *pnOut)
{
if (anDice[0] != anDice[1])
FindBestMoveOSR2(anBoard, anDice, pnOut);
@@ -554,21 +557,21 @@ static void FindBestMoveOSR(unsigned int anBoard[ 25 ], const int anDice[ 2 ], u
* quadrant.
*/
-static int osr(unsigned int anBoard[25], const int iGame, const int nGames, unsigned int nOut)
+static unsigned int osr(unsigned int anBoard[25], const unsigned int iGame, const unsigned int nGames, unsigned int nOut)
{
- int iTurn = 0;
- int anDice[ 2 ];
+ unsigned int iTurn = 0;
+ unsigned int anDice[ 2 ];
/* loop until all chequers are in home quadrant */
while (nOut)
{
/* roll dice */
- if ( OSRQuasiRandomDice ( iTurn, iGame, nGames, anDice ) < 0 )
- return -1;
+ if ( OSRQuasiRandomDice ( iTurn, iGame, nGames, anDice ) == FALSE)
+ g_warning("Error in function OSRQuasiRandomDice");
if ( anDice[ 0 ] < anDice[ 1 ] )
- swap ( anDice, anDice + 1 );
+ swap_us ( anDice, anDice + 1 );
/* find and move best move */
FindBestMoveOSR(anBoard, anDice, &nOut);
@@ -595,15 +598,15 @@ static int osr(unsigned int anBoard[25], const int iGame, const int nGames, unsi
*/
static void
-rollOSR ( const int nGames, const unsigned int anBoard[ 25 ], const int nOut,
+rollOSR ( const unsigned int nGames, const unsigned int anBoard[ 25 ], const unsigned int nOut,
float arProbs[], const unsigned int nMaxProbs,
float arGammonProbs[], const unsigned int nMaxGammonProbs ) {
unsigned int an[ 25 ];
unsigned short int anProb[ 32 ];
unsigned int i;
- int n, m;
- int iGame;
+ unsigned int n, m;
+ unsigned int iGame;
int *anCounts = (int*) g_alloca(nMaxGammonProbs * sizeof(int));
@@ -630,11 +633,11 @@ rollOSR ( const int nGames, const unsigned int anBoard[ 25 ], const int nOut,
/* update counts */
- ++anCounts[ MIN( m == 15 ? n + 1 : n, (int)nMaxGammonProbs - 1 ) ];
+ ++anCounts[ MIN( m == 15 ? n + 1 : n, nMaxGammonProbs - 1 ) ];
/* get prob. from bearoff1 */
- getBearoffProbs ( PositionBearoff ( an, 6, 15 ), anProb );
+ getBearoffProbs ( PositionBearoff ( an, pbc1->nPoints, pbc1->nChequers ), anProb );
for ( i = 0; i < 32; ++i )
arProbs[ MIN( n + i, nMaxProbs - 1 ) ] += anProb[ i ] / 65535.0f;
@@ -675,13 +678,12 @@ rollOSR ( const int nGames, const unsigned int anBoard[ 25 ], const int nOut,
*
*/
-static int
-osp ( const unsigned int anBoard[ 25 ], const int nGames,
+static unsigned int osp ( const unsigned int anBoard[ 25 ], const unsigned int nGames,
unsigned int an[ 25 ], float arProbs[ MAX_PROBS ],
float arGammonProbs[ MAX_GAMMON_PROBS ] ) {
int i, n;
- int nTotal, nOut;
+ unsigned int nTotal, nOut;
unsigned short int anProb[ 32 ];
/* copy board into an, and find total number of chequers left,
@@ -721,7 +723,7 @@ osp ( const unsigned int anBoard[ 25 ], const int nGames,
for ( i = 0; i < MAX_PROBS; ++i )
arProbs[ i ] = 0.0f;
- getBearoffProbs ( PositionBearoff ( anBoard, 6, 15 ), anProb );
+ getBearoffProbs ( PositionBearoff ( anBoard, pbc1->nPoints, pbc1->nChequers), anProb );
for ( i = 0; i < 32; ++i ) {
n = MIN( i, MAX_PROBS - 1 );
@@ -739,12 +741,12 @@ osp ( const unsigned int anBoard[ 25 ], const int nGames,
static float
bgProb ( const unsigned int anBoard[ 25 ],
const int fOnRoll,
- const int nTotal,
+ const unsigned int nTotal,
const float arProbs[],
- const int nMaxProbs ) {
+ const unsigned int nMaxProbs ) {
- int nTotPipsHome = 0;
- int i, j;
+ unsigned int nTotPipsHome = 0;
+ unsigned int i, j;
float r, s;
unsigned short int anProb[ 32 ];
@@ -761,15 +763,15 @@ bgProb ( const unsigned int anBoard[ 25 ],
/* (nTotPipsHome + 2) / 3: numbers of rolls before I'm out of
opponent's home quadrant (with consequtive 2-1's) */
- if ( ( nTotal + 3 ) / 4 - 1 <= ( nTotPipsHome + 2 ) / 3 ) {
-
+ if ( ( nTotal + 3 ) / 4 - 1 <= ( nTotPipsHome + 2 ) / 3 )
+ {
/* backgammon is possible */
/* get "bear-off" prob (for getting out of opp.'s home quadr.) */
/* FIXME: this ignores chequers on the bar */
- getBearoffProbs ( PositionBearoff ( anBoard + 18, 6, 15 ), anProb );
+ getBearoffProbs ( PositionBearoff ( anBoard + 18, pbc1->nPoints, pbc1->nChequers ), anProb );
for ( i = 0; i < nMaxProbs; ++i ) {
@@ -777,7 +779,7 @@ bgProb ( const unsigned int anBoard[ 25 ],
s = 0.0f;
- for ( j = i + ! fOnRoll; j < 32; ++j )
+ for (j = i + !fOnRoll; j < 32; ++j)
s += anProb[ j ] / 65535.0f;
r += s * arProbs[ i ];
@@ -809,7 +811,7 @@ bgProb ( const unsigned int anBoard[ 25 ],
*/
extern void
-raceProbs ( const TanBoard anBoard, const int nGames,
+raceProbs ( const TanBoard anBoard, const unsigned int nGames,
float arOutput[ NUM_OUTPUTS ],
float arMu[ 2 ] ) {
@@ -818,7 +820,7 @@ raceProbs ( const TanBoard anBoard, const int nGames,
float aarGammonProbs[ 2 ][ MAX_PROBS ];
float arG[ 2 ], arBG[ 2 ];
- int anTotal[ 2 ];
+ unsigned int anTotal[ 2 ];
int i, j, k;
@@ -832,9 +834,7 @@ raceProbs ( const TanBoard anBoard, const int nGames,
arOutput[ i ] = 0.0f;
for ( i = 0; i < 2; ++i )
- anTotal[ i ] =
- osp ( anBoard[ i ], nGames, an[ i ],
- aarProbs[ i ], aarGammonProbs[ i ] );
+ anTotal[ i ] = osp(anBoard[ i ], nGames, an[ i ], aarProbs[ i ], aarGammonProbs[ i ]);
/* calculate OUTPUT_WIN */