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
const radku = 20; sloupcu = 70;

type piskvorky = array[1..radku, 1..sloupcu] of char;

var p:piskvorky;

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:=1 to radku do
  for sloupec:=1 to sloupcu do
    p[radek,sloupec]:='.';

Displaying Game Status

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

Zobrazení stavu hry
for radek:=1 to pocetradku do
begin
for sloupec:=1 to pocetsloupcu do
begin
write(p[radek,sloupec]);
end;
writeln;
end;

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
var vyhra:byte;
...
vyhra:=0;
while ((vyhra < 3) or (vyhra > 5)) do
begin
writeln ('Set the number of symbols in sequence to win (3, 4 or 5) :');
readln (vyhra);
if ((vyhra < 3) or (vyhra > 5)) then
writeln ('Wrong number was inserted.');
end;

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
program hrapiskvorky;

{$APPTYPE CONSOLE}

uses
SysUtils;

const radku = 20; sloupcu = 70;

type piskvorky = array[1..radku, 1..sloupcu] of char;

var p:piskvorky;
radek, sloupec, pocetradku, pocetsloupcu, vyhra:byte;
hrac1, hrac2:string;

begin
pocetradku:=0;
pocetsloupcu:=0;

while ((pocetradku) < 1) or ((pocetradku) > (radku)) do
begin
writeln ('Zadejte pocet radku hraci plochy 1 - ',radku,':');
readln (pocetradku);
if ((pocetradku) < 1) or ((pocetradku) > (radku)) then
writeln ('Zadali jste spatny pocet radku.');
end;

while ((pocetsloupcu) < 1) or ((pocetsloupcu) > (sloupcu)) do
begin
writeln ('Zadejte pocet sloupcu hraci plochy 1 - ',sloupcu,':');
readln (pocetsloupcu);
if ((pocetsloupcu) < 1) or ((pocetsloupcu) > (sloupcu)) then
writeln ('Zadali jste spatny pocet sloupcu.');
end;

vyhra:=0;

while ((vyhra < 3) or (vyhra > 5)) do
begin
writeln ('Zadejte pocet kamenu pro vyhru (3, 4 nebo 5) :');
readln (vyhra);
if ((vyhra < 3) or (vyhra > 5)) then
writeln ('Nezadal jste spravny pocet kamenu.');
end;

for radek:=1 to pocetradku do
begin
for sloupec:=1 to pocetsloupcu do
begin
p[radek,sloupec]:='.';
end;
end;

writeln('Zadejte prosim jmeno hrace c.1:');
readln(hrac1);
writeln;
writeln('Zadejte prosim jmeno hrace c.2:');
readln(hrac2);

for radek:=1 to pocetradku do
begin
for sloupec:=1 to pocetsloupcu do
begin
write(p[radek,sloupec]);
end;
writeln;
end;

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áš