2. Games
1. game rules:
1-11 numbers are randomly selected, and each time the player and the computer smoke once, players and the computer can announce that no number is drawn, the sum of all numbers is more than,, everyone wins.
(1) Welcome players
The gchar character type is used to store the player name.
Typedef char gchar;
Write the following code:
# Include
# Include
# Include
Int main (int argc, char * argv []) {
Setlocale (LC_ALL ,"");
Gchar gamename [10];
G_print ("What is your name? \ N ");
Scanf ("% s", & gamename );
G_print ("Welcome, % s. Here is GAME \ n", gamename );
Return 0;
}
Dp @ dp :~ /Gliblearn % gcc 'pkg-config -- cflags -- libs glib-2.0 gthread-2.0 '1.c-o mytest
Dp @ dp :~ /Gliblearn %./mytest
What is your name?
Mai Hao
Welcome, Mike. Here is the game!
Dp @ dp :~ /Gliblearn %
All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/
(2) introduce random numbers
Use the random number function of glib to generate a random number
Gamerand = g_rand_new ();
Rndnumber = g_rand_int_range (gamerand, 1, 11 );
The procedure is as follows:
# Include
# Include
# Include
Int main (int argc, char * argv []) {
Setlocale (LC_ALL ,"");
GRand * gamerand;
Gchar gamename [10];
G_print ("What is your name? \ N ");
Scanf ("% s", & gamename );
G_print ("Welcome, % s. Here is GAME \ n", gamename );
G_print ("% s, please press a number! \ N ", gamename );
Getchar ();
Getchar ();
Gint rndnumber;
Gamerand = g_rand_new ();
Rndnumber = g_rand_int_range (gamerand, 1, 11 );
G_print ("% s, you have drawn: % d \ n", gamename, rndnumber );
G_rand_free (gamerand );
Return 0;
}
Dp @ dp :~ /Gliblearn %./mytest
What is your name?
Myhaspl
Welcome, myhaspl. Here is the game.
Myhaspl. Press a number!
Myhaspl: 3
Dp @ dp :~ /Gliblearn %