Czech version
logolink

< Back to the list of lessons

Gomoku Game I

AlgortimyContent of the lesson:

  • Principle of Gomoku
  • Algorithm - Main Steps of Program
  • Initializing Game Environment
  • Displaying Game Status
  • Changing the Length of Sequence to Win
  • Inserting Names of Players

Principle of Gomoku

Gomoku is a strategic game for two players. It is mostly played using a list of squared paper and both players use their signs (usually a cross and a circle). Winner has to create a sequence of five symbols of his type. [http://cs.wikipedia.org/wiki/Piškvorky]

Algorithm - Main Steps of Program

  • Initializing the game environment (explain the word initialization using the Internet).
  • Selecting the length of sequence needed to win
  • Inserting names of players.
  • 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).

Variables and Data Types

We need to store data for our program inside variables. Consider what types of variables are the best ones for this program.

The game environment (our fictive list of squared paper) will be represented by a 2D array using m x n squares (if you want to create the game using the standard text console inside Windows, you have to find out the maximum number of rows and columns which can fit in the console. To set a solid and not changing variable you should use constants named radku and sloupcu. The last question which remains is: "What data type should we use?". To be able to answer it you should think about all data needed for the game. In what state can a field be?

  • 1. There is nothing on a field (it is empty).
  • 2. There is the symbol of Player 1 on a field (usually a circle).
  • 3. There is the symbol of Player 2 on a field (usually a cross).

We need a data type which is able to store 3 different characters (cross - the upper X letter, circle - upper O letter and then a character for every empty field - for example a dot). The best data type is the char (it allows you to store one alphanumeric character).

You see the definition of two constants in the following box, then the definition of our data type and variable p which is of piskvorky data type.

Declaration of variables and data types
#include "stdafx.h"
#define radku 20
#define sloupcu 70

int _tmain(int argc, _TCHAR* argv[])
{
    char p[radku][sloupcu];

    ...

    return 0;
}

Initializing Game Environment

We will use a game environment of radku x sloupcu dimensions (m x n fields in total). Initialize the game environment by setting every field to the '.' character which means an empty field. The radek variable stores the number of current row and the variable sloupec stores similarly the number of current column.

Initializing game environment
for (radek = 0; radek < radku; radek++)
{
  for (sloupec = 0; sloupec < sloupcu)
  {
    p[radek][sloupec]='.';
  }
}

Displaying Game Status

To display the game status you have to write the array to the screen.

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

Changing the Length of Sequence to Win

In Gomoku wins that player who creates a sequence of his symbols in one of 4 directions. The question is how long should the sequence be (usually 5 symbols mean victory but we will allow players to set 5, 4, or 3 symbols to win). Create a new variable to store this information. What data type should we use and how to ensure that no other value can be inserted? What name should we use? (It is said that naming a variable is more difficult than writing the code which uses it in many cases.)

After answering all questions from the previous paragraph, you can look at one of the possible solutions:

Setting the length of sequence to win the game
int vyhra;
...
vyhra=0;
while ((vyhra < 3) || (vyhra > 5))
{
  fprintf("Set the number of symbols in sequence to win (3, 4 or 5) :");
  fscanf("%d",&vyhra);
  if ((vyhra < 3) || (vyhra > 5)) printf("Wrong number was inserted!");
}

Program

Take a look at the result as a complex. The program is enriched by the possibility to define the dimensions of the game environment (pocetradku and pocetsloupcu variables).

The whole program
#include "stdafx.h"
#define radku 20
#define sloupcu 70

int _tmain(int argc, _TCHAR* argv[])
{
  char p[radku][sloupcu];
  int radek, sloupec, pocetradku, pocetsloupcu, vyhra;
  char hrac1[50], hrac2[50];

  pocetradku=0;
  pocetsloupcu=0;

  while ((pocetradku) < 1) || ((pocetradku) > (radku))
  {
    printf("Zadejte pocet radku hraci plochy 1 - %d:",radku);
    scanf("%d",&pocetradku);
    if ((pocetradku) < 1) || ((pocetradku) > (radku)) printf("Zadali jste spatny pocet radku!");
  }

  while ((pocetsloupcu) < 1) || ((pocetsloupcu) > (sloupcu))
  {
    printf("Zadejte pocet sloupcu hraci plochy 1 - ',sloupcu,':');
    scanf("%d",&pocetsloupcu);
    if ((pocetsloupcu) < 1) || ((pocetsloupcu) > (sloupcu)) printf("Zadali jste spatny pocet sloupcu.");
  }

  vyhra=0;

  while ((vyhra < 3) || (vyhra > 5))
  {
    fprintf("Zadejte pocet kamenu pro vyhru (3, 4 nebo 5) :");
    fscanf("%d",&vyhra);
    if ((vyhra < 3) || (vyhra > 5)) printf("Nezadal jste spravny pocet kamenu!");
  }

  for (radek = 0; radek < radku; radek++)
  {
    for (sloupec = 0; sloupec < sloupcu)
    {
      p[radek][sloupec]='.';
    }
  }

  printf("Zadejte prosim jmeno hrace c.1:");
  scanf("%s",&hrac1);
  printf("\n");
  printf("Zadejte prosim jmeno hrace c.2:");
  scanf("%s",&hrac2);

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

Individual Task

Treat the process of inserting names of players so it is not possible to insert two identic names and ensure every name will be 2 characters long at least (help: try to use the length function).

Additional Texts

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