0
Guessing game
I’m trying to do a guessing game in {c} but can not figure out how to randomly have 3 chests with 3 values of 500,300,0 chips in them. And if chest 1 is opened and has 300 chips in eliminate that chest so there is 2 left with 500 and 0. And if they choose the chest with 0 they lose all their chips and retry. I’m not good with the srand() and rand() and loops can someone help me out with this?
3 Réponses
0
I'll look into it
0
Please add the link you want help with
0
I can help you with that guessing game in C. Here’s a simple example to get you started with srand() and rand() to randomize the values in the chests and implement the game logic:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
// Initialize random seed
srand(time(0));
// Array to store chest values
int chests[3] = {0, 300, 500};
// Shuffle the chest values
for (int i = 0; i < 3; i++) {
int j = rand() % 3;
int temp = chests[i];
chests[i] = chests[j];
chests[j] = temp;
}
int choice;
while (1) {
//Display available chests
printf("Choose a chest (1, 2, 3): ");
scanf("%d", &choice);
if (choice < 1 || choice > 3) {
printf("Invalid choice. Try again.\n");
continue;
}
choice--; //Adjust choice to 0-based index
// Check the chosen chest
if (chests[choice] == 0) {
printf("You opened a chest with 0 chips! You lose all your chips. Try again.\n");
break; //End the game
} else {
printf("You opened a chest with %d chips!\n", chests[choice]);
chests[choice] = -1; // Mark the chest as opened
}
// Check remaining chests
int remainingChests = 0;
for (int i = 0; i < 3; i++) {
if (chests[i] != -1) remainingChests++;
}
if (remainingChests == 0) {
printf("All chests are opened. Game over.\n");
break;
}
}
return 0;
}
This code sets up an array with the chest values and shuffles them randomly. Then it enters a loop to let the user choose a chest. If they open a chest with 0 chips, the game ends.
I also love playing games, and I often visit https://foresthillarena.com/new-online-casinos/ to find the best gaming sites based on real player reviews. Once I find a good site, I can spend hours enjoying the games there. It's really fun!