C pointer principle (72)-GLIB

Source: Internet
Author: User

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 %


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.