因此,我正在为我的c编程类编写一个赌博程序。它的菜单必不可少,您必须下注,选择一只狗,然后程序会选择一个获胜者,并告诉您您赢了(多少)或输了。我使用ran()函数,但是我设置为要赢得10%的时间的特定狗比任何其他狗(包括应该赢得40%的时间的狗)的获胜率更高。
/*Programmer: John S. Bolen*/
/*Date: 3/1/19*/
/* User Will Choose to gamble, View Results, Or Quit program.
There are 9 Dogs to Chose from with a set %chance to win and
a set Payout Rate */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define SIZE 1000
#define PAUSE system("pause")
#define CLS system("cls")
#define FLUSH nothingFlush()
#define SIZE 1000
void nothingFlush() {
char nothing;
while (scanf("%c", ¬hing) == NULL);
}
typedef struct {
char name[100];
float payout;
float winOdds;
} DOG;
//Prototype Functions
void displayAllRaces(DOG dog[], int winners[], int counter, int countOne,
int countTwo, int countThree, int countFour, int countFive, int countSix,
int countSeven, int countEight, int countNine);
void displayMenu();
void getBet(float *bet);
char getChoice();
float getOdds();
void pickDog(DOG dog[], int *counter, int winners[], float *bet, int *countOne,
int *countTwo, int *countThree, int *countFour, int *countFive, int *countSix,
int *countSeven, int *countEight, int *countNine);
main() {
char userChoice;
DOG dog[SIZE];
int counter = 0;
int countOne = 0, countTwo = 0, countThree = 0, countFour = 0,
countFive = 0, countSix = 0, countSeven = 0, countEight = 0,
countNine = 0;
int winners[SIZE] = { 0 };
float bet = 0;
do {
userChoice = getChoice();
switch (userChoice) {
case 'G': // Gamble
getBet(&bet);
pickDog(dog, &counter, winners, &bet, &countOne, &countTwo, &countThree,
&countFour, &countFive, &countSix, &countSeven, &countEight, &countNine);
break;
case 'R': //Results
displayAllRaces(dog, winners,counter, countOne, countTwo, countThree, countFour,
countFive, countSix, countSeven, countEight, countNine);
break;
case 'L': //Leave
printf("\n\tThank you for visiting the Dog Track\n");
break;
default:
printf("\n\tERROR! -- Enter A Valid Selection...\n");
break;
}//End Switch
PAUSE;
} while (userChoice != 'L');
}
void displayAllRaces(DOG dog[], int winners[], int counter, int countOne,
int countTwo, int countThree, int countFour, int countFive, int countSix,
int countSeven, int countEight, int countNine) {
int i = 0;
CLS;
printf("\n\tRace Results: \n");
printf("\tDog\t\t\t\t\tNum of Wins\n");
if (counter > 0) {
printf("\t%s\t\t\t%i\n", dog[0].name, countOne);
printf("\t%s\t\t\t%i\n", dog[1].name, countTwo);
printf("\t%s\t\t%i\n", dog[2].name, countThree);
printf("\t%s\t\t%i\n", dog[3].name, countFour);
printf("\t%s\t\t%i\n", dog[4].name, countFive);
printf("\t%s\t\t%i\n", dog[5].name, countSix);
printf("\t%s\t\t%i\n", dog[6].name, countSeven);
printf("\t%s\t\t\t%i\n", dog[7].name, countEight);
printf("\t%s\t\t\t%i\n", dog[8].name, countNine);
}
else {
printf("\n\t\n");
}
}
void displayMenu(){
CLS;
printf("\n\t==================================================\n");
printf("\n\t== MAIN MENU ==\n");
printf("\n\t==================================================\n");
printf("\n\t[G]amble\n");
printf("\n\t[R]esults\n");
printf("\n\t[L]eave\n");
printf("\n\tEnter Your Selection: ");
}
void getBet(float *bet) {
float result = 0;
printf("\n\tEnter the amount you want to gamble: ");
printf("\n\t(Bet has to be between $1 and $1000)\n");
scanf_s("%f", &result); FLUSH;
if(result < 1 || result > 1000){
printf("\n\tERROR-- Please enter a valid amount\n");
printf("\n\tEnter the amount you want to gamble: ");
printf("\n\t(Bet has to be between $1 and $1000)\n");
scanf_s("%f", &result); FLUSH;
}
printf("\n\tThank you for placing your wager.\n\tGood luck!\n");
*bet = result;
PAUSE;
}
char getChoice() {
char result;
displayMenu();
scanf_s("%c", &result); FLUSH;
result = toupper(result);
return result;
}
float getOdds() {
float odds[9] = { 40, 10, 8, 6, 1, 4, 8, 10, 13 };
return odds[9];
}
void pickDog(DOG dog[], int *counter, int winners[], float *bet, int *countOne,
int *countTwo, int *countThree, int *countFour, int *countFive, int *countSix,
int *countSeven, int *countEight, int *countNine) {
int i = 0;
int choice;
float betMoney = 0;
int winner = 0;
CLS;
printf("\n\tPick a dog from the list:");
strcpy(dog[0].name, "\n\t1.Gandalf the Great");
strcpy(dog[1].name, "\n\t2.Fenrir Greyback");
strcpy(dog[2].name, "\n\t3.Marley the Magnificent");
strcpy(dog[3].name, "\n\t4.Clifford the Big Red Dog");
strcpy(dog[4].name, "\n\t5.Petey the Little Rascal");
strcpy(dog[5].name, "\n\t6.Courage the Cowardly Dog");
strcpy(dog[6].name, "\n\t7.Old Yeller Before the Bullet");
strcpy(dog[7].name, "\n\t8.Pongo the Dalmatian");
strcpy(dog[8].name, "\n\t9.Nymeria Stark\n");
for (i = 0; i < 9; i++) {
printf("%s", dog[i].name);
}
printf("\n");
scanf_s("%i", &choice); FLUSH;
do {
if (choice < 1 || choice > 9) {
printf("\n\tPlease, try again...\n");
scanf_s("%i", &choice);
}
} while (choice < 1 || choice > 9);
betMoney = *bet;
winner = 1 + rand() % (100 - 1 + 1);
if (winner <= 40) {
winner = 1;
(*countOne)++;
}
else if (winner <= 50) {
winner = 2;
(*countTwo)++;
}
else if (winner <= 48) {
winner = 3;
(*countThree)++;
}
else if (winner <= 64) {
winner = 4;
(*countFour)++;
}
else if (winner <= 65) {
winner = 5;
(*countFive)++;
}
else if (winner <= 69) {
winner = 6;
(*countSix)++;
}
else if (winner <= 77) {
winner = 7;
(*countSeven)++;
}
else if (winner <= 87) {
winner = 8;
(*countEight)++;
}
else if (winner <= 100) {
winner = 9;
(*countNine)++;
}
winners[*counter] = winner;
(*counter)++;
if (choice == winner) {
printf("\n\tYou picked a WINNER!\n");
if (winner == 1) {
(*bet) = betMoney * 2;
printf("You win $%.2f\n", *bet);
}
if (winner == 2) {
(*bet) = betMoney * 5;
printf("You win $%.2f\n", *bet);
}
if (winner == 3) {
(*bet) = betMoney * 10;
printf("You win $%.2f\n", *bet);
}
if (winner == 4) {
(*bet) = betMoney * 15;
printf("You win $%.2f\n", *bet);
}
if (winner == 5) {
(*bet) = betMoney * 50;
printf("You win $%.2f\n", *bet);
}
if (winner == 6) {
(*bet) = betMoney * 20;
printf("You win $%.2f\n", *bet);
}
if (winner == 7) {
(*bet) = betMoney * 10;
printf("You win $%.2f\n", *bet);
}
if (winner == 8) {
(*bet) = betMoney * 5;
printf("You win $%.2f\n", *bet);
}
if (winner == 9) {
(*bet) = betMoney * 3;
printf("You win $%.2f\n", *bet);
}
}
else {
printf("\n\tYou picked a LOSER!\n");
(*bet) = (*bet) - betMoney;
}
}
我已经在此处包含了程序的完整代码(因此您可以根据需要对其进行测试),但是仅需要修复我的rand()函数的帮助。如您所见,我想随机选择一个介于1到100之间的数字。如果数字<= 40,那么它的狗获胜的几率是40%;如果数字是<= 50(意味着其<= 50,但> 40),那么那只狗2获胜的几率是10%。我已经运行了无数次该程序,并且在我运行它x次之后,dog 2似乎总是获得最多的胜利。任何帮助或建议,将不胜感激!