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?

9th Sep 2019, 4:27 PM
Zachary Wright
Zachary Wright - avatar
6 Answers
0
I'll look into it
26th Sep 2019, 2:47 PM
Felix SchĆ¼ltz
Felix SchĆ¼ltz - avatar
0
Please add the link you want help with
26th Sep 2019, 2:48 PM
Felix SchĆ¼ltz
Felix SchĆ¼ltz - avatar
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!
23rd May 2024, 5:04 PM
Erin Carpenter
0
Hey everyone! If you're looking for a smooth and modern platform for both sports betting and casino games, I highly recommend checking out PinUp India. You can find it at https://pinupindia.co.in/ . The site offers a great selection of sports events, including football, cricket, tennis, and basketball, with real-time updates and odds. It's also packed with casino games, from slots to live dealer games. The interface is user-friendly, and they offer secure payment options along with a helpful support team. If you're new, they even have great bonuses for first-time deposits!
2nd Dec 2024, 11:55 AM
work hard
work hard - avatar
0
Sure! Hereā€™s a simple approach to implement your guessing game in C: Use rand() with srand() to randomize the distribution of chip values in the chests. Keep track of opened chests and ensure they cannot be selected again. Implement the game logic to handle win/lose scenarios and allow retries. Here's a sample implementation: c ŠšŠ¾ŠæŠøрŠ¾Š²Š°Ń‚ŃŒ ŠŗŠ¾Š“ #include <stdio.h> #include <stdlib.h> #include <time.h> void shuffle(int chests[], int size) { for (int i = size - 1; i > 0; i--) { int j = rand() % (i + 1); int temp = chests[i]; chests[i] = chests[j]; chests[j] = temp; } } int main() { srand(time(NULL)); // Seed for randomization int chests[3] = {500, 300, 0}; // The values in the chests int revealedChests[3] = {0, 0, 0}; // To track which chests have been opened shuffle(chests, 3); int choice, retry = 1; while (retry) { printf("\nChoose a chest (1, 2, or 3): "); scanf("%d", &choice); // Validate input if (choice < 1 || choice > 3 || revealedChests[choice - 1]) { printf("Invalid choice. Try again.\n"); continue; } // Reveal the chest int chips = chests[choice - 1]; revealedChests[choice - 1] = 1; printf("You opened chest %d and found %d chips!\n", choice, chips); if (chips == 0) { printf("You lost all your chips! Restarting...\n"); retry = 0; // End the current loop } else { printf("Two chests remain. Can you find the one with the highest chips?\n"); printf("Remaining chests: "); for (int i = 0; i < 3; i++) { if (!revealedChests[i]) printf("%d ", i + 1); } printf("\n"); } } printf("Game over! Want to try something else exciting? Check out platforms like 1win https://1winca.ca/ for more fun games. šŸŽ®\n"); return 0; } Explanation: Shuffling the chests: The shuffle function randomizes the chip values in the chests using Fish
9th Jan 2025, 6:26 PM
Zevc
0
11 Winner is your gateway to endless entertainment and big wins! Dive into a world of thrilling slots, live casino games, and exciting sports betting options. With a sleek design, seamless gameplay, and generous bonuses, 11 Winner caters to both new players and seasoned gamers. Whether youā€™re playing at home or on the go with the mobile app, every moment is packed with excitement. Join https://11winnerapk.co.in/ (11 Winner) today and experience the ultimate in online gaming!
22nd Jan 2025, 11:43 AM
Ananda Bhate