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.
#include "stdafx.h"
#define radku 20
#define sloupcu 70
int _tmain(int argc, _TCHAR* argv[])
{
char p[radku][sloupcu];
...
return 0;
}
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 = 0; radek < radku; radek++)
{
for (sloupec = 0; sloupec < sloupcu)
{
p[radek][sloupec]='.';
}
}
To display the game status you have to write the array to the screen.
for (radek = 0; radek < radku; radek++)
{
for (sloupec = 0; sloupec < sloupcu)
{
printf("%c",p[radek][sloupec]);
}
printf("\n");
}
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:
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!");
}
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).
#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();
}
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).