我正在用C编写一个掷骰子游戏,我有几个问题。我认为我不正确地使用函数和数组。它可能与我使用返回值的方式有关?我不认为我的if和while循环是关闭的,但它们可能是。这是我的问题,我是一个初学者,我的智慧结束了:
这是我的代码: 我将地点下注设置为自动为1000以便快速运行。 (我没有包含我的头文件,他们有原型。)
//
// main.c
// craps.c
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "place.h"
#include "dice.h"
//DECLARE VARIABLES
int roll, shoot, bank;
//DECLARE VARIABLES
int bank, payout;
int pass, field, location;
int places [10] = {0, 0 ,0 ,0 ,0, 0, 0, 0, 0, 0};
int placer, location, amount, craps;
//MAIN FUNCTION CRAPS!
int main() {
int craps = 1;
//DEBUG:SET ALL PLACES
// clear_bets(&places [10]);
//initialize cash
printf("*****CRAPS****\n");
printf("You have $%d\n", bank);
while (craps == 1){
// clear_bets(&places [10]);
printf("Would you like to make a Place bet? 1=YES 0=NO \n");
int i = 0;
scanf("%d", &i);
while (i == 1) {
print_place_bets (&places [10]);
place_bet (&places [10], location, amount);
printf("Would you like to make another Place bet? 1=YES 0=NO \n");
scanf("%d", &i);}
pass_line(pass);
printf("Pass %d\n", pass);
field_bet(field);
printf("field = %d\n", field);
//SHOOT DICE
roll_dice();
//COMPUTES PLAYOUT
bank = bank + compute_payout(&places[10], pass, roll, field, bank);
printf("Now you have $%d\n", bank);
printf("Would you like to shoot again? 1=YES 0=NO\n");
scanf("%d", &craps);}
}
&#13;
////place.c
#include <stdio.h>
#include "place.h"
//Clears all bets (set places to zero)
void clear_bets(int places []){
# ifdef DEBUG
printf("*****Clearing Bets*****\n");
int i;
for(i=4;i<11;++i) {
//MOD
places [i] = 1000;
if (i!=7){
printf("Place %d has $%d\n", i, places[i]);
i = 0;}}}
#endif
//Places the given amount in the given location
void place_bet (int places [], int location, int amount){
printf("Which Place would you like to bet on? (4-6 8-10)\n");
scanf("%d", &location);
printf("How much would you like to bet?\n");
scanf("%d", &amount);
places[location] = amount;
printf("You bet $%d on Place %d\n", places [location], location);}
//Reduces the bet at location by given amount (not to go below zero)
void remove_bet (int places[], int location, int amount){
int i;
printf("Would you like to reduce any Place bets? 1=YES 0=NO\n");
scanf("%d", &i);
while (i == 1) {
printf("Which Place bet would you like to reduce? (4-6 8-10)\n");
scanf("%d", &location);
printf("The current bet on Place %d: $%d\n", location, places [location]);
while (places[location] >= 0){
printf("How much would you like to reduce the bet?\n");
scanf("%d", &amount);
places[location] = places[location] - amount;
// something?
printf("Place bets cannot be negative, try again\n");
places[location] = places[location] + amount;}
printf("The new bet on Place %d: $%d\n", location, places [location]);
printf("Would you like to reduce a bet on any other Place? 1=YES 0=NO\n");
scanf("%d", &i);}
}
//Returns the current bet at the given location
int get_bet(int places[], int location){
printf("Which Place bet would you like to know?");
scanf("%d", &location);
printf("The current bet on Place %d: $%d\n", location, places [location]);
return places[location];
}
//Prints out all place bets to the screen (using some combinations of printfs
void print_place_bets(int places[]){
printf("*****Place Bets*****\n");
int i;
for(i=4;i<11;++i) {
if (i!=7){
printf("Place %d has $%d\n", i, places[i]);}}}
//FIELD BET
int field_bet(int field){
int i;
printf("Make a Field bet? 1=YES 0=NO \n");
scanf("%d", &i);
if (i==1){
printf("How much are you betting on the Field?\n");
scanf("%d", &field);
printf("You have %d on the Field\n", field);}
else {field = 0;}
return field;
}
//PASS LINE
int pass_line(int pass){
int i=0;
printf("Bet on the Pass line? 1=YES 0=NO \n");
scanf("%d", &i);
if (i==1){
printf("How much are you betting on the Pass Line?\n");
scanf("%d", &pass);
printf("You bet %d on the pass line\n", pass);}return pass;}
//COMPUTE BET
//Given the rolled value, returns the payout based on current bets
int compute_payout(int places[], int pass, int field, int roll, int bank){
int payout = 0;
//PLACES[roll] = roll? where to look?
//knows that places[roll] = bet in place.c but not in craps.c
// print_place_bets(&places [10]);
// printf("debug:compute payout %d\n", places[roll]);
// printf("debug: %d\n", roll);
// COMPUTE PAYOUT FOR PLACE BET IF THERE IS A BET ON THE PLACE THAT WAS ROLLED:
if (places[roll] != 0){
//Places 4 & 10 $9:$5 Ratio
if (roll == 4 || roll == 10){
payout = (((places[roll])*9)/5);
}
//Places 5 & 9 $2:$1 Ratio
//WORKS FOR 9
if (roll == 5){
payout = ((places[roll]*2));
}
//PLACE 9 only pays $18 WHY?
else if(roll == 9){
payout = ((places[roll]*2));
}
else if (roll == 6){
payout = ((places[roll])*7)/6;
}
else if (roll == 8){
payout = ((places[roll])*7)/6;
}
printf("Won bet on Place %d\n", roll);
printf("You won $%d from Place bets\n", payout);
}
//Field Bet Payout
//if there is a bet on the field, reward.
//FIELD BETS: 2 3 4 9 10 11 12
// 2:1 for 2 or 12
//1:1 for 3 4 9 10 11
// if (field !=0){}
if (field != 0){
printf("field = %d\n", field);
if (roll == 2 || roll == 12){
payout = payout + 2 * field;}
else if (roll == 3 || roll == 4 || roll == 9 || roll == 10|| roll == 11){
payout = payout + field;
printf("You won on the Field %d\n", payout);}
else {
printf("You lost on the Field\n");}
}
//PASS LINE PAYOUT
//If there is a bet on the pass line, reward if rolled.
//1:1 for 7 11
while(pass != 0){
// printf("PASS LINE!");
if (roll == 2 || roll == 3|| roll ==12){
printf("You've crapped out!\n");
//LOOSE BET
payout = payout - pass;
}
else if (roll == 7 || roll == 11){
printf("Natural! Pass line wins!\n");
printf("You won $%d\n", pass);
//WRITE REWARD
payout = payout + pass;
}
break;
}
roll = 0;
printf("You had $%d before payout\n", bank);
return (payout);
}
&#13;
//
// dice.c
// driver1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
//DECLARE VARIABLES
int roll, shoot, total;
//DEFINE FUNCTIONS
int roll_dice(){
srand(time(NULL));
printf ("*****Shooting*****\n");
roll = (rand() % 6 + 1) + (rand() % 6 + 1);
printf ("Rolled %d\n", roll);
return 0;}
&#13;
答案 0 :(得分:1)
像这样的行:
place_bet (&places [10], location, amount);
正在传递places
的第11个元素的地址,该元素超过了数组的末尾。然后place_bet
函数将此地址用作其places
参数的起始点,这意味着它将在数组末尾写入更远的地方。在places
数组的末尾写入是未定义的行为,并且可能导致其他变量的值在其内存被覆盖时发生更改。
要解决此问题,请更改函数调用(而不是定义或声明)以传递数组,而不是通过数组末尾的元素地址:
place_bet (places, location, amount);