Class List

Here are the classes, structs, unions and interfaces with brief descriptions:
Game
Player
Generated by doxygen 1.6.2


Game Struct Reference

#include <TicTacToe.h>

List of all members.

Public Attributes

char | board [9]
int | nextPlayer
void * | players [2]
enum PLUGIN_GAME_STATUS | status

Detailed Description

Definition at line 3 of file TicTacToe.h.

Member Data Documentation

char Game::board[9]

Definition at line 5 of file TicTacToe.h.

int Game::nextPlayer

Definition at line 6 of file TicTacToe.h.

void* Game::players[2]

Definition at line 7 of file TicTacToe.h.

enum PLUGIN_GAME_STATUS Game::status

Definition at line 8 of file TicTacToe.h.

The documentation for this struct was generated from the following file:
  • TicTacToe.h


Player Struct Reference

List of all members.

Public Attributes

int dummy

Detailed Description

Definition at line 40 of file random-ttt.c.

Member Data Documentation

int Player::dummy

Definition at line 36 of file random-ttt.c.

The documentation for this struct was generated from the following file:
  • random-ttt.c


Class Members

  • Main Page
  • Classes
  • Files
  • Class List
  • Class Members
  • All
  • Variables
Here is a list of all class members with links to the classes they belong to:
  • board : Game
  • dummy : Player
  • nextPlayer : Game
  • players : Game
  • status : Game


File Index

  • File List
  • File Members

File List

Here is a list of all files with brief descriptions:
PluginCommon.h [code] | Common information for DIDCGAF plugins
PluginInterface.h [code] | Common interface for all DIDCGAF plugins
random-ttt.c [code] | Test driver for the TicTacToe plugin
TicTacToe.c [code] | Implementation of Tic-Tac-Toe
TicTacToe.h [code] |


PluginCommon.h File Reference

Common information for DIDCGAF plugins. More...

Go to the source code of this file.

Enumerations

enum PLUGIN_GAME_STATUS {
PLUGIN_GAME_STATUS_ERROR, PLUGIN_GAME_STATUS_CREATED, PLUGIN_GAME_STATUS_PLAYING, PLUGIN_GAME_STATUS_PAUSED,
PLUGIN_GAME_STATUS_OVER_DRAW, PLUGIN_GAME_STATUS_OVER_WINNER
}

Detailed Description

Common information for DIDCGAF plugins.

Author:
Nathan Williams
email: nwilliam@digipen.edu
Date:
04/10/2010

Definition in file PluginCommon.h.

Enumeration Type Documentation

enum PLUGIN_GAME_STATUS
Enumerator:
PLUGIN_GAME_STATUS_ERROR
PLUGIN_GAME_STATUS_CREATED
PLUGIN_GAME_STATUS_PLAYING
PLUGIN_GAME_STATUS_PAUSED
PLUGIN_GAME_STATUS_OVER_DRAW
PLUGIN_GAME_STATUS_OVER_WINNER

Definition at line 12 of file PluginCommon.h.

00013 {
00014   PLUGIN_GAME_STATUS_ERROR,
00015   PLUGIN_GAME_STATUS_CREATED,
00016   PLUGIN_GAME_STATUS_PLAYING,
00017   PLUGIN_GAME_STATUS_PAUSED,
00018   PLUGIN_GAME_STATUS_OVER_DRAW,
00019   PLUGIN_GAME_STATUS_OVER_WINNER
00020 };


PluginInterface.h File Reference

Common interface for all DIDCGAF plugins. More...

Go to the source code of this file.

Functions

int | Init (void)
void | Shutdown (void)
const char * | Name (void)
const char * | Author (void)
const char * | Type (void)
const char * | Version (void)
const char * | APIVersion (void)
int | NumberOfPlayers (void)
void * | CreateGame (void)
int | SetGameOption (void *game, const char *option)
int | AddPlayer (void *game, void *player)
int | RemovePlayer (void *game, void *player)
void * | GetPlayer (void *game, int playerNumber)
int | GetPlayerNumber (void *game, void *player)
int | PlayerCount (void *game)
int | StartGame (void *game)
void * | GetNextPlayer (void *game)
int | ValidateMove (void *game, void *player, const char *move)
int | GetGameStatus (void *game)
void | DestroyGame (void *game)

Detailed Description

Common interface for all DIDCGAF plugins.

Author:
Nathan Williams
email: nwilliam@digipen.edu
Date:
04/05/2010

Definition in file PluginInterface.h.

Function Documentation

int AddPlayer | ( | void * | game,
 |  | void * | player
 | ) |  |

Definition at line 85 of file TicTacToe.c.

00086 {
00087   struct Game* game = (struct Game *)igame;
00088
00089   if(game->players[0] == NULL)
00090     game->players[0] = player;
00091   else if(game->players[1] == NULL)
00092     game->players[1] = player;
00093   else
00094     return 1;
00095
00096   return 0;
00097 }

const char* APIVersion ( void )

Definition at line 58 of file TicTacToe.c.

00059 {
00060   return "0.1";
00061 }

const char* Author ( void )

Definition at line 43 of file TicTacToe.c.

00044 {
00045   return "nlw";
00046 }

void* CreateGame ( void )

Definition at line 68 of file TicTacToe.c.

00069 {
00070   struct Game* newGame = (struct Game *)calloc(1, sizeof(struct Game));
00071   if(newGame)
00072     newGame->status = PLUGIN_GAME_STATUS_CREATED;
00073   return newGame;
00074 }

void DestroyGame ( void * game )

Definition at line 229 of file TicTacToe.c.

00230 {
00231   free(igame);
00232 }

int GetGameStatus ( void * game )

Definition at line 223 of file TicTacToe.c.

00224 {
00225   struct Game* game = (struct Game *)igame;
00226   return game->status;
00227 }

void* GetNextPlayer ( void * game )

Definition at line 158 of file TicTacToe.c.

00159 {
00160   struct Game* game = (struct Game *)igame;
00161
00162   if(game->nextPlayer > 0)
00163     return game->players[game->nextPlayer-1];
00164
00165   return NULL;
00166 }

void* GetPlayer | ( | void * | game,
 |  | int | playerNumber
 | ) |  |

Definition at line 122 of file TicTacToe.c.

00123 {
00124   struct Game* game = (struct Game *)igame;
00125
00126   if(playerNumber == 1 || playerNumber == 2)
00127     return game->players[playerNumber-1];
00128
00129   return NULL;
00130 }

int GetPlayerNumber | ( | void * | game,
 |  | void * | player
 | ) |  |

Definition at line 132 of file TicTacToe.c.

00133 {
00134   struct Game* game = (struct Game *)igame;
00135
00136   if(game->players[0] == player)
00137     return 1;
00138
00139   if(game->players[1] == player)
00140     return 2;
00141
00142   return 0;
00143 }

int Init ( void )

Definition at line 28 of file TicTacToe.c.

00029 {
00030   /* Nothing to do */
00031   return 0;
00032 }

const char* Name ( void )

Definition at line 38 of file TicTacToe.c.

00039 {
00040   return "Tic-Tac-Toe";
00041 }

int NumberOfPlayers ( void )

Definition at line 63 of file TicTacToe.c.

00064 {
00065   return 2;
00066 }

int PlayerCount ( void * game )

Definition at line 116 of file TicTacToe.c.

00117 {
00118   struct Game* game = (struct Game *)igame;
00119   return (game->players[0] != 0) + (game->players[1] != 0);
00120 }

int RemovePlayer | ( | void * | game,
 |  | void * | player
 | ) |  |

Definition at line 99 of file TicTacToe.c.

00100 {
00101   struct Game* game = (struct Game *)igame;
00102
00103   if(game->players[0] == player)
00104     game->players[0] = NULL;
00105   else if(game->players[1] == player)
00106     game->players[1] = NULL;
00107   else
00108     return 1;
00109
00110   if(game->status == PLUGIN_GAME_STATUS_PLAYING)
00111     game->status = PLUGIN_GAME_STATUS_PAUSED;
00112
00113   return 0;
00114 }

int SetGameOption | ( | void * | game,
 |  | const char * | option
 | ) |  |

Definition at line 76 of file TicTacToe.c.

00077 {
00078   /* No options for this game */
00079   (void)igame;
00080   (void)option;
00081
00082   return 1;
00083 }

void Shutdown ( void )

Definition at line 34 of file TicTacToe.c.

00035 {
00036 }

int StartGame ( void * game )

Definition at line 145 of file TicTacToe.c.

00146 {
00147   struct Game* game = (struct Game *)igame;
00148
00149   if(PlayerCount(game) != 2)
00150     return 1;
00151
00152   game->nextPlayer = 1;
00153   game->status = PLUGIN_GAME_STATUS_PLAYING;
00154
00155   return 0;
00156 }

const char* Type ( void )

Definition at line 48 of file TicTacToe.c.

00049 {
00050   return "TicTacToe";
00051 }

int ValidateMove | ( | void * | game,
 |  | void * | player,
 |  | const char * | move
 | ) |  |

Definition at line 168 of file TicTacToe.c.

00169 {
00170   struct Game* game = (struct Game *)igame;
00171   char* board = game->board;
00172   int i, movePos;
00173
00174   int wins[8][3] = { {0,1,2},  {3,4,5},  {6,7,8},     /* Horizontal */
00175                      {0,3,6},  {1,4,7},  {2,5,8},     /* Vertical */
00176                      {0,4,8},  {2,4,6}            };  /* Diagonal */
00177
00178                 /* Sanity check */
00179   if(move == NULL || strlen(move) != 1 || game->status != PLUGIN_GAME_STATUS_PLAYING)
00180     return -1;
00181
00182   movePos = move[0] - '0';
00183
00184   /* Check out of range or already played */
00185   if(movePos < 0 || movePos > 8 || board[movePos] != 0)
00186     return -1;
00187
00188   if(game->players[0] == player)
00189   {
00190     board[movePos] = 'X';
00191     game->nextPlayer = 2;
00192   }
00193   else
00194   {
00195     board[movePos] = 'O';
00196     game->nextPlayer = 1;
00197   }
00198
00199   /* Check for winner or draw */
00200   for(i = 0; i < 8; ++i)
00201   {
00202     if(board[wins[i][0]] == board[wins[i][1]] &&
00203        board[wins[i][0]] == board[wins[i][2]] &&
00204        board[wins[i][0]] != 0)
00205     {
00206       game->nextPlayer = GetPlayerNumber(igame, player);
00207       game->status = PLUGIN_GAME_STATUS_OVER_WINNER;
00208       return 0;
00209     }
00210   }
00211
00212   /* Check for draw */
00213   if(board[0] && board[1] && board[2] && board[3] && board[4] &&
00214      board[5] && board[6] && board[7] && board[8])
00215   {
00216     game->nextPlayer = 0;
00217     game->status = PLUGIN_GAME_STATUS_OVER_DRAW;
00218   }
00219
00220   return 0;
00221 }

const char* Version ( void )

Definition at line 53 of file TicTacToe.c.

00054 {
00055   return "0.1";
00056 }


random-ttt.c File Reference

Test driver for the TicTacToe plugin. More...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "TicTacToe.h"
#include "PluginInterface.h"

Go to the source code of this file.

Classes

struct | Player

Functions

static int | RandomInt (int low, int high)
static int | GetNextMove (void)
static void | ClearBoard (void)
static void | DumpBoard (void)
static void | AddOutput (const char *str, int newline)
static int | driver_CreateGame (void)
static int | driver_JoinGame (void)
static int | driver_StartGame (void)
static void | driver_DestroyGame (void)
static int | NewGame (void)
static int | init (int argc, char **argv)
static void | CalcGameOverStats (int movenum)
static void | DisplayGrandTotals (void)
int | main (int argc, char **argv)

Variables

static struct Game * | gGame = 0
static int | gSilent = 1
static int | gVerbose = 0
static int | gPlayer = 1
static int | gOther = 2
static int | gCount = 1
static int | gWins [3] = {0}
static int | gLosses [3] = {0}
static int | gDraws = 0
static int | gGamesPlayed = 0
static const char | EMPTY_SQUARE = '.'
static const char | SYMBOLS [] = {' ', 'X', 'O'}
static int | gWinsMovesX [10] = {0}
static int | gWinsMovesO [10] = {0}
static void * | gPlayers [2] = {0}
static char | Board [9] = {0}

Detailed Description

Test driver for the TicTacToe plugin.

Author:
Matthew Mead
email: mmead@digipen.edu
Date:
01/16/2010 This test driver interfaces with the TicTacToe plugin directly. It does not require networking or a server. The plugin is the same code that the server uses so this should test the client exactly like the server would. The benefit of this is that this client can play about 1,000,000 games per second.

To run this program from the command line:

./random-ttt {count}

where count is the number of games to play. For example:

./random-ttt 10000

will play 10,000 games of Tic-Tac-Toe and display the statistics. Look at the Options below to enable/disable more/less output.

Currently, this program always generates the same set of random numbers each time it is run. To enable randomness between runs, uncomment the call to srand() in the main() function at the bottom of this file.

Definition in file random-ttt.c.

Function Documentation

static void AddOutput | ( | const char * | str,
 |  | int | newline |
 | ) |  |  | [static]

Definition at line 141 of file random-ttt.c.

00142 {
00143   printf("%s", str);
00144   if (newline)
00145     printf("\n");
00146
00147   fflush(stdout);
00148 }

static void CalcGameOverStats ( int movenum ) [static]

Definition at line 323 of file random-ttt.c.

00324 {
00325   int winner = GetPlayerNumber(gGame, GetNextPlayer(gGame));
00326   char buffer[100];
00327
00328   if (winner == 0)
00329   {
00330     sprintf(buffer, "The game is a draw!");
00331   }
00332   else
00333     sprintf(buffer, "Game over: Player %i is the winner after %i moves!", winner, movenum);
00334
00335   if (gVerbose)
00336     AddOutput(buffer, 1);
00337
00338   if (winner == 0)
00339     gDraws++;
00340   else if (winner == gPlayer)
00341   {
00342     gWins[gPlayer]++;
00343     gLosses[gOther]++;
00344   }
00345   else
00346   {
00347     gWins[gOther]++;
00348     gLosses[gPlayer]++;
00349   }
00350
00351   if (winner == 1)
00352     gWinsMovesX[movenum]++;
00353   else if (winner == 2)
00354     gWinsMovesO[movenum]++;
00355
00356   if (!gSilent)
00357   {
00358     printf("Wins - Player 1:%i, Player 2:%i, Draws: %i, Games Played:%i, Games Left:%i\n",
00359             gWins[1], gWins[2], gDraws, gGamesPlayed, gCount - gGamesPlayed);
00360   }
00361 }

static void ClearBoard ( void ) [static]

Definition at line 114 of file random-ttt.c.

00115 {
00116   int i;
00117   for (i = 0; i < 9; i++)
00118     Board[i] = 0;
00119 }

static void DisplayGrandTotals ( void ) [static]

Definition at line 363 of file random-ttt.c.

00364 {
00365   int i;
00366
00367   printf("Grand Total:\n");
00368   printf("Wins - Player 1:%i, Player 2:%i, Draws: %i, Games Played:%i, Games Left:%i\n",
00369           gWins[1], gWins[2], gDraws, gGamesPlayed, gCount - gGamesPlayed);
00370
00371   printf("Player 1 won this many games on move number: ");
00372   for (i = 5; i <= 9; i++)
00373     printf("%i:%i   ", i, gWinsMovesX[i]);
00374   printf("\n");
00375
00376   printf("Player 2 won this many games on move number: ");
00377   for (i = 5; i <= 9; i++)
00378     printf("%i:%i   ", i, gWinsMovesO[i]);
00379   printf("\n");
00380 }

static int driver_CreateGame ( void ) [static]

Definition at line 153 of file random-ttt.c.

00154 {
00155   struct Player *player = NULL;
00156
00157   if (gVerbose)
00158     AddOutput("Creating game...", 1);
00159
00160   gGame = (struct Game *)CreateGame();
00161   if (!gGame)
00162   {
00163     AddOutput("Error: Can't create game.", 1);
00164     return 0;
00165   }
00166
00167   player = malloc(sizeof (struct Player));
00168   if (AddPlayer(gGame, player))
00169   {
00170     AddOutput("Error: Can't add player 1.", 1);
00171     DestroyGame(gGame);
00172     free(player);
00173     return 0;
00174   }
00175
00176   gPlayers[0] = player;
00177   return 1;
00178 }

static void driver_DestroyGame ( void ) [static]

Definition at line 253 of file random-ttt.c.

00254 {
00255   if (gVerbose)
00256     AddOutput("Destroying game...", 1);
00257
00258   DestroyGame(gGame);
00259   free(gPlayers[0]);
00260   free(gPlayers[1]);
00261 }

static int driver_JoinGame ( void ) [static]

Definition at line 183 of file random-ttt.c.

00184 {
00185   struct Player *player;
00186   if (gVerbose)
00187   {
00188     AddOutput("Joining game with player 2", 1);
00189   }
00190
00191   if (!gGame)
00192   {
00193     AddOutput("Error: Can't join a game: No game created.", 1);
00194     return 0;
00195   }
00196
00197   player = malloc(sizeof(struct Player));
00198   if (AddPlayer(gGame, player))
00199   {
00200     AddOutput("Error: Can't add player 2.", 1);
00201     free(player);
00202     return 0;
00203   }
00204
00205   gPlayers[1] = player;
00206   return 1;
00207 }

static int driver_StartGame ( void ) [static]

Definition at line 215 of file random-ttt.c.

00216 {
00217   int count;
00218
00219   if (gVerbose)
00220     AddOutput("Starting game...", 1);
00221
00222   if (!gGame)
00223   {
00224     AddOutput("Error: Can't start a game: No game created.", 1);
00225     return 0;
00226   }
00227
00228     /* Sanity check */
00229   count = PlayerCount(gGame);
00230   if (count != 2)
00231   {
00232     printf("count = %i\n", count);
00233     AddOutput("Can't start game: requires 2 players.", 1);
00234     return 0;
00235   }
00236     /*
00237       This should always succeed because the only check the plugin makes
00238       is to see that there are 2 players in the game. We just checked that
00239       case above.
00240      */
00241   if (StartGame(gGame))
00242   {
00243     AddOutput("Error: Can't start game.", 1);
00244     return 0;
00245   }
00246
00247   return 1;
00248 }

static void DumpBoard ( void ) [static]

Definition at line 121 of file random-ttt.c.

00122 {
00123   /*char *board = gGame->board;*/
00124
00125     /* For each row */
00126   int i;
00127   for (i = 0; i < 9; i++)
00128   {
00129     int ch = Board[i];
00130     if ( !ch )
00131       printf("%2c", EMPTY_SQUARE);
00132     else
00133       printf("%2c", ch);
00134
00135     if (((i + 1) % 3) == 0)
00136       printf("\n");
00137   }
00138   printf("\n");
00139 }

static int GetNextMove ( void ) [static]

Definition at line 95 of file random-ttt.c.

00096 {
00097   int position;
00098
00099   /* Keep choosing a random square until a valid (e.g. empty) square is found */
00100   do
00101   {
00102     position = RandomInt(0, 8);
00103   }while (Board[position] != 0);
00104
00105   return position;
00106 }

static int init | ( | int | argc,
 |  | char ** | argv |
 | ) |  |  | [static]

Definition at line 292 of file random-ttt.c.

00293 {
00294   if (argc < 2)
00295   {
00296     printf("Usage: %s {count}\n", argv[0]);
00297     printf("\n");
00298     printf("where: count is the number of games to play.\n");
00299     return 0;
00300   }
00301   gCount = atoi(argv[1]);
00302
00303   if (gVerbose)
00304   {
00305     printf("Plugin information\n");
00306     printf("----------------------------\n");
00307     printf("       Name: %s\n", Name());
00308     printf("     Author: %s\n", Author());
00309     printf("       Type: %s\n", Type());
00310     printf("    Players: %i\n", NumberOfPlayers());
00311     printf("    Version: %s\n", Version());
00312     printf("API Version: %s\n", APIVersion());
00313     printf("----------------------------\n");
00314   }
00315
00316   return 1;
00317 }

int main | ( | int | argc,
 |  | char ** | argv
 | ) |  |

Definition at line 385 of file random-ttt.c.

00386 {
00387   int i;
00388
00389   if (!init(argc, argv))
00390     return 0;
00391
00392     /* Uncomment the call to srand to have randomness every time the program is run. */
00393     /* It's easier to debug a program that is repeatable each time. Once the         */
00394     /* program is correct, enable the randomness.                                    */
00395   /* srand(time(NULL)); */
00396
00397     /* Loop once for each game */
00398   for (i = 0; i < gCount; i++)
00399   {
00400     int i, movenum = 0;
00401
00402       /* Set up a new game each time */
00403     NewGame();
00404
00405       /* Player 1 always goes first (X's) */
00406     gPlayer = 1;
00407
00408       /* There are at most 9 moves in 3x3 Tic-Tac-Toe */
00409     for (i = 0; i < 9; i++)
00410     {
00411       int player;
00412       char buf[10];
00413
00414         /* GetNextMove() call will return the current player's (i.e. gPlayer) next move.  */
00415         /* The "intelligence" of this function determines the intelligence of the client. */
00416       int move = GetNextMove();
00417
00418         /* Set the move to the current player's symbol (i.e. 'X' or 'O') */
00419       Board[move] = SYMBOLS[gPlayer];
00420       movenum++;
00421
00422         /* Call the plugin to verify that the move is valid */
00423       sprintf(buf, "%i", move);
00424       ValidateMove(gGame, GetPlayer(gGame, gPlayer), buf);
00425
00426         /* Display board on the screen if we're debugging */
00427       if (gVerbose)
00428         DumpBoard();
00429
00430         /* Game ended with a winner or a draw */
00431       if (GetGameStatus(gGame) == PLUGIN_GAME_STATUS_OVER_WINNER || GetGameStatus(gGame) == PLUGIN_GAME_STATUS_OVER_DRAW)
00432       {
00433           /* Calculate and print statistics */
00434         CalcGameOverStats(movenum);
00435         break;
00436       }
00437
00438         /* Toggle next player */
00439       player = GetPlayerNumber(gGame, GetPlayer(gGame, gPlayer));
00440       gPlayer = (player == 1) ? 2 : 1;
00441       gOther = (gPlayer == 1) ? 2 : 1;
00442     }
00443     gGamesPlayed++;
00444     driver_DestroyGame();
00445   }
00446
00447   DisplayGrandTotals();
00448   return 0;
00449 }

static int NewGame ( void ) [static]

Definition at line 266 of file random-ttt.c.

00267 {
00268   if (!driver_CreateGame())
00269       return 0;
00270
00271   if (!driver_JoinGame())
00272       return 0;
00273
00274   if (!driver_StartGame())
00275       return 0;
00276
00277   if (gVerbose)
00278   {
00279     int count = PlayerCount(gGame);
00280     printf("Player count: %i\n", count);
00281   }
00282
00283   ClearBoard();
00284
00285   return 1;
00286 }

static int RandomInt | ( | int | low,
 |  | int | high |
 | ) |  |  | [static]

Definition at line 75 of file random-ttt.c.

00076 {
00077   int number;
00078   number = rand() % (high - low + 1) + low;
00079   return number;
00080 }

Variable Documentation

char Board[9] = {0} [static]

Definition at line 72 of file random-ttt.c.

const char EMPTY_SQUARE = '.' [static]

Definition at line 61 of file random-ttt.c.

int gCount = 1 [static]

Definition at line 54 of file random-ttt.c.

int gDraws = 0 [static]

Definition at line 57 of file random-ttt.c.

struct Game* gGame = 0 [static]

Definition at line 46 of file random-ttt.c.

int gGamesPlayed = 0 [static]

Definition at line 58 of file random-ttt.c.

int gLosses[3] = {0} [static]

Definition at line 56 of file random-ttt.c.

int gOther = 2 [static]

Definition at line 53 of file random-ttt.c.

int gPlayer = 1 [static]

Definition at line 52 of file random-ttt.c.

void* gPlayers[2] = {0} [static]

Definition at line 69 of file random-ttt.c.

int gSilent = 1 [static]

Definition at line 49 of file random-ttt.c.

int gVerbose = 0 [static]

Definition at line 50 of file random-ttt.c.

int gWins[3] = {0} [static]

Definition at line 55 of file random-ttt.c.

int gWinsMovesO[10] = {0} [static]

Definition at line 68 of file random-ttt.c.

int gWinsMovesX[10] = {0} [static]

Definition at line 67 of file random-ttt.c.

const char SYMBOLS[] = {' ', 'X', 'O'} [static]

Definition at line 62 of file random-ttt.c.


TicTacToe.c File Reference

Implementation of Tic-Tac-Toe. More...

#include <string.h>
#include <stdlib.h>
#include "PluginCommon.h"
#include "PluginInterface.h"
#include "TicTacToe.h"

Go to the source code of this file.

Functions

int | Init (void)
void | Shutdown (void)
const char * | Name (void)
const char * | Author (void)
const char * | Type (void)
const char * | Version (void)
const char * | APIVersion (void)
int | NumberOfPlayers (void)
void * | CreateGame (void)
int | SetGameOption (void *igame, const char *option)
int | AddPlayer (void *igame, void *player)
int | RemovePlayer (void *igame, void *player)
int | PlayerCount (void *igame)
void * | GetPlayer (void *igame, int playerNumber)
int | GetPlayerNumber (void *igame, void *player)
int | StartGame (void *igame)
void * | GetNextPlayer (void *igame)
int | ValidateMove (void *igame, void *player, const char *move)
int | GetGameStatus (void *igame)
void | DestroyGame (void *igame)

Detailed Description

Implementation of Tic-Tac-Toe.

Author:
Nathan Williams
email: nwilliam@digipen.edu
Date:
04/10/2010 Game Rules: Standard gameplay, 3x3 board with Xs and Os.

Board: 0 | 1 | 2 ------------- 3 | 4 | 5 ------------- 6 | 7 | 8

Move String: [int] Represents board position being played.

Definition in file TicTacToe.c.

Function Documentation

int AddPlayer | ( | void * | igame,
 |  | void * | player
 | ) |  |

Definition at line 85 of file TicTacToe.c.

00086 {
00087   struct Game* game = (struct Game *)igame;
00088
00089   if(game->players[0] == NULL)
00090     game->players[0] = player;
00091   else if(game->players[1] == NULL)
00092     game->players[1] = player;
00093   else
00094     return 1;
00095
00096   return 0;
00097 }

const char* APIVersion ( void )

Definition at line 58 of file TicTacToe.c.

00059 {
00060   return "0.1";
00061 }

const char* Author ( void )

Definition at line 43 of file TicTacToe.c.

00044 {
00045   return "nlw";
00046 }

void* CreateGame ( void )

Definition at line 68 of file TicTacToe.c.

00069 {
00070   struct Game* newGame = (struct Game *)calloc(1, sizeof(struct Game));
00071   if(newGame)
00072     newGame->status = PLUGIN_GAME_STATUS_CREATED;
00073   return newGame;
00074 }

void DestroyGame ( void * igame )

Definition at line 229 of file TicTacToe.c.

00230 {
00231   free(igame);
00232 }

int GetGameStatus ( void * igame )

Definition at line 223 of file TicTacToe.c.

00224 {
00225   struct Game* game = (struct Game *)igame;
00226   return game->status;
00227 }

void* GetNextPlayer ( void * igame )

Definition at line 158 of file TicTacToe.c.

00159 {
00160   struct Game* game = (struct Game *)igame;
00161
00162   if(game->nextPlayer > 0)
00163     return game->players[game->nextPlayer-1];
00164
00165   return NULL;
00166 }

void* GetPlayer | ( | void * | igame,
 |  | int | playerNumber
 | ) |  |

Definition at line 122 of file TicTacToe.c.

00123 {
00124   struct Game* game = (struct Game *)igame;
00125
00126   if(playerNumber == 1 || playerNumber == 2)
00127     return game->players[playerNumber-1];
00128
00129   return NULL;
00130 }

int GetPlayerNumber | ( | void * | igame,
 |  | void * | player
 | ) |  |

Definition at line 132 of file TicTacToe.c.

00133 {
00134   struct Game* game = (struct Game *)igame;
00135
00136   if(game->players[0] == player)
00137     return 1;
00138
00139   if(game->players[1] == player)
00140     return 2;
00141
00142   return 0;
00143 }

int Init ( void )

Definition at line 28 of file TicTacToe.c.

00029 {
00030   /* Nothing to do */
00031   return 0;
00032 }

const char* Name ( void )

Definition at line 38 of file TicTacToe.c.

00039 {
00040   return "Tic-Tac-Toe";
00041 }

int NumberOfPlayers ( void )

Definition at line 63 of file TicTacToe.c.

00064 {
00065   return 2;
00066 }

int PlayerCount ( void * igame )

Definition at line 116 of file TicTacToe.c.

00117 {
00118   struct Game* game = (struct Game *)igame;
00119   return (game->players[0] != 0) + (game->players[1] != 0);
00120 }

int RemovePlayer | ( | void * | igame,
 |  | void * | player
 | ) |  |

Definition at line 99 of file TicTacToe.c.

00100 {
00101   struct Game* game = (struct Game *)igame;
00102
00103   if(game->players[0] == player)
00104     game->players[0] = NULL;
00105   else if(game->players[1] == player)
00106     game->players[1] = NULL;
00107   else
00108     return 1;
00109
00110   if(game->status == PLUGIN_GAME_STATUS_PLAYING)
00111     game->status = PLUGIN_GAME_STATUS_PAUSED;
00112
00113   return 0;
00114 }

int SetGameOption | ( | void * | igame,
 |  | const char * | option
 | ) |  |

Definition at line 76 of file TicTacToe.c.

00077 {
00078   /* No options for this game */
00079   (void)igame;
00080   (void)option;
00081
00082   return 1;
00083 }

void Shutdown ( void )

Definition at line 34 of file TicTacToe.c.

00035 {
00036 }

int StartGame ( void * igame )

Definition at line 145 of file TicTacToe.c.

00146 {
00147   struct Game* game = (struct Game *)igame;
00148
00149   if(PlayerCount(game) != 2)
00150     return 1;
00151
00152   game->nextPlayer = 1;
00153   game->status = PLUGIN_GAME_STATUS_PLAYING;
00154
00155   return 0;
00156 }

const char* Type ( void )

Definition at line 48 of file TicTacToe.c.

00049 {
00050   return "TicTacToe";
00051 }

int ValidateMove | ( | void * | igame,
 |  | void * | player,
 |  | const char * | move
 | ) |  |

Definition at line 168 of file TicTacToe.c.

00169 {
00170   struct Game* game = (struct Game *)igame;
00171   char* board = game->board;
00172   int i, movePos;
00173
00174   int wins[8][3] = { {0,1,2},  {3,4,5},  {6,7,8},     /* Horizontal */
00175                      {0,3,6},  {1,4,7},  {2,5,8},     /* Vertical */
00176                      {0,4,8},  {2,4,6}            };  /* Diagonal */
00177
00178                 /* Sanity check */
00179   if(move == NULL || strlen(move) != 1 || game->status != PLUGIN_GAME_STATUS_PLAYING)
00180     return -1;
00181
00182   movePos = move[0] - '0';
00183
00184   /* Check out of range or already played */
00185   if(movePos < 0 || movePos > 8 || board[movePos] != 0)
00186     return -1;
00187
00188   if(game->players[0] == player)
00189   {
00190     board[movePos] = 'X';
00191     game->nextPlayer = 2;
00192   }
00193   else
00194   {
00195     board[movePos] = 'O';
00196     game->nextPlayer = 1;
00197   }
00198
00199   /* Check for winner or draw */
00200   for(i = 0; i < 8; ++i)
00201   {
00202     if(board[wins[i][0]] == board[wins[i][1]] &&
00203        board[wins[i][0]] == board[wins[i][2]] &&
00204        board[wins[i][0]] != 0)
00205     {
00206       game->nextPlayer = GetPlayerNumber(igame, player);
00207       game->status = PLUGIN_GAME_STATUS_OVER_WINNER;
00208       return 0;
00209     }
00210   }
00211
00212   /* Check for draw */
00213   if(board[0] && board[1] && board[2] && board[3] && board[4] &&
00214      board[5] && board[6] && board[7] && board[8])
00215   {
00216     game->nextPlayer = 0;
00217     game->status = PLUGIN_GAME_STATUS_OVER_DRAW;
00218   }
00219
00220   return 0;
00221 }

const char* Version ( void )

Definition at line 53 of file TicTacToe.c.

00054 {
00055   return "0.1";
00056 }


TicTacToe.h File Reference

#include "PluginCommon.h"

Go to the source code of this file.

Classes

struct Game


Class Members

  • File List
  • File Members
  • All
  • Functions
  • Variables
  • Enumerations
  • Enumerator
  • a
  • b
  • c
  • d
  • e
  • g
  • i
  • m
  • n
  • p
  • r
  • s
  • t
  • v
Here is a list of all file members with links to the files they belong to:

- a -

  • AddOutput() : random-ttt.c
  • AddPlayer() : TicTacToe.c , PluginInterface.h
  • APIVersion() : PluginInterface.h , TicTacToe.c
  • Author() : TicTacToe.c , PluginInterface.h

- b -

  • Board : random-ttt.c

- c -

  • CalcGameOverStats() : random-ttt.c
  • ClearBoard() : random-ttt.c
  • CreateGame() : TicTacToe.c , PluginInterface.h

- d -

  • DestroyGame() : TicTacToe.c , PluginInterface.h
  • DisplayGrandTotals() : random-ttt.c
  • driver_CreateGame() : random-ttt.c
  • driver_DestroyGame() : random-ttt.c
  • driver_JoinGame() : random-ttt.c
  • driver_StartGame() : random-ttt.c
  • DumpBoard() : random-ttt.c

- e -

  • EMPTY_SQUARE : random-ttt.c

- g -

  • gCount : random-ttt.c
  • gDraws : random-ttt.c
  • GetGameStatus() : PluginInterface.h , TicTacToe.c
  • GetNextMove() : random-ttt.c
  • GetNextPlayer() : TicTacToe.c , PluginInterface.h
  • GetPlayer() : TicTacToe.c , PluginInterface.h
  • GetPlayerNumber() : TicTacToe.c , PluginInterface.h
  • gGame : random-ttt.c
  • gGamesPlayed : random-ttt.c
  • gLosses : random-ttt.c
  • gOther : random-ttt.c
  • gPlayer : random-ttt.c
  • gPlayers : random-ttt.c
  • gSilent : random-ttt.c
  • gVerbose : random-ttt.c
  • gWins : random-ttt.c
  • gWinsMovesO : random-ttt.c
  • gWinsMovesX : random-ttt.c

- i -

  • init() : random-ttt.c
  • Init() : PluginInterface.h , TicTacToe.c

- m -

  • main() : random-ttt.c

- n -

  • Name() : TicTacToe.c , PluginInterface.h
  • NewGame() : random-ttt.c
  • NumberOfPlayers() : TicTacToe.c , PluginInterface.h

- p -

  • PlayerCount() : TicTacToe.c , PluginInterface.h
  • PLUGIN_GAME_STATUS : PluginCommon.h
  • PLUGIN_GAME_STATUS_CREATED : PluginCommon.h
  • PLUGIN_GAME_STATUS_ERROR : PluginCommon.h
  • PLUGIN_GAME_STATUS_OVER_DRAW : PluginCommon.h
  • PLUGIN_GAME_STATUS_OVER_WINNER : PluginCommon.h
  • PLUGIN_GAME_STATUS_PAUSED : PluginCommon.h
  • PLUGIN_GAME_STATUS_PLAYING : PluginCommon.h

- r -

  • RandomInt() : random-ttt.c
  • RemovePlayer() : PluginInterface.h , TicTacToe.c

- s -

  • SetGameOption() : TicTacToe.c , PluginInterface.h
  • Shutdown() : PluginInterface.h , TicTacToe.c
  • StartGame() : PluginInterface.h , TicTacToe.c
  • SYMBOLS : random-ttt.c

- t -

  • Type() : TicTacToe.c , PluginInterface.h

- v -

  • ValidateMove() : TicTacToe.c , PluginInterface.h
  • Version() : TicTacToe.c , PluginInterface.h


PluginCommon.h Source File

  • File List
  • File Members

PluginCommon.h

Go to the documentation of this file.
00001 /***** All content © 2010 DigiPen (USA) Corporation, all rights reserved. *****/
00009 #ifndef DP_PLUGIN_COMMON_H
00010 #define DP_PLUGIN_COMMON_H
00011
00012 enum PLUGIN_GAME_STATUS
00013 {
00014   PLUGIN_GAME_STATUS_ERROR,
00015   PLUGIN_GAME_STATUS_CREATED,
00016   PLUGIN_GAME_STATUS_PLAYING,
00017   PLUGIN_GAME_STATUS_PAUSED,
00018   PLUGIN_GAME_STATUS_OVER_DRAW,
00019   PLUGIN_GAME_STATUS_OVER_WINNER
00020 };
00021
00022 #endif /* DP_PLUGIN_COMMON_H */
00023


PluginInterface.h Source File

  • File List
  • File Members

PluginInterface.h

Go to the documentation of this file.
00001 /***** All content © 2010 DigiPen (USA) Corporation, all rights reserved. ****/
00009 #ifndef DP_PLUGIN_INTERFACE_H
00010 #define DP_PLUGIN_INTERFACE_H
00011
00012 /* Plugin functions must have undecorated names and cdecl calling convention. */
00013 #ifdef __cplusplus
00014 extern "C" {
00015 #endif
00016
00017
00018 /******************************************************************************/
00019 /* BEGIN API 0.1 */
00020 /******************************************************************************/
00021 int Init(void);
00022 void Shutdown(void);
00023
00024 const char* Name(void);
00025 const char* Author(void);
00026 const char* Type(void);
00027 const char* Version(void);
00028 const char* APIVersion(void);
00029 int NumberOfPlayers(void);
00030
00031 void* CreateGame(void);
00032 int SetGameOption(void* game, const char* option);
00033 int AddPlayer(void* game, void* player);
00034 int RemovePlayer(void* game, void* player);
00035 void* GetPlayer(void* game, int playerNumber);
00036 int GetPlayerNumber(void* game, void* player);
00037 int PlayerCount(void* game);
00038
00039 int StartGame(void* game);
00040 void* GetNextPlayer(void* game);
00041 int ValidateMove(void* game, void* player, const char* move);
00042 int GetGameStatus(void* game);
00043 void DestroyGame(void* game);
00044 /******************************************************************************/
00045 /* END API 0.1 */
00046 /******************************************************************************/
00047
00048
00049 #ifdef __cplusplus
00050 }
00051 #endif
00052
00053 #endif /* DP_PLUGIN_INTERFACE_H */
00054


TicTacToe.c Source File

  • File List
  • File Members

TicTacToe.c

Go to the documentation of this file.
00001 /***** All content © 2010 DigiPen (USA) Corporation, all rights reserved. ****/
00020 #include <string.h> /* strlen */
00021 #include <stdlib.h> /* calloc, free */
00022
00023 #include "PluginCommon.h"
00024 #include "PluginInterface.h"
00025
00026 #include "TicTacToe.h"
00027
00028 int Init(void)
00029 {
00030   /* Nothing to do */
00031   return 0;
00032 }
00033
00034 void Shutdown(void)
00035 {
00036 }
00037
00038 const char* Name(void)
00039 {
00040   return "Tic-Tac-Toe";
00041 }
00042
00043 const char* Author(void)
00044 {
00045   return "nlw";
00046 }
00047
00048 const char* Type(void)
00049 {
00050   return "TicTacToe";
00051 }
00052
00053 const char* Version(void)
00054 {
00055   return "0.1";
00056 }
00057
00058 const char* APIVersion(void)
00059 {
00060   return "0.1";
00061 }
00062
00063 int NumberOfPlayers(void)
00064 {
00065   return 2;
00066 }
00067
00068 void* CreateGame(void)
00069 {
00070   struct Game* newGame = (struct Game *)calloc(1, sizeof(struct Game));
00071   if(newGame)
00072     newGame->status = PLUGIN_GAME_STATUS_CREATED;
00073   return newGame;
00074 }
00075
00076 int SetGameOption(void* igame, const char* option)
00077 {
00078   /* No options for this game */
00079   (void)igame;
00080   (void)option;
00081
00082   return 1;
00083 }
00084
00085 int AddPlayer(void* igame, void* player)
00086 {
00087   struct Game* game = (struct Game *)igame;
00088
00089   if(game->players[0] == NULL)
00090     game->players[0] = player;
00091   else if(game->players[1] == NULL)
00092     game->players[1] = player;
00093   else
00094     return 1;
00095
00096   return 0;
00097 }
00098
00099 int RemovePlayer(void* igame, void* player)
00100 {
00101   struct Game* game = (struct Game *)igame;
00102
00103   if(game->players[0] == player)
00104     game->players[0] = NULL;
00105   else if(game->players[1] == player)
00106     game->players[1] = NULL;
00107   else
00108     return 1;
00109
00110   if(game->status == PLUGIN_GAME_STATUS_PLAYING)
00111     game->status = PLUGIN_GAME_STATUS_PAUSED;
00112
00113   return 0;
00114 }
00115
00116 int PlayerCount(void* igame)
00117 {
00118   struct Game* game = (struct Game *)igame;
00119   return (game->players[0] != 0) + (game->players[1] != 0);
00120 }
00121
00122 void* GetPlayer(void* igame, int playerNumber)
00123 {
00124   struct Game* game = (struct Game *)igame;
00125
00126   if(playerNumber == 1 || playerNumber == 2)
00127     return game->players[playerNumber-1];
00128
00129   return NULL;
00130 }
00131
00132 int GetPlayerNumber(void* igame, void* player)
00133 {
00134   struct Game* game = (struct Game *)igame;
00135
00136   if(game->players[0] == player)
00137     return 1;
00138
00139   if(game->players[1] == player)
00140     return 2;
00141
00142   return 0;
00143 }
00144
00145 int StartGame(void* igame)
00146 {
00147   struct Game* game = (struct Game *)igame;
00148
00149   if(PlayerCount(game) != 2)
00150     return 1;
00151
00152   game->nextPlayer = 1;
00153   game->status = PLUGIN_GAME_STATUS_PLAYING;
00154
00155   return 0;
00156 }
00157
00158 void* GetNextPlayer(void* igame)
00159 {
00160   struct Game* game = (struct Game *)igame;
00161
00162   if(game->nextPlayer > 0)
00163     return game->players[game->nextPlayer-1];
00164
00165   return NULL;
00166 }
00167
00168 int ValidateMove(void* igame, void* player, const char* move)
00169 {
00170   struct Game* game = (struct Game *)igame;
00171   char* board = game->board;
00172   int i, movePos;
00173
00174   int wins[8][3] = { {0,1,2},  {3,4,5},  {6,7,8},     /* Horizontal */
00175                      {0,3,6},  {1,4,7},  {2,5,8},     /* Vertical */
00176                      {0,4,8},  {2,4,6}            };  /* Diagonal */
00177
00178                 /* Sanity check */
00179   if(move == NULL || strlen(move) != 1 || game->status != PLUGIN_GAME_STATUS_PLAYING)
00180     return -1;
00181
00182   movePos = move[0] - '0';
00183
00184   /* Check out of range or already played */
00185   if(movePos < 0 || movePos > 8 || board[movePos] != 0)
00186     return -1;
00187
00188   if(game->players[0] == player)
00189   {
00190     board[movePos] = 'X';
00191     game->nextPlayer = 2;
00192   }
00193   else
00194   {
00195     board[movePos] = 'O';
00196     game->nextPlayer = 1;
00197   }
00198
00199   /* Check for winner or draw */
00200   for(i = 0; i < 8; ++i)
00201   {
00202     if(board[wins[i][0]] == board[wins[i][1]] &&
00203        board[wins[i][0]] == board[wins[i][2]] &&
00204        board[wins[i][0]] != 0)
00205     {
00206       game->nextPlayer = GetPlayerNumber(igame, player);
00207       game->status = PLUGIN_GAME_STATUS_OVER_WINNER;
00208       return 0;
00209     }
00210   }
00211
00212   /* Check for draw */
00213   if(board[0] && board[1] && board[2] && board[3] && board[4] &&
00214      board[5] && board[6] && board[7] && board[8])
00215   {
00216     game->nextPlayer = 0;
00217     game->status = PLUGIN_GAME_STATUS_OVER_DRAW;
00218   }
00219
00220   return 0;
00221 }
00222
00223 int GetGameStatus(void* igame)
00224 {
00225   struct Game* game = (struct Game *)igame;
00226   return game->status;
00227 }
00228
00229 void DestroyGame(void* igame)
00230 {
00231   free(igame);
00232 }
00233


TicTacToe.h Source File

  • File List
  • File Members

TicTacToe.h

Go to the documentation of this file.
00001 #include "PluginCommon.h" /* status */
00002
00003 struct Game
00004 {
00005   char board[9];
00006   int nextPlayer;
00007   void* players[2]; /* 0=Xs, 1=Os */
00008   enum PLUGIN_GAME_STATUS status;
00009 };


Alphabetical List

  • Class List
  • Class Members

Class Index

G | P
G
Game
P
Player
G | P


Class Members - Variables

  • Class List
  • Class Members
  • All
  • Variables
  • board : Game
  • dummy : Player
  • nextPlayer : Game
  • players : Game
  • status : Game


Class Members

  • File List
  • File Members
  • All
  • Functions
  • Variables
  • Enumerations
  • Enumerator
  • PLUGIN_GAME_STATUS : PluginCommon.h


Class Members

  • File List
  • File Members
  • All
  • Functions
  • Variables
  • Enumerations
  • Enumerator
  • PLUGIN_GAME_STATUS_CREATED : PluginCommon.h
  • PLUGIN_GAME_STATUS_ERROR : PluginCommon.h
  • PLUGIN_GAME_STATUS_OVER_DRAW : PluginCommon.h
  • PLUGIN_GAME_STATUS_OVER_WINNER : PluginCommon.h
  • PLUGIN_GAME_STATUS_PAUSED : PluginCommon.h
  • PLUGIN_GAME_STATUS_PLAYING : PluginCommon.h


Class Members

  • File List
  • File Members
  • All
  • Functions
  • Variables
  • Enumerations
  • Enumerator
  • a
  • c
  • d
  • g
  • i
  • m
  • n
  • p
  • r
  • s
  • t
  • v

- a -

  • AddOutput() : random-ttt.c
  • AddPlayer() : TicTacToe.c , PluginInterface.h
  • APIVersion() : PluginInterface.h , TicTacToe.c
  • Author() : TicTacToe.c , PluginInterface.h

- c -

  • CalcGameOverStats() : random-ttt.c
  • ClearBoard() : random-ttt.c
  • CreateGame() : TicTacToe.c , PluginInterface.h

- d -

  • DestroyGame() : TicTacToe.c , PluginInterface.h
  • DisplayGrandTotals() : random-ttt.c
  • driver_CreateGame() : random-ttt.c
  • driver_DestroyGame() : random-ttt.c
  • driver_JoinGame() : random-ttt.c
  • driver_StartGame() : random-ttt.c
  • DumpBoard() : random-ttt.c

- g -

  • GetGameStatus() : TicTacToe.c , PluginInterface.h
  • GetNextMove() : random-ttt.c
  • GetNextPlayer() : TicTacToe.c , PluginInterface.h
  • GetPlayer() : PluginInterface.h , TicTacToe.c
  • GetPlayerNumber() : PluginInterface.h , TicTacToe.c

- i -

  • init() : random-ttt.c
  • Init() : PluginInterface.h , TicTacToe.c

- m -

  • main() : random-ttt.c

- n -

  • Name() : TicTacToe.c , PluginInterface.h
  • NewGame() : random-ttt.c
  • NumberOfPlayers() : TicTacToe.c , PluginInterface.h

- p -

  • PlayerCount() : TicTacToe.c , PluginInterface.h

- r -

  • RandomInt() : random-ttt.c
  • RemovePlayer() : PluginInterface.h , TicTacToe.c

- s -

  • SetGameOption() : TicTacToe.c , PluginInterface.h
  • Shutdown() : PluginInterface.h , TicTacToe.c
  • StartGame() : PluginInterface.h , TicTacToe.c

- t -

  • Type() : TicTacToe.c , PluginInterface.h

- v -

  • ValidateMove() : TicTacToe.c , PluginInterface.h
  • Version() : TicTacToe.c , PluginInterface.h


Class Members

  • File List
  • File Members
  • All
  • Functions
  • Variables
  • Enumerations
  • Enumerator
  • Board : random-ttt.c
  • EMPTY_SQUARE : random-ttt.c
  • gCount : random-ttt.c
  • gDraws : random-ttt.c
  • gGame : random-ttt.c
  • gGamesPlayed : random-ttt.c
  • gLosses : random-ttt.c
  • gOther : random-ttt.c
  • gPlayer : random-ttt.c
  • gPlayers : random-ttt.c
  • gSilent : random-ttt.c
  • gVerbose : random-ttt.c
  • gWins : random-ttt.c
  • gWinsMovesO : random-ttt.c
  • gWinsMovesX : random-ttt.c
  • SYMBOLS : random-ttt.c


Main Page


random-ttt.c Source File

  • File List
  • File Members

random-ttt.c

Go to the documentation of this file.
00001
00029 #include <stdio.h>  /* printf, sprintf, fflush */
00030 #include <stdlib.h> /* rand, srand, atoi       */
00031 #include <time.h>   /* time                    */
00032
00033 #include "TicTacToe.h"
00034 #include "PluginInterface.h"
00035
00036 /*
00037   The server creates its own Player structs internally. I'm just using this
00038   as a place-holder for the server's player. It can be anything actually.
00039 */
00040 struct Player
00041 {
00042   int dummy;
00043 };
00044
00045 /* The game that is created by the server (i.e. plugin) */
00046 static struct Game* gGame = 0;
00047
00048 /* Options */
00049 static int gSilent = 1;      /* Produce very little output (just displays totals)           */
00050 static int gVerbose = 0;     /* Produce copious amounts of output during play               */
00051
00052 static int gPlayer = 1;      /* Which player is currently making a move                     */
00053 static int gOther = 2;       /* Which player is not making a move                           */
00054 static int gCount = 1;       /* The number of games to play                                 */
00055 static int gWins[3] = {0};   /* gWins[1] counts wins for player 1, gWins[2] counts player 2 */
00056 static int gLosses[3] = {0}; /* same as gWins except keeps track of losses                  */
00057 static int gDraws = 0;       /* Total number of draws                                       */
00058 static int gGamesPlayed = 0; /* The number of games played thus far                         */
00059
00060 /* For drawing the board on the screen */
00061 static const char EMPTY_SQUARE = '.';
00062 static const char SYMBOLS[] = {' ', 'X', 'O'};
00063
00064 /* Track how many moves it took for a win i.e.
00065     gWinsMovesX[7]++ means that X won on move #7
00066 */
00067 static int gWinsMovesX[10] = {0};
00068 static int gWinsMovesO[10] = {0};
00069 static void *gPlayers[2] = {0};
00070
00071 /* The representation of the tic-tac-toe board */
00072 static char Board[9] = {0};
00073
00074 /* generate random numbers between low and high */
00075 static int RandomInt(int low, int high)
00076 {
00077   int number;
00078   number = rand() % (high - low + 1) + low;
00079   return number;
00080 }
00081
00082
00083 /*******************************************************************************/
00084 /*******************************************************************************/
00085 /*******************************************************************************/
00086 /*******************************************************************************/
00087 /*
00088   If you wanted to make this test client intelligent, this is likely the only
00089   function you'd have to change. You'd also have to know  which player is
00090   moving so you could pick the best next move based on which player is making
00091   the move. The player that is currently moving is in the variable gPlayer.
00092
00093   Currently, this function simply picks a random move
00094 */
00095 static int GetNextMove(void)
00096 {
00097   int position;
00098
00099   /* Keep choosing a random square until a valid (e.g. empty) square is found */
00100   do
00101   {
00102     position = RandomInt(0, 8);
00103   }while (Board[position] != 0);
00104
00105   return position;
00106 }
00107 /*******************************************************************************/
00108 /*******************************************************************************/
00109 /*******************************************************************************/
00110 /*******************************************************************************/
00111
00112
00113
00114 static void ClearBoard(void)
00115 {
00116   int i;
00117   for (i = 0; i < 9; i++)
00118     Board[i] = 0;
00119 }
00120
00121 static void DumpBoard(void)
00122 {
00123   /*char *board = gGame->board;*/
00124
00125     /* For each row */
00126   int i;
00127   for (i = 0; i < 9; i++)
00128   {
00129     int ch = Board[i];
00130     if ( !ch )
00131       printf("%2c", EMPTY_SQUARE);
00132     else
00133       printf("%2c", ch);
00134
00135     if (((i + 1) % 3) == 0)
00136       printf("\n");
00137   }
00138   printf("\n");
00139 }
00140
00141 static void AddOutput(const char* str, int newline)
00142 {
00143   printf("%s", str);
00144   if (newline)
00145     printf("\n");
00146
00147   fflush(stdout);
00148 }
00149
00150 /*
00151   Creates the game and adds a player as Player 1
00152 */
00153 static int driver_CreateGame(void)
00154 {
00155   struct Player *player = NULL;
00156
00157   if (gVerbose)
00158     AddOutput("Creating game...", 1);
00159
00160   gGame = (struct Game *)CreateGame();
00161   if (!gGame)
00162   {
00163     AddOutput("Error: Can't create game.", 1);
00164     return 0;
00165   }
00166
00167   player = malloc(sizeof (struct Player));
00168   if (AddPlayer(gGame, player))
00169   {
00170     AddOutput("Error: Can't add player 1.", 1);
00171     DestroyGame(gGame);
00172     free(player);
00173     return 0;
00174   }
00175
00176   gPlayers[0] = player;
00177   return 1;
00178 }
00179
00180 /*
00181   Joining the game simply adds a second player to the game.
00182 */
00183 static int driver_JoinGame(void)
00184 {
00185   struct Player *player;
00186   if (gVerbose)
00187   {
00188     AddOutput("Joining game with player 2", 1);
00189   }
00190
00191   if (!gGame)
00192   {
00193     AddOutput("Error: Can't join a game: No game created.", 1);
00194     return 0;
00195   }
00196
00197   player = malloc(sizeof(struct Player));
00198   if (AddPlayer(gGame, player))
00199   {
00200     AddOutput("Error: Can't add player 2.", 1);
00201     free(player);
00202     return 0;
00203   }
00204
00205   gPlayers[1] = player;
00206   return 1;
00207 }
00208
00209 /*
00210   After the game is created and both players (TicTacToe) are added,
00211   the game can be started. The server will do this automatically after
00212   bot players are in the game and bot players have sent the "Ready"
00213   message.
00214 */
00215 static int driver_StartGame(void)
00216 {
00217   int count;
00218
00219   if (gVerbose)
00220     AddOutput("Starting game...", 1);
00221
00222   if (!gGame)
00223   {
00224     AddOutput("Error: Can't start a game: No game created.", 1);
00225     return 0;
00226   }
00227
00228     /* Sanity check */
00229   count = PlayerCount(gGame);
00230   if (count != 2)
00231   {
00232     printf("count = %i\n", count);
00233     AddOutput("Can't start game: requires 2 players.", 1);
00234     return 0;
00235   }
00236     /*
00237       This should always succeed because the only check the plugin makes
00238       is to see that there are 2 players in the game. We just checked that
00239       case above.
00240      */
00241   if (StartGame(gGame))
00242   {
00243     AddOutput("Error: Can't start game.", 1);
00244     return 0;
00245   }
00246
00247   return 1;
00248 }
00249
00250 /*
00251   Clean up the game.
00252 */
00253 static void driver_DestroyGame(void)
00254 {
00255   if (gVerbose)
00256     AddOutput("Destroying game...", 1);
00257
00258   DestroyGame(gGame);
00259   free(gPlayers[0]);
00260   free(gPlayers[1]);
00261 }
00262
00263 /*
00264   Wrapper function for getting a game going.
00265 */
00266 static int NewGame(void)
00267 {
00268   if (!driver_CreateGame())
00269       return 0;
00270
00271   if (!driver_JoinGame())
00272       return 0;
00273
00274   if (!driver_StartGame())
00275       return 0;
00276
00277   if (gVerbose)
00278   {
00279     int count = PlayerCount(gGame);
00280     printf("Player count: %i\n", count);
00281   }
00282
00283   ClearBoard();
00284
00285   return 1;
00286 }
00287
00288 /*
00289   Deals with any command line parameters our driver wants to accept.
00290   Your driver can do anything here.
00291 */
00292 static int init(int argc, char **argv)
00293 {
00294   if (argc < 2)
00295   {
00296     printf("Usage: %s {count}\n", argv[0]);
00297     printf("\n");
00298     printf("where: count is the number of games to play.\n");
00299     return 0;
00300   }
00301   gCount = atoi(argv[1]);
00302
00303   if (gVerbose)
00304   {
00305     printf("Plugin information\n");
00306     printf("----------------------------\n");
00307     printf("       Name: %s\n", Name());
00308     printf("     Author: %s\n", Author());
00309     printf("       Type: %s\n", Type());
00310     printf("    Players: %i\n", NumberOfPlayers());
00311     printf("    Version: %s\n", Version());
00312     printf("API Version: %s\n", APIVersion());
00313     printf("----------------------------\n");
00314   }
00315
00316   return 1;
00317 }
00318
00319 /*
00320   One of the goals of this test client is to accumulate lots of statistics
00321   about the game and display them after running many games.
00322 */
00323 static void CalcGameOverStats(int movenum)
00324 {
00325   int winner = GetPlayerNumber(gGame, GetNextPlayer(gGame));
00326   char buffer[100];
00327
00328   if (winner == 0)
00329   {
00330     sprintf(buffer, "The game is a draw!");
00331   }
00332   else
00333     sprintf(buffer, "Game over: Player %i is the winner after %i moves!", winner, movenum);
00334
00335   if (gVerbose)
00336     AddOutput(buffer, 1);
00337
00338   if (winner == 0)
00339     gDraws++;
00340   else if (winner == gPlayer)
00341   {
00342     gWins[gPlayer]++;
00343     gLosses[gOther]++;
00344   }
00345   else
00346   {
00347     gWins[gOther]++;
00348     gLosses[gPlayer]++;
00349   }
00350
00351   if (winner == 1)
00352     gWinsMovesX[movenum]++;
00353   else if (winner == 2)
00354     gWinsMovesO[movenum]++;
00355
00356   if (!gSilent)
00357   {
00358     printf("Wins - Player 1:%i, Player 2:%i, Draws: %i, Games Played:%i, Games Left:%i\n",
00359             gWins[1], gWins[2], gDraws, gGamesPlayed, gCount - gGamesPlayed);
00360   }
00361 }
00362
00363 static void DisplayGrandTotals(void)
00364 {
00365   int i;
00366
00367   printf("Grand Total:\n");
00368   printf("Wins - Player 1:%i, Player 2:%i, Draws: %i, Games Played:%i, Games Left:%i\n",
00369           gWins[1], gWins[2], gDraws, gGamesPlayed, gCount - gGamesPlayed);
00370
00371   printf("Player 1 won this many games on move number: ");
00372   for (i = 5; i <= 9; i++)
00373     printf("%i:%i   ", i, gWinsMovesX[i]);
00374   printf("\n");
00375
00376   printf("Player 2 won this many games on move number: ");
00377   for (i = 5; i <= 9; i++)
00378     printf("%i:%i   ", i, gWinsMovesO[i]);
00379   printf("\n");
00380 }
00381
00382 /*
00383   This is the loop where the games are "played".
00384 */
00385 int main(int argc, char **argv)
00386 {
00387   int i;
00388
00389   if (!init(argc, argv))
00390     return 0;
00391
00392     /* Uncomment the call to srand to have randomness every time the program is run. */
00393     /* It's easier to debug a program that is repeatable each time. Once the         */
00394     /* program is correct, enable the randomness.                                    */
00395   /* srand(time(NULL)); */
00396
00397     /* Loop once for each game */
00398   for (i = 0; i < gCount; i++)
00399   {
00400     int i, movenum = 0;
00401
00402       /* Set up a new game each time */
00403     NewGame();
00404
00405       /* Player 1 always goes first (X's) */
00406     gPlayer = 1;
00407
00408       /* There are at most 9 moves in 3x3 Tic-Tac-Toe */
00409     for (i = 0; i < 9; i++)
00410     {
00411       int player;
00412       char buf[10];
00413
00414         /* GetNextMove() call will return the current player's (i.e. gPlayer) next move.  */
00415         /* The "intelligence" of this function determines the intelligence of the client. */
00416       int move = GetNextMove();
00417
00418         /* Set the move to the current player's symbol (i.e. 'X' or 'O') */
00419       Board[move] = SYMBOLS[gPlayer];
00420       movenum++;
00421
00422         /* Call the plugin to verify that the move is valid */
00423       sprintf(buf, "%i", move);
00424       ValidateMove(gGame, GetPlayer(gGame, gPlayer), buf);
00425
00426         /* Display board on the screen if we're debugging */
00427       if (gVerbose)
00428         DumpBoard();
00429
00430         /* Game ended with a winner or a draw */
00431       if (GetGameStatus(gGame) == PLUGIN_GAME_STATUS_OVER_WINNER || GetGameStatus(gGame) == PLUGIN_GAME_STATUS_OVER_DRAW)
00432       {
00433           /* Calculate and print statistics */
00434         CalcGameOverStats(movenum);
00435         break;
00436       }
00437
00438         /* Toggle next player */
00439       player = GetPlayerNumber(gGame, GetPlayer(gGame, gPlayer));
00440       gPlayer = (player == 1) ? 2 : 1;
00441       gOther = (gPlayer == 1) ? 2 : 1;
00442     }
00443     gGamesPlayed++;
00444     driver_DestroyGame();
00445   }
00446
00447   DisplayGrandTotals();
00448   return 0;
00449 }
00450


Member List

  • Class List
  • Class Members

Game Member List

This is the complete list of members for Game, including all inherited members.
board | Game
nextPlayer | Game
players | Game
status | Game


Member List

  • Class List
  • Class Members

Player Member List

This is the complete list of members for Player, including all inherited members.
dummy Player