C pointer principle (75)-GLIB

Source: Internet
Author: User

4. Rebuild a 21-point game

1. Introduce the string type

The GString structure contains three members: the C string that saves the current state of the string, except the string length of the Terminator and the current memory size allocated for the string. If the length of the allocated memory is exceeded, GString automatically allocates more memory to it.

Typedef struct

{

Gchar * str;

Gsize len;

Gsize allocated_len;

} GString;

All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/

After the number of players and computers is pumped, only the number drawn this time is displayed. You need to display all the numbers after each extraction, and use Gstring to dynamically generate strings containing multiple numbers. One way to create a new GString is to use g_string_new () and g_string_append_printf () to append the formatted string to the end of the GString and keep the current content unchanged.

Dp @ dp :~ /Gliblearn % cat 21dian. c

# Include

# Include

# Include

// Code: myhaspl@myhaspl.com

// Date: 2014-01-26

// Do not use it for any purpose without the written consent of the author

Int main (int argc, char * argv []) {

Setlocale (LC_ALL ,"");

GString * man_list;

GString * comp_list;

Man_list = g_string_new ("number drawn by the player :");

Comp_list = g_string_new ("computer-drawn number :");

GRand * gamerand;

Gchar gamename [10];

G_print ("What is your name? \ N ");

Scanf ("% s", & gamename );

G_print ("Welcome, % s. Here is GAME \ n", gamename );

Setbuf (stdin, NULL );

Gint key = 0;

Gint rndnumber;

Gint man_number, comp_number;

Gint man_count = 0, comp_count = 0;

Gboolean man_end = FALSE, comp_end = FALSE;

Gboolean gameover = FALSE;

Gamerand = g_rand_new ();

Do {

If (! Man_end ){

G_print ("% s, Press Y/y to draw a number, press another key to no longer draw a number! \ N ", gamename );

Key = getchar ();

Getchar ();

// Players draw numbers

If (key = 'y' | key = 'y '){

Rndnumber = g_rand_int_range (gamerand, 1, 11 );

Man_number = rndnumber;

Man_count + = man_number;

G_string_append_printf (man_list, "% d", man_number );

G_print ("% s, you obtained: % d \ n", gamename, man_number );

} Else

{

G_print ("players give up smoking! \ N ", comp_number );

Man_end = TRUE;

}

}

// Draw numbers from the computer

If (comp_count <= 17 ){

Rndnumber = g_rand_int_range (gamerand, 1, 11 );

Comp_number = rndnumber;

Comp_count + = comp_number;

G_string_append_printf (comp_list, "% d", comp_number );

G_print ("PC: % d \ n", comp_number );

} Else

{

G_print ("computer does not smoke! \ N ", comp_number );

Comp_end = TRUE;

}

G_print ("$ details of both parties after the current round ends $ \ n ");

G_print ("% s \ n", man_list-> str );

G_print ("% s \ n", comp_list-> str );

If (man_count> 21 & comp_count> 21) | (man_count <= 21 & comp_count <= 21 & man_count = comp_count & man_end & comp_end )){

G_print ("flat hands, computer % dpoints, % s % dpoints \ n", comp_count, gamename, man_count );

Gameover = TRUE;

}

Else if (man_count> 21 & comp_count <= 21) | (man_count <21 & comp_count = 21 )){

G_print ("Computer wins, computer % dpoint, % s % dpoint \ n", comp_count, gamename, man_count );

Gameover = TRUE;

}

Else if (man_count <= 21 & comp_count> 21) | (man_count = 21 & comp_count <21 )){

G_print ("Players win, computer % dpoints, % s % dpoint \ n", comp_count, gamename, man_count );

Gameover = TRUE;

}

Else if (man_end & comp_end ){

Man_count> comp_count? G_print ("Players win, computer % dpoint, % s % dpoint \ n", comp_count, gamename, man_count): g_print ("Computer win, computer % dpoint, % s % d \ n ", comp_count, gamename, man_count );

Gameover = TRUE;

}

Else

{

G_print ("\ n $ computer % dpoints, % s % dpoints $ \ n", comp_count, gamename, man_count );

}

} While (! Gameover );

G_string_free (man_list, TRUE );

G_string_free (comp_list, TRUE );

G_rand_free (gamerand );

Return 0;

}

All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/

Dp @ dp :~ /Gliblearn %./21 dian

What is your name?

Myhaspl

Welcome, myhaspl. Here is the game.

Myhaspl: Press Y/y to extract numbers. Press other keys to remove numbers!

Y

Myhaspl: 8

PC: 8

$ End of current round, details of both parties $

Number drawn by players: 8

Number drawn from the computer: 8

$ Computer, myhaspl $

Myhaspl: Press Y/y to extract numbers. Press other keys to remove numbers!

N

Players quit smoking!

The computer is: 1

$ End of current round, details of both parties $

Number drawn by players: 8

Number drawn from the computer: 8 1

$ Computer, myhaspl $

The computer is: 7

$ End of current round, details of both parties $

Number drawn by players: 8

Number drawn from the computer: 8 1 7

$ Computer, myhaspl $

The computer is: 2

$ End of current round, details of both parties $

Number drawn by players: 8

Number drawn from the computer: 8 1 7 2

$ Computer, myhaspl $

Computers quit smoking!

$ End of current round, details of both parties $

Number drawn by players: 8

Number drawn from the computer: 8 1 7 2

The computer won. The computer won at and myhaspl won.

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.