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 string data type to store names of players. Each player will then insert his name inside these variables.

Inserting names of players
var hrac1, hrac2:string;
...
writeln('Insert name of Player 1:');
readln(hrac1);
writeln;
writeln('Insert name of Player 2:');
readln(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 byte data type (we will not use more than 255 fields).

One Move
write('Enter row number: ');
readln(r);
write('Enter column number: ');
readln(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
writeln('The coordinates of the row must be between 1 and ',pocetradku,'.');
write('row: ');
readln(r);
writeln('The coordinates of the column must be between 1 and ',pocetsloupcu,'.');
write('column: ');
readln(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
writeln('The coordinates of the row must be between 1 and ',pocetradku,' and field with these coordinates must be free.');
write('row: ');
readln(r);
writeln('The coordinates of the column must be between 1 and ',pocetsloupcu,' and field with these coordinates must be free.');
write('column: ');
readln(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 boolean data type and set it to false (game is not over). You will change it to true in case the program evaluates that the game is over.

Adding the while cycle
konec:=false;
while (konec=false) do
begin

end;

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:=false;
typkamene:='X';
while (konec=false) do
begin

end;

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.

Inserting the symbol and writing the array at the end of each while cycle
konec:=false;
typkamene:='X';

while (konec=false) do
begin
  writeln('Zadej radek pro kamen: (1 - ',pocetradku,')');
  readln(r);
  while (r > (pocetradku)) or (r < 1) do
  begin
    IF (r > (pocetradku)) or (r < 1) then
    begin
      writeln ('Zadal jste radek mimo hraci plochu.');
      writeln('Zadej radek pro kamen: (1 - ',pocetradku,')');
      readln(r);
    end;
  end;

  writeln('Zadej sloupec pro kamen: (1 - ',pocetsloupcu,')');
  readln(s);
  while (s > (pocetsloupcu)) or (s < 1) do
  begin
    IF (s > (pocetsloupcu)) or (s < 1) then
    begin
      writeln ('Zadal jste sloupec mimo hraci plochu.');
      writeln('Zadej sloupec pro kamen: (1 - ',pocetsloupcu,')');
      readln(s);
    end;
  end;

  p[r,s]:=typkamene;

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

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') then 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áš