diff options
Diffstat (limited to 'ver3')
| -rw-r--r-- | ver3/ttt.c | 775 | ||||
| -rw-r--r-- | ver3/ttt.h | 79 |
2 files changed, 854 insertions, 0 deletions
diff --git a/ver3/ttt.c b/ver3/ttt.c new file mode 100644 index 0000000..426fc73 --- /dev/null +++ b/ver3/ttt.c @@ -0,0 +1,775 @@ +/*++
+
+Module Name:
+
+ ttt.c
+
+Abstract:
+
+ tic tac toe program to illustrate simple minimax searching
+
+Author:
+
+ Scott Gasch (SGasch) 18 Mar 2004
+
+Revision History:
+
+ ver0 : random play
+ ver1 : simple search
+ ver2 : alpha beta search
+ ver3 : added eval and depth on ab search, more efficient gaveover
+
+--*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <memory.h>
+#include <time.h>
+#include "ttt.h"
+
+SQUARE g_sComputerPlays = O_MARK; // what side comp plays
+unsigned int g_uPly = 0;
+MOVE g_mvBest = { 0 };
+unsigned int g_uNodes = 0;
+
+//+----------------------------------------------------------------------------
+//
+// Function: SquareContentsToChar
+//
+// Synopsis: Helper function for DrawBoard
+//
+// Arguments: IN SQUARE s - a square to return a char to represent
+//
+// Returns: char - character representing square
+//
+//+----------------------------------------------------------------------------
+char SquareContentsToChar(IN SQUARE s)
+{
+ static char c;
+ switch(s)
+ {
+ case X_MARK:
+ c = 'X';
+ break;
+ case O_MARK:
+ c = 'O';
+ break;
+ case EMPTY:
+ c = '_';
+ break;
+ default:
+ ASSERT(FALSE);
+ c = '?';
+ break;
+ }
+ return(c);
+}
+
+//+----------------------------------------------------------------------------
+//
+// Function: DrawBoard
+//
+// Synopsis: Draw the board
+//
+// Arguments: IN POSITION *p - pointer to a position whose board to draw
+//
+// Returns: void
+//
+//+----------------------------------------------------------------------------
+void DrawBoard(IN POSITION *p)
+{
+ COORD x, y;
+
+ for (y = 0; y < BOARD_SIZE; y++)
+ {
+ for (x = 0; x < BOARD_SIZE; x++)
+ {
+ printf("%c ", SquareContentsToChar(p->sBoard[y][x]));
+ }
+#ifdef DEBUG
+ printf(" = %d\n", p->iHSums[y]);
+#else
+ printf("\n");
+#endif
+ }
+
+#ifdef DEBUG
+ for (x = 0; x < BOARD_SIZE; x++)
+ {
+ printf("| ");
+ }
+ printf("\n");
+ for (x = 0; x < BOARD_SIZE; x++)
+ {
+ printf("%d ", p->iVSums[x]);
+ }
+ printf("\n");
+#endif
+
+ ASSERT(X_OR_O(p->sWhoseTurn));
+ printf("\n%c to move.\n", SquareContentsToChar(p->sWhoseTurn));
+}
+
+//+----------------------------------------------------------------------------
+//
+// Function: ClearBoard
+//
+// Synopsis: Clear the board
+//
+// Arguments: IN OUT POSITION *p - pointer to position whose board to clear
+//
+// Returns: void
+//
+//+----------------------------------------------------------------------------
+void ClearBoard(IN OUT POSITION *p)
+{
+ memset(p->sBoard, 0, sizeof(p->sBoard));
+ memset(p->iHSums, 0, sizeof(p->iHSums));
+ memset(p->iVSums, 0, sizeof(p->iVSums));
+ p->iDSums[0] = p->iDSums[1] = 0;
+ p->sWhoseTurn = X_MARK; // x's go first
+ p->uNumEmpty = (BOARD_SIZE * BOARD_SIZE);
+}
+
+//+----------------------------------------------------------------------------
+//
+// Function: IsLegalMove
+//
+// Synopsis: Determine if a given move is legal on a given board
+//
+// Arguments: IN POSITION *p - the board to play the move on
+// IN MOVE *m - the move in question
+//
+// Returns: BOOL - TRUE if it's legal, FALSE otherwise
+//
+//+----------------------------------------------------------------------------
+BOOL IsLegalMove(IN POSITION *p, IN MOVE *m)
+{
+ if ((m->cVpos < BOARD_SIZE) && (m->cHpos < BOARD_SIZE))
+ {
+ if (IS_SQUARE_EMPTY(p->sBoard[m->cVpos][m->cHpos]))
+ {
+ return(TRUE);
+ }
+ }
+ return(FALSE);
+}
+
+//+----------------------------------------------------------------------------
+//
+// Function: GetHumanMove
+//
+// Synopsis: Ask the human for a move
+//
+// Arguments: IN POSITION *p - the current board
+// OUT MOVE *m - the move the human made; this struct is populated
+// as a side-effect of this function.
+//
+// Returns: void* (populates the move struct)
+//
+//+----------------------------------------------------------------------------
+void GetHumanMove(IN POSITION *p, OUT MOVE *m)
+{
+ unsigned int x;
+
+ do
+ {
+ printf("Enter your move number: ");
+ scanf("%u", &x);
+
+ m->cHpos = NUM_TO_HPOS(x);
+ m->cVpos = NUM_TO_VPOS(x);
+ m->sMark = OPPOSITE_MARK(g_sComputerPlays);
+ }
+ while(FALSE == IsLegalMove(p, m));
+}
+
+
+//+----------------------------------------------------------------------------
+//
+// Function: GameOver
+//
+// Synopsis: Is the game over?
+//
+// Arguments: IN POSITION *p - the board
+// OUT SQUARE *psWhoWon - who won the game (if it's over)
+//
+// Returns: TRUE if the game is over. Also sets psWhoWon telling
+// which side one if the game is over. This also serves
+// as a very simple evaluation routine for the search.
+//
+// FALSE if the game is not over.
+//
+//+----------------------------------------------------------------------------
+BOOL GameOver(IN POSITION *p, OUT SQUARE *psWhoWon)
+{
+ int iSum;
+ COORD x;
+
+ for (x = 0; x < BOARD_SIZE; x++)
+ {
+ iSum = p->iHSums[x];
+ if (abs(iSum) == BOARD_SIZE) goto winner;
+
+ iSum = p->iVSums[x];
+ if (abs(iSum) == BOARD_SIZE) goto winner;
+ }
+
+ iSum = p->iDSums[0];
+ if (abs(iSum) == BOARD_SIZE) goto winner;
+
+ iSum = p->iDSums[1];
+ if (abs(iSum) == BOARD_SIZE) goto winner;
+
+ //
+ // No one won yet, either game ongoing or draw.
+ //
+ *psWhoWon = EMPTY;
+ if (p->uNumEmpty == 0)
+ {
+ return(TRUE);
+ }
+ else
+ {
+ return(FALSE);
+ }
+
+ winner:
+ //
+ // Some side won
+ //
+ *psWhoWon = (iSum / BOARD_SIZE);
+ ASSERT(X_OR_O(*psWhoWon));
+ return(TRUE);
+}
+
+
+//+----------------------------------------------------------------------------
+//
+// Function: CountAdjacents
+//
+// Synopsis: Return the number of marks in adjacent squares to square x
+// that are of the same type (x or o) as the mark in square x.
+//
+// FIXME: does not consider diagonals
+//
+// Arguments: IN POSITION *p - the board
+// IN SQUARE x - the square to test
+//
+// Returns: A count
+//
+//+----------------------------------------------------------------------------
+unsigned int CountAdjacents(IN POSITION *p, IN SQUARE x)
+{
+ COORD v = NUM_TO_VPOS(x);
+ COORD h = NUM_TO_HPOS(x);
+ SQUARE sSide = p->sBoard[h][v];
+ unsigned int uCount = 0;
+
+ //
+ // If nothing at square x, nothing to count
+ //
+ if (sSide == EMPTY) goto end;
+
+ //
+ // Look above, below, left and right
+ //
+ if ((v > 0) && (p->sBoard[h][v-1] == sSide))
+ {
+ uCount++;
+ }
+
+ if ((v < (BOARD_SIZE - 1)) && (p->sBoard[h][v+1] == sSide))
+ {
+ uCount++;
+ }
+
+ if ((h > 0) && (p->sBoard[h-1][v] == sSide))
+ {
+ uCount++;
+ }
+
+ if ((h < (BOARD_SIZE - 1)) && (p->sBoard[h+1][v] == sSide))
+ {
+ uCount++;
+ }
+
+ end:
+ ASSERT(0 <= uCount);
+ ASSERT(uCount <= 4);
+ return(uCount);
+}
+
+
+//+----------------------------------------------------------------------------
+//
+// Function: Eval
+//
+// Synopsis: Evaluate a position
+//
+// Arguments: IN POSITION *p - the board
+//
+// Returns: A score
+//
+//+----------------------------------------------------------------------------
+int Eval(IN POSITION *p)
+{
+ SQUARE sWinner;
+ COORD x;
+ SQUARE sMark;
+ int iScore = 0;
+
+ //
+ // See if the game is already over.
+ //
+ if (TRUE == GameOver(p, &sWinner))
+ {
+ if (sWinner == p->sWhoseTurn)
+ {
+ iScore = +INFINITY - g_uPly;
+ goto end;
+ }
+ else if (sWinner == OPPOSITE_MARK(p->sWhoseTurn))
+ {
+ iScore = -INFINITY + g_uPly;
+ goto end;
+ }
+ iScore = DRAWSCORE;
+ goto end;
+ }
+
+ //
+ // No one won but instead of returning score=0 see if we can
+ // find some "good characteristics" or "bad characteristics"
+ // of the position and give bonuses / penalties.
+ //
+ for (x = BOARD_SIZE + 1;
+ x < ((BOARD_SIZE * BOARD_SIZE) - BOARD_SIZE - 1);
+ x++)
+ {
+ sMark = p->sBoard[NUM_TO_HPOS(x)][NUM_TO_VPOS(x)];
+ if (sMark == p->sWhoseTurn)
+ {
+ iScore += CountAdjacents(p, x);
+ }
+ else if (sMark == OPPOSITE_MARK(p->sWhoseTurn))
+ {
+ iScore -= CountAdjacents(p, x);
+ }
+ }
+
+ end:
+ ASSERT(-INFINITY <= iScore);
+ ASSERT(iScore <= +INFINITY);
+ return(iScore);
+}
+
+
+
+//+----------------------------------------------------------------------------
+//
+// Function: MakeMove
+//
+// Synopsis: Make a move on a board
+//
+// Arguments: IN OUT POSITION *p - the board
+// IN MOVE *m - the move
+//
+// Returns: void
+//
+//+----------------------------------------------------------------------------
+void MakeMove(IN OUT POSITION *p, IN MOVE *m)
+{
+ if (TRUE == IsLegalMove(p, m))
+ {
+ //
+ // Make the new make on the board
+ //
+ ASSERT(p->sBoard[m->cVpos][m->cHpos] == EMPTY);
+ p->sBoard[m->cVpos][m->cHpos] = m->sMark;
+
+ //
+ // One less empty square
+ //
+ p->uNumEmpty--;
+ ASSERT(p->uNumEmpty < (BOARD_SIZE * BOARD_SIZE));
+
+ //
+ // Update sums as appropriate
+ //
+ p->iHSums[m->cHpos] += m->sMark;
+ ASSERT(VALID_SUM(p->iHSums[m->cHpos]));
+ p->iVSums[m->cVpos] += m->sMark;
+ ASSERT(VALID_SUM(p->iVSums[m->cVpos]));
+ if (m->cHpos == m->cVpos)
+ {
+ p->iDSums[0] += m->sMark;
+ ASSERT(VALID_SUM(p->iDSums[0]));
+ }
+ else if (m->cHpos == (BOARD_SIZE - m->cVpos))
+ {
+ p->iDSums[1] += m->sMark;
+ ASSERT(VALID_SUM(p->iDSums[1]));
+ }
+
+ //
+ // Other guy's turn
+ //
+ p->sWhoseTurn = OPPOSITE_MARK(p->sWhoseTurn);
+ ASSERT(X_OR_O(p->sWhoseTurn));
+
+ //
+ // One ply deeper
+ //
+ g_uPly++;
+ ASSERT(g_uPly > 0);
+ }
+}
+
+//+----------------------------------------------------------------------------
+//
+// Function: UnmakeMove
+//
+// Synopsis: The opposite of MakeMove
+//
+// Arguments: IN OUT POSITION *p - the board
+// IN MOVE *m - the move to undo
+//
+// Returns: void
+//
+//+----------------------------------------------------------------------------
+void UnmakeMove(IN OUT POSITION *p, IN MOVE *m)
+{
+ if (p->sBoard[m->cVpos][m->cHpos] == m->sMark)
+ {
+ p->sBoard[m->cVpos][m->cHpos] = EMPTY;
+
+ //
+ // One more empty square
+ //
+ p->uNumEmpty++;
+ ASSERT(p->uNumEmpty > 0);
+ ASSERT(p->uNumEmpty <= (BOARD_SIZE * BOARD_SIZE));
+
+ //
+ // Update sums as appropriate
+ //
+ p->iHSums[m->cHpos] -= m->sMark;
+ ASSERT(VALID_SUM(p->iHSums[m->cHpos]));
+ p->iVSums[m->cVpos] -= m->sMark;
+ ASSERT(VALID_SUM(p->iVSums[m->cVpos]));
+ if (m->cHpos == m->cVpos)
+ {
+ p->iDSums[0] -= m->sMark;
+ ASSERT(VALID_SUM(p->iDSums[0]));
+ }
+ else if (m->cHpos == (BOARD_SIZE - m->cVpos))
+ {
+ p->iDSums[1] -= m->sMark;
+ ASSERT(VALID_SUM(p->iDSums[1]));
+ }
+
+ //
+ // Other guy's turn
+ //
+ p->sWhoseTurn = OPPOSITE_MARK(p->sWhoseTurn);
+ ASSERT(X_OR_O(p->sWhoseTurn));
+
+ //
+ // One ply deeper
+ //
+ ASSERT(g_uPly > 0);
+ g_uPly--;
+ }
+}
+
+
+//+----------------------------------------------------------------------------
+//
+// Function: AlphaBeta
+//
+// Synopsis: An AlphaBeta Search
+//
+// Arguments: IN OUT POSITION *p - the board
+// IN int iAlpha - the lower bound of the score window
+// IN int iBeta - the upper bound of the score window
+// IN int uDepth - search depth horizon
+//
+// Returns: int
+//
+//+----------------------------------------------------------------------------
+int
+AlphaBeta(IN POSITION *p, IN int iAlpha, IN int iBeta, IN unsigned int uDepth)
+{
+ SQUARE sWhoWon;
+ SQUARE s;
+ MOVE mv;
+ int iScore;
+ int iBestScore = -INFINITY;
+
+ g_uNodes++;
+
+ //
+ // Evaluate this position
+ //
+ if (TRUE == GameOver(p, &sWhoWon))
+ {
+ if (sWhoWon == p->sWhoseTurn)
+ {
+ return(+INFINITY - g_uPly);
+ }
+ else if (sWhoWon == (p->sWhoseTurn * -1))
+ {
+ return(-INFINITY + g_uPly);
+ }
+ return(DRAWSCORE);
+ }
+ else
+ {
+ if (uDepth == 0)
+ {
+ return(Eval(p));
+ }
+ }
+
+ //
+ // No one won, game is still going. Evaluate every
+ // possible move from here.
+ //
+ ASSERT(p->uNumEmpty > 0);
+ for (s = 0; s < (BOARD_SIZE * BOARD_SIZE); s++)
+ {
+ mv.cHpos = NUM_TO_HPOS(s);
+ mv.cVpos = NUM_TO_VPOS(s);
+ mv.sMark = p->sWhoseTurn;
+
+ if (IsLegalMove(p, &mv))
+ {
+ MakeMove(p, &mv);
+
+ iScore = -1 * AlphaBeta(p, -iBeta, -iAlpha, uDepth - 1);
+ ASSERT(-INFINITY <= iScore);
+ ASSERT(iScore <= +INFINITY);
+
+ UnmakeMove(p, &mv);
+
+ if (iScore >= iBeta)
+ {
+ return(iScore);
+ }
+
+ if (iScore > iBestScore)
+ {
+ iBestScore = iScore;
+
+ if (iScore > iAlpha)
+ {
+ iAlpha = iScore;
+
+ //
+ // If this is the ply 0 move, remember it.
+ //
+ if (g_uPly == 0)
+ {
+ g_mvBest = mv;
+ }
+ }
+ }
+ }
+ }
+ return(iBestScore);
+}
+
+
+//+----------------------------------------------------------------------------
+//
+// Function: SimpleSearch
+//
+// Synopsis: A Simple Search
+//
+// Arguments: IN OUT POSITION *p - the board
+//
+// Returns: int
+//
+//+----------------------------------------------------------------------------
+int
+SimpleSearch(IN POSITION *p)
+{
+ SQUARE sWhoWon;
+ SQUARE s;
+ MOVE mv;
+ int iScore;
+ int iBestScore = -INFINITY;
+
+ g_uNodes++;
+
+ //
+ // Evaluate this position
+ //
+ if (TRUE == GameOver(p, &sWhoWon))
+ {
+ if (sWhoWon == p->sWhoseTurn)
+ {
+ return(+INFINITY - g_uPly);
+ }
+ else if (sWhoWon == (p->sWhoseTurn * -1))
+ {
+ return(-INFINITY + g_uPly);
+ }
+ return(DRAWSCORE);
+ }
+
+ //
+ // No one won, game is still going. Evaluate every
+ // possible move from here.
+ //
+ ASSERT(p->uNumEmpty > 0);
+ for (s = 0; s < (BOARD_SIZE * BOARD_SIZE); s++)
+ {
+ mv.cHpos = NUM_TO_HPOS(s);
+ mv.cVpos = NUM_TO_VPOS(s);
+ mv.sMark = p->sWhoseTurn;
+
+ if (IsLegalMove(p, &mv))
+ {
+ MakeMove(p, &mv);
+
+ iScore = -1 * SimpleSearch(p);
+ if (iScore > iBestScore)
+ {
+ iBestScore = iScore;
+ if (g_uPly == 1)
+ {
+ g_mvBest = mv;
+ }
+ }
+ UnmakeMove(p, &mv);
+ }
+ }
+ return(iBestScore);
+}
+
+//+----------------------------------------------------------------------------
+//
+// Function: SearchForComputerMove
+//
+// Synopsis: Use our sophisticated search algorithm to find a computer
+// move
+//
+// Arguments: IN POSITION *p - the current board
+// OUT MOVE *m - the move the computer chooses; this move struct
+// is populated as a side-effect of this function.
+//
+// Returns: void* (populates move struct)
+//
+//+----------------------------------------------------------------------------
+void SearchForComputerMove(IN POSITION *p, OUT MOVE *m)
+{
+ unsigned int x;
+
+#if defined(PLAY_RANDOMLY)
+
+ do
+ {
+ x = rand() % (BOARD_SIZE * BOARD_SIZE);
+ m->cHpos = NUM_TO_HPOS(x);
+ m->cVpos = NUM_TO_VPOS(x);
+ m->sMark = g_sComputerPlays;
+ }
+ while(FALSE == IsLegalMove(p, m));
+
+#elif defined(SIMPLE_SEARCH)
+
+ g_uPly = g_uNodes = 0;
+ SimpleSearch(p);
+ *m = g_mvBest;
+ printf("Searched %u node(s).\n", g_uNodes);
+
+#elif defined(ALPHA_BETA_SEARCH)
+
+ g_uPly = g_uNodes = 0;
+ AlphaBeta(p, -INFINITY-1, +INFINITY+1, BOARD_SIZE);
+ *m = g_mvBest;
+ printf("Searched %u node(s).\n", g_uNodes);
+
+#else
+
+ #error "No Search Strategy Defined"
+
+#endif
+}
+
+//+----------------------------------------------------------------------------
+//
+// Function: main
+//
+// Synopsis: The program entry point and main game loop.
+//
+// Arguments: void
+//
+// Returns: int
+//
+//+----------------------------------------------------------------------------
+int main(void)
+{
+ POSITION p;
+ MOVE mv;
+ SQUARE sResult;
+
+ //
+ // Randomize: the random numbers returned by rand() will be based on
+ // the system clock when the program starts up.
+ //
+ srand(time(0));
+
+ //
+ // Setup the board and draw it once.
+ //
+ ClearBoard(&p);
+ DrawBoard(&p);
+
+ //
+ // Main game loop
+ //
+ do
+ {
+ //
+ // See whose turn it is -- the human's or the computers -- and
+ // get a move from whoever's turn it is.
+ //
+ if (p.sWhoseTurn == g_sComputerPlays)
+ {
+ SearchForComputerMove(&p, &mv);
+ }
+ else
+ {
+ GetHumanMove(&p, &mv);
+ }
+
+ //
+ // Make the move on the board and draw the board again.
+ //
+ MakeMove(&p, &mv);
+ DrawBoard(&p);
+ }
+ while(FALSE == GameOver(&p, &sResult));
+
+ //
+ // If we get here the game is over... see what happened.
+ //
+ switch(sResult)
+ {
+ case X_MARK:
+ printf("\nX's win.\n");
+ break;
+ case O_MARK:
+ printf("\nO's win.\n");
+ break;
+ default:
+ printf("Tie (what a surprise)\n");
+ break;
+ }
+
+ exit(0);
+}
+
diff --git a/ver3/ttt.h b/ver3/ttt.h new file mode 100644 index 0000000..115e5b9 --- /dev/null +++ b/ver3/ttt.h @@ -0,0 +1,79 @@ +#ifndef TTT_H_ +#define TTT_H_ + +#define ALPHA_BETA_SEARCH 1 + +#define TRUE (1) +#define FALSE (0) + +#define IN +#define OUT + +typedef unsigned int BOOL; + +// +// Constants for each state a board square can be in and a programmer +// defined type for these states. +// +#define X_MARK (-1) +#define EMPTY (0) +#define O_MARK (+1) +#define TIE (+2) +#define OPPOSITE_MARK(m) ((m) * -1) +#define X_OR_O(m) (((m) == X_MARK) || \ + ((m) == O_MARK)) + +typedef signed char SQUARE; +#define IS_SQUARE_EMPTY(x) (x == EMPTY) + +// +// A (simple) representation of a tic tac toe position +// +#define BOARD_SIZE (5) + +typedef struct _POSITION +{ + SQUARE sWhoseTurn; + SQUARE sBoard[BOARD_SIZE][BOARD_SIZE]; + int iVSums[BOARD_SIZE]; + int iHSums[BOARD_SIZE]; + int iDSums[2]; + unsigned int uNumEmpty; +} POSITION; + +#define NUM_TO_HPOS(x) ((x) % BOARD_SIZE) +#define NUM_TO_VPOS(x) ((x) / BOARD_SIZE) +#define VALID_SUM(x) (((x) <= BOARD_SIZE) && \ + ((x) >= -BOARD_SIZE)) + +// +// A representation of a move in a tic tac toe game +// +typedef unsigned int COORD; + +typedef struct _MOVE +{ + COORD cHpos; + COORD cVpos; + SQUARE sMark; +} MOVE; + +// +// Score values +// +#define INFINITY (+100) +#define DRAWSCORE (0) + +// +// An assert mechanism +// +#ifdef DEBUG +#define ASSERT(x) if (x) \ + { ; } \ + else \ + { (void) _assert(__FILE__, __LINE__); } +#else +#define ASSERT(x) ; +#endif /* DEBUG */ + +#endif /* TTT_H_ */ |
