(3) determine whether to win or lose by taking turns
The logic type gboolean is defined as follows:
Typedef gint gboolean;
A standard boolean type. Variables of this type shoshould only contain the value TRUE or FALSE.
All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/
The Code is as follows:
Dp @ dp :~ /Gliblearn % cat 21dian. c
# 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 );
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_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_print ("PC: % d \ n", comp_number );
} Else
{
G_print ("computer does not smoke! \ N ", comp_number );
Comp_end = TRUE;
}
If (man_count> 21 & comp_count> 21) | (man_count = 21 & comp_count = 21 )){
G_print ("flat hands, computer % dpoints, % s % dpoints \ n", comp_count, gamename, man_count );
Gameover = TRUE;
}
Else if (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 ){
G_print ("Players win, computer % dpoints, % s % dpoint \ n", comp_count, gamename, man_count );
Break;
} 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 $ end of current round, computer % d point, % s % d point $ \ n", comp_count, gamename, man_count );
}
} While (! Gameover );
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/
Compile and execute:
Dp @ dp :~ /Gliblearn % gcc 'pkg-config -- cflags -- libs glib-2.0 gthread-2.0 '21dian. c-o 21 dian
Dp @ dp :~ /Gliblearn %./21 dian
What is your name?
Mai Hao
Welcome, Mike. Here is the game.
Press Y/y to extract the number. Press other keys to remove the number!
Y
Hello, what you got is: 7
COMPUTER: 10
$ End of current round, for computer, for mic testing $
Press Y/y to extract the number. Press other keys to remove the number!
Y
Hello, what you got is: 1
PC: 9
$ End of current round, on computer, on mic interaction $
Press Y/y to extract the number. Press other keys to remove the number!
Y
Hello, what you got is: 10
Computers quit smoking!
$ End of current round, on computer, on mic interaction $
Press Y/y to extract the number. Press other keys to remove the number!
Y
Hello, what you got is: 7
Computers quit smoking!
The computer won, the computer won at, and the computer won.
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: 2
PC: 8
$ End of current round, on computer, on myhaspl $
Myhaspl: Press Y/y to extract numbers. Press other keys to remove numbers!
Y
Myhaspl: 2
The computer is: 3
$ End of current round, computer, myhaspl $
Myhaspl: Press Y/y to extract numbers. Press other keys to remove numbers!
Y
Myhaspl: 5
The computer is: 5
$ End of current round, computer, myhaspl $
Myhaspl: Press Y/y to extract numbers. Press other keys to remove numbers!
Y
Myhaspl: 2
PC: 9
Players win, computer, myhaspl
Dp @ dp :~ /Gliblearn %