Figure 1


Pseudocode for a basic user-computer game.

#define MAXLEN 120   /* max length of a string */ 
/* Data structure for Game */ 
/* Data structure for a Move */ 
enum player_type {USER,COMPUTER}; 
typedef enum player_type Player;

int main() 
{
  Game G;    /* G represents the game */
  Move mv;   /* mv will hold the current move in the game */
  Player p = USER;  /* the first player is the user */
  char user_name[MAXLEN];

  init_game(&G, user_name);  /* initialisation */
  printf("Hello %s\n", user_name);
  display_game(G);
  do {
    choose_move(G, p, &mv);   /* get a move */
    make_move(p, user_name, &mv, &G);   
/* apply the move to the game */
    display_game(G);
    swap_player(&p);
{ while (!game_over(G)); 
  give_result(G, p, user_name);
  return 0;
}

void choose_move(Game G, Player p, Move *mv)
{
  if (p == USER)
    user_move(G, mv);
  else
    comp_move(G, mv);  /* computer's move */
}

void user_move(Game G, Move *mv)
{
  read_move(mv);
  while (!legal_move(G, *mv))
    read_move(mv); 
}

void swap_player(Player *p)
{
  if (*p == USER)
    *p = COMPUTER;
  else
    *p = USER;
}