Content of the lesson:
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]
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?
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.
const radku = 20; sloupcu = 70;
type piskvorky = array[1..radku, 1..sloupcu] of char;
var p:piskvorky;
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.
for radek:=1 to radku do
for sloupec:=1 to sloupcu do
p[radek,sloupec]:='.';
To display the game status you have to write the array to the screen.
for radek:=1 to pocetradku do
begin
for sloupec:=1 to pocetsloupcu do
begin
write(p[radek,sloupec]);
end;
writeln;
end;
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:
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;
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).
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;
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).