📜  在 C 中使用 rand() 和 srand() 猜游戏

📅  最后修改于: 2021-10-19 05:01:02             🧑  作者: Mango

在 C 中使用 srand() 和 rand()函数,可以制作一个简单但有趣的游戏。这个游戏叫做“猜猜游戏”。

游戏规则 :

  • 有三个孔。一只老鼠藏在这三个洞中的一个洞里。
  • 老鼠每次都改变它的位置。
  • 你必须猜出三个洞中藏有老鼠的洞。
  • 老鼠所在的洞被命名为“R”,其余两个被命名为“N”。
  • 你有一些现金(inhand_cash)。
  • 每次进行猜测时,您都会为玩这个游戏而下注 (amount_bet)。
  • 如果您的猜测是错误的,您就会从您的 inhand_cash 中输掉 amount_bet。
  • 如果您猜对了,您将赢得 inhand_cash 中的 amount_bet 的两倍。
  • 继续玩,继续赢,直到你用完现金。

下面是这个简单而有趣的游戏的 C 代码:

注意:由于这个游戏需要玩家输入他们的 inhand_cash、bet_amount 和老鼠的猜测位置,所以这不会在在线编译器中运行。

// Cpp program for guessing game
// using rand() and srand()
#include 
#include 
#include 
  
void GuessGame(int amount_bet, int* inhand_cash)
{
    char Hole[3] = { 'N', 'R', 'N' };
    printf("\nWait !! Rat is shuffling its position...\n");
    srand((time(NULL)));
    int i, x, y, temp;
  
    /*Swapping the Rat's (R's) position  five times using
    the random number for random index*/
  
    for (i = 0; i < 5; i++) {
        x = rand() % 3;
        y = rand() % 3;
        temp = Hole[x];
        Hole[x] = Hole[y];
        Hole[y] = temp;
    }
  
    int PlayerGuess;
  
    printf("\nYou may now guess the hole in which Rat is present: ");
  
    scanf("%d", &PlayerGuess);
  
    if (Hole[PlayerGuess - 1] == 'R') {
        (*inhand_cash) += 2 * amount_bet;
        printf("You win ! The holes are as follows: ");
        printf("\"%c %c %c\" ", Hole[0], Hole[1], Hole[2]);
        printf("\nYour inhand_cash is now = %d \n", *inhand_cash);
    }
  
    else {
        (*inhand_cash) -= amount_bet;
        printf("You Loose ! The holes are as follows: ");
        printf("\"%c %c %c\" ", Hole[0], Hole[1], Hole[2]);
        printf("\nYour inhand_cash is now = %d \n", *inhand_cash);
    }
}
  
int main()
{
    int amount_bet, inhand_cash;
    /*
    You have to guess the hole in which the 
     Rat is hidden among three holes
    The hole in which Rat is present is 
    named as 'R' and rest two are named as 'N'
    If your guess is wrong, you loose the 
    amount_bet from your inhand_cash
    If you guess it right, you win 
     twice the amount_bet in your inhand_cash
    Keep playing and keep winning 
    until you go out of cash
    */
  
    printf("----Enter the inhand_cash you have right now---- : ");
  
    scanf("%d", &inhand_cash);
  
    while (inhand_cash > 0) {
        printf("\nEnter the amount_bet you want to play for : ");
        scanf("%d", &amount_bet);
        if (inhand_cash == 0 || amount_bet > inhand_cash)
            break;
        GuessGame(amount_bet, &inhand_cash);
    }
  
    if (inhand_cash == 0 || amount_bet > inhand_cash) {
        printf("\n\""
               " 🙁 Sorry you don't have enough cash to play more, ");
        printf("Do come next time\""
               "\n");
        printf("Thank You for playing 🙂 \n");
    }
    return 0;
}

注意:此输出并非来自在线编译器
输出:

----Enter the inhand_cash you have right now---- : 1

Enter the amount_bet you want to play for : 1

Wait !! Rat is shuffling its position...

You may now guess the hole in which Rat is present: 1
You Loose ! The holes are as follows: "N N R"
Your inhand_cash is now = 0

" :-( Sorry you don't have enough cash to play more, Do come next time"
Thank You for playing :-)
想要从精选的视频和练习题中学习,请查看C 基础到高级C 基础课程