Czech version
logolink

< Back to the list of lessons

Gomoku Game II

AlgortimyContent of the lesson:

  • Setting Names of Players
  • One Move
  • The Process of Game (particular moves)

Setting Names of Players

Setting the names of players - try to suggest suitable data structures to store this information (consider two players). This step is very simple; you have to create two variables (hrac1, hrac2) of the char data type to store the string (the size of array sets the possible length of names). You have to be aware of the fact that the last character is used to end the string so you can save maximally 49 letters into array of 50 elements. Players will insert their names into the arrays.

Inserting names of players
char hrac1[50], hrac2[50];
...
printf("Zadejte prosim jmeno hrace c.1:");
scanf("%s", &hrac1);
printf("\n");
printf("Zadejte prosim jmeno hrace c.2:");
scanf("%s", &hrac2);

One Move

One move means that one player chooses a position to place his symbol. He has to enter coordinates (r, s) of a field. The coordinates will be represented by two variables r, s of the integer data type (this data type should be enough for our purpose).

One Move
printf("Enter row number: ");
scanf("%d, &r);
printf("Enter column number: ");
scanf("%d, &s);

You have to think about the possibility that a player can insert any number of byte data type inside the r and s variables. What does it mean?

You have to treat the situation when a position (r,s) outside the piskvorky array was set. You should write a warning and let the player set new coordinates.

One Move
printf("The coordinates of the row must be between 1 and %d.",pocetradku);
printf("radek: ");
scanf("%d, &r);
printf("The coordinates of the column must be between 1 and %d.\n",pocetsloupcu);
printf("sloupec: ");
scanf("%d, &s);

Realize that you cannot place the symbol even after this check. You have to be sure that the field with these coordinates is empty (not occupied by another symbol - you would replace it). Create a new condition and write a warning message in case that the player tries to place his symbol on an occupied field.

One Move
printf("The coordinates of the row must be between 1 and %d and field with these coordinates must be free.\n",pocetradku);
printf("radek: ");
scanf("%d, &r);
printf("The coordinates of the column must be between 1 and %d and field with these coordinates must be free.\n",pocetsloupcu);
printf("sloupec: ");
scanf("%d, &s);

The Process of Game (particular moves)

Remember the steps which are being repeated throughout the game:

  • Repeating moves of both players until the game is over. Their moves can be described using these steps:
  • Player 1 chooses the row and the column for his symbol and the symbol is placed.
  • Player 2 chooses the row and the column for his symbol and the symbol is placed.
  • Player 1 chooses the row and the column for his symbol and the symbol is placed.
  • ...
  • Moves are repeated until the game is over (the end occurs in case that a player completes sequence of his symbols).

You can see that this is a cycle - repeated steps - until the game is over (in case that a condition indicating the end of game is valid). The while cycle is the best one for this situation (to repeat something until the condition is valid ... repeat the game moves until the game is over).

The condition for the end of the game can be complex and it would not be practical to write it inside the header of the while cycle. Create a new variable konec of integer data type and set it to 0 (game is not over). We will change it to 1 in case that the program evaluates that the game is over.

Adding the while cycle
konec=0;
while (konec==0)
{

}

The last thing you should do before the evaluation of the end of game is switching the players. You have to know the active player throughout the game and the active symbol (cross or circle).

Declare the variable typkamene:char which will contain the circle ('O') or the cross ('X') symbol. Before the beginning of the cycle set this variable to the value 'X' for example.

Definition of the variable to store the active symbol
konec=0;
typkamene='X';
while (konec==0)
{

}

Insert the instruction to insert the active symbol on the set position [r][s] inside the while cycle using this command: p[r][s]=typkamene. Write the array to screen at the end of each while cycle.

Writing the array
konec=0;
typkamene='X';
while (konec==0)
{
  printf("Zadej radek pro kamen: (1 - %d)",pocetradku);
  scanf("%d",&r);
  while ((r > pocetradku) || (r < 1))
  {
    if ((r > pocetradku) || (r < 1))
    {
      printf("Zadal jste radek mimo hraci plochu.\n");
      printf("Zadej radek pro kamen: (1 - %d)",pocetradku);
      scanf("%d", &r);
    }
  }

  printf("Zadej sloupec pro kamen: (1 - %d)",pocetrsloupcu);
  scanf("%d",&s);
  while ((s > pocetrsloupcu) || (s < 1))
  {
    if ((s > pocetrsloupcu) || (s < 1))
    {
      printf("Zadal jste sloupec mimo hraci plochu.\n");
      printf("Zadej sloupec pro kamen: (1 - %d)",pocetrsloupcu);
      scanf("%d", &s);
    }
  }

  p[r][s]=typkamene;

  for (radek = 0; radek < radku; radek++)
  {
    for (sloupec = 0; sloupec < sloupcu)
    {
      printf("%c",p[radek][sloupec]);
    }
    printf("\n");
  }
}

Add the following command at the end of the cycle to switch between players and to switch the active symbol.

Switching players
if (typkamene=='X') typkamene='O';
  else typkamene='X';

Individual Task

Try to write the part of this program which will evaluate the end of game using a game environment of size 3x3. The end will occur in case that 3 symbols are in a row (horizontally, vertically of diagonally).

Additional Texts

Links
webdesign, xhtml, css, php - Mgr. Michal Mikláš