C - 一个C语言猜字游戏

下面是一个简陋的猜字游戏,玩了一会儿,发现自己打不过自己写的游戏,除非赢了就跑,最高分没有过1000。

说明:srand(time(NULL))和rand(),srand,time和rand都是函数,其中srand和rand配对使用,srand是start random,也就是随机数的初始化,time函数中的NULL表示获取系统时间,所以整个意思是:以系统时间开始初始化,然后获取随机数。随机数结果是一个整数。

#include <stdio.h>
#include <stdlib.h>
#include <time.h> int cash = 100; void play(int bet) { char *C = (char *) malloc(sizeof(char) * 100);
C[0] = 'J';
C[1] = 'Q';
C[2] = 'K';
printf("Shuffling... \n");
srand(time(NULL)); int i;
for (i = 0; i < 5; i++) {
int x = rand() % 3;
int y = rand() % 3;
char temp = C[x];
C[x] = C[y];
C[y] = temp;
} int playersGuess;
printf("What's your guess for the Queen's position? 1, 2 or 3: ");
scanf("%d", &playersGuess); if (C[playersGuess - 1] == 'Q') {
cash = cash + bet * 3;
printf("You win!, the result is: %c %c %c\n", C[0], C[1], C[2]);
printf("Your money left is $ %d.\n", cash);
} else {
cash = cash - bet * 3;
printf("You lose the bet, the result is: %c %c %c\n", C[0], C[1], C[2]);
printf("Your money left is $ %d.\n", cash);
} free(C);
} void main(void) {
int bet;
printf("Welcome to the virtual Casino!\n");
printf("The total cash you have from the start is $%d.\n", cash); while (cash > 0) {
printf("Please enter your bet: $ ");
scanf("%d", &bet); if (bet <= 0) break;
else if (bet > cash) {
printf("Error. The bet is bigger than your cash.");
break;
} play(bet); }
printf("You lost the game. \n"); }

有时间加入一个重新开始的功能,可玩性会高一些。

上一篇:java 网络编程(四)TCP通讯


下一篇:Python Mysql 篇