Usando las funciones srand() y rand() en C, se puede crear un juego simple pero interesante. Este juego se llama «Juego de adivinanzas».
Reglas del juego :
- Hay tres agujeros. Una rata está escondida en uno de esos tres agujeros.
- La Rata cambia su posición cada vez.
- Tienes que adivinar el agujero en el que se esconde la Rata entre los tres agujeros.
- El agujero en el que está presente Rat se denomina ‘R’ y los dos restantes se denominan ‘N’.
- Tiene algo de efectivo (inhand_cash) con usted.
- Haces una apuesta (amount_bet) para jugar este juego cada vez que adivinas.
- Si su conjetura es incorrecta, pierde la cantidad_apuesta de su inhand_cash.
- Si aciertas, ganas el doble de la cantidad_apuesta en tu inhand_cash.
- Sigue jugando y sigue ganando hasta que te quedes sin dinero.
A continuación se muestra el código en C para este juego simple e interesante:
Nota: Como este juego toma información de los jugadores para su inhand_cash, bet_amount y la ubicación adivinada de la rata, esto no se ejecutará en el compilador en línea.
// Cpp program for guessing game // using rand() and srand() #include <stdio.h> #include <stdlib.h> #include <time.h> 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; }
Nota: Esta salida no se toma de la
salida del compilador en línea:
----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 :-)
Publicación traducida automáticamente
Artículo escrito por mazhar_mik y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA