7 SQUARE g_sComputerPlays = O_MARK; // what side comp plays
8 unsigned int g_uPly = 0;
11 //+----------------------------------------------------------------------------
13 // Function: SquareContentsToChar
15 // Synopsis: Helper function for DrawBoard
17 // Arguments: IN SQUARE s - a square to return a char to represent
19 // Returns: char - character representing square
21 //+----------------------------------------------------------------------------
22 char SquareContentsToChar(IN SQUARE s)
44 //+----------------------------------------------------------------------------
46 // Function: DrawBoard
48 // Synopsis: Draw the board
50 // Arguments: IN POSITION *p - pointer to a position whose board to draw
54 //+----------------------------------------------------------------------------
55 void DrawBoard(IN POSITION *p)
59 for (y = 0; y < BOARD_SIZE; y++)
61 for (x = 0; x < BOARD_SIZE; x++)
63 printf("%c ", SquareContentsToChar(p->sBoard[y][x]));
67 ASSERT(X_OR_O(p->sWhoseTurn));
68 printf("\n%c to move.\n", SquareContentsToChar(p->sWhoseTurn));
71 //+----------------------------------------------------------------------------
73 // Function: ClearBoard
75 // Synopsis: Clear the board
77 // Arguments: IN OUT POSITION *p - pointer to position whose board to clear
81 //+----------------------------------------------------------------------------
82 void ClearBoard(IN OUT POSITION *p)
84 memset(p->sBoard, 0, sizeof(p->sBoard));
85 p->sWhoseTurn = X_MARK; // x's go first
86 p->uNumEmpty = (BOARD_SIZE * BOARD_SIZE);
89 //+----------------------------------------------------------------------------
91 // Function: IsLegalMove
93 // Synopsis: Determine if a given move is legal on a given board
95 // Arguments: IN POSITION *p - the board to play the move on
96 // IN MOVE *m - the move in question
98 // Returns: BOOL - TRUE if it's legal, FALSE otherwise
100 //+----------------------------------------------------------------------------
101 BOOL IsLegalMove(IN POSITION *p, IN MOVE *m)
103 if ((m->cVpos < BOARD_SIZE) && (m->cHpos < BOARD_SIZE))
105 if (IS_SQUARE_EMPTY(p->sBoard[m->cVpos][m->cHpos]))
113 //+----------------------------------------------------------------------------
115 // Function: GetHumanMove
117 // Synopsis: Ask the human for a move
119 // Arguments: IN POSITION *p - the current board
120 // OUT MOVE *m - the move the human made; this struct is populated
121 // as a side-effect of this function.
123 // Returns: void* (populates the move struct)
125 //+----------------------------------------------------------------------------
126 void GetHumanMove(IN POSITION *p, OUT MOVE *m)
132 printf("Enter your move number: ");
135 m->cHpos = NUM_TO_HPOS(x);
136 m->cVpos = NUM_TO_VPOS(x);
137 m->sMark = OPPOSITE_MARK(g_sComputerPlays);
139 while(FALSE == IsLegalMove(p, m));
142 //+----------------------------------------------------------------------------
144 // Function: GameOver
146 // Synopsis: Is the game over?
148 // Arguments: IN POSITION *p - the board
149 // OUT SQUARE *psWhoWon - who won the game (if it's over)
151 // Returns: TRUE if the game is over. Also sets psWhoWon telling
152 // which side one if the game is over. This also serves
153 // as a very simple evaluation routine for the search.
155 // FALSE if the game is not over.
157 //+----------------------------------------------------------------------------
158 BOOL GameOver(IN POSITION *p, OUT SQUARE *psWhoWon)
163 for (x = 0; x < BOARD_SIZE; x++)
167 for (y = 0; y < BOARD_SIZE; y++)
169 iSum += p->sBoard[x][y];
171 if (abs(iSum) == BOARD_SIZE) goto winner;
174 for (y = 0; y < BOARD_SIZE; y++)
178 for (x = 0; x < BOARD_SIZE; x++)
180 iSum += p->sBoard[x][y];
182 if (abs(iSum) == BOARD_SIZE) goto winner;
186 for (x = 0; x < BOARD_SIZE; x++)
188 iSum += p->sBoard[x][x];
190 if (abs(iSum) == BOARD_SIZE) goto winner;
193 for (x = 0; x < BOARD_SIZE; x++)
195 iSum += p->sBoard[x][(BOARD_SIZE - 1 - x)];
197 if (abs(iSum) == BOARD_SIZE) goto winner;
200 if (p->uNumEmpty == 0)
210 *psWhoWon = (iSum / BOARD_SIZE);
211 ASSERT(X_OR_O(*psWhoWon));
216 //+----------------------------------------------------------------------------
218 // Function: MakeMove
220 // Synopsis: Make a move on a board
222 // Arguments: IN OUT POSITION *p - the board
223 // IN MOVE *m - the move
227 //+----------------------------------------------------------------------------
228 void MakeMove(IN OUT POSITION *p, IN MOVE *m)
230 if (TRUE == IsLegalMove(p, m))
232 ASSERT(p->sBoard[m->cVpos][m->cHpos] == EMPTY);
233 p->sBoard[m->cVpos][m->cHpos] = m->sMark;
235 ASSERT(p->uNumEmpty < (BOARD_SIZE * BOARD_SIZE));
236 p->sWhoseTurn = OPPOSITE_MARK(p->sWhoseTurn);
242 //+----------------------------------------------------------------------------
244 // Function: UnmakeMove
246 // Synopsis: The opposite of MakeMove
248 // Arguments: IN OUT POSITION *p - the board
249 // IN MOVE *m - the move to undo
253 //+----------------------------------------------------------------------------
254 void UnmakeMove(IN OUT POSITION *p, IN MOVE *m)
256 if (p->sBoard[m->cVpos][m->cHpos] == m->sMark)
258 p->sBoard[m->cVpos][m->cHpos] = EMPTY;
260 ASSERT(p->uNumEmpty > 0);
261 ASSERT(p->uNumEmpty <= (BOARD_SIZE * BOARD_SIZE));
262 p->sWhoseTurn = OPPOSITE_MARK(p->sWhoseTurn);
270 SimpleSearch(IN POSITION *p)
276 int iBestScore = -INFINITY;
279 // Evaluate this position
281 if (TRUE == GameOver(p, &sWhoWon))
283 if (sWhoWon == p->sWhoseTurn)
287 else if (sWhoWon == (p->sWhoseTurn * -1))
295 // No one won, game is still going. Evaluate every
296 // possible move from here.
298 ASSERT(p->uNumEmpty > 0);
299 for (s = 0; s < (BOARD_SIZE * BOARD_SIZE); s++)
301 mv.cHpos = NUM_TO_HPOS(s);
302 mv.cVpos = NUM_TO_VPOS(s);
303 mv.sMark = p->sWhoseTurn;
305 if (IsLegalMove(p, &mv))
309 iScore = -1 * SimpleSearch(p);
310 if (iScore > iBestScore)
325 //+----------------------------------------------------------------------------
327 // Function: SearchForComputerMove
329 // Synopsis: Use our sophisticated search algorithm to find a computer
332 // Arguments: IN POSITION *p - the current board
333 // OUT MOVE *m - the move the computer chooses; this move struct
334 // is populated as a side-effect of this function.
336 // Returns: void* (populates move struct)
338 //+----------------------------------------------------------------------------
339 void SearchForComputerMove(IN POSITION *p, OUT MOVE *m)
343 #if defined(PLAY_RANDOMLY)
346 x = rand() % (BOARD_SIZE * BOARD_SIZE);
347 m->cHpos = NUM_TO_HPOS(x);
348 m->cVpos = NUM_TO_VPOS(x);
349 m->sMark = g_sComputerPlays;
351 while(FALSE == IsLegalMove(p, m));
352 #elif defined(SIMPLE_SEARCH)
357 #error "No Search Strategy Defined"
361 //+----------------------------------------------------------------------------
365 // Synopsis: The program entry point and main game loop.
371 //+----------------------------------------------------------------------------
379 // Randomize: the random numbers returned by rand() will be based on
380 // the system clock when the program starts up.
385 // Setup the board and draw it once.
396 // See whose turn it is -- the human's or the computers -- and
397 // get a move from whoever's turn it is.
399 if (p.sWhoseTurn == g_sComputerPlays)
401 SearchForComputerMove(&p, &mv);
405 GetHumanMove(&p, &mv);
409 // Make the move on the board and draw the board again.
414 while(FALSE == GameOver(&p, &sResult));
417 // If we get here the game is over... see what happened.
422 printf("\nX's win.\n");
425 printf("\nO's win.\n");
428 printf("Tie (what a surprise)\n");