c - C 中数组索引自动赋值?

标签 c

我正在用 C 语言编写一个掷骰子游戏,但遇到了几个问题。我认为我不正确地一起使用函数和数组。这可能与我使用返回值的方式有关?我不认为我的 if 和 while 循环已关闭,但它们可能是这样。这是我的问题,我是初学者,束手无策:

  • roll 返回的值等于 field。
  • 当 if 语句为真时,Pass 不会奖励。
  • 第 9 名成为掷骰并根据掷出的值(而不是下注金额)支付奖金。它不断返还 18,如 9 * 2,而不是 2 *(下注)。

这是我的代码: 为了跑得快,我把下注自动设置为1000。 (我没有包含我的头文件,它们有原型(prototype)。)

//
//  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);}
    
    }
////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);

}


    
//
//  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;}

最佳答案

像这样的行:

place_bet (&places [10], location, amount);

传入places第11个元素的地址,该元素位于数组末尾。然后,place_bet 函数使用该地址作为其 places 参数的起点,这意味着它将进一步写入数组末尾。写入超过 places 数组的末尾是未定义的行为,并且可能会导致其他变量的值在其内存被覆盖时发生变化。

要解决此问题,请更改函数调用(而不是定义或声明)以传递数组,而不是传递超出数组末尾的元素的地址:

place_bet (places, location, amount);

关于c - C 中数组索引自动赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29202717/

相关文章:

c - 简单的C程序过早退出

c - C中两个字符串的字母顺序比较

在低级文件复制程序中使用 Gotoxy() 时生成代码错误

c - 在 C 中显示大型数组的错误

通过与小于它的素数的整除来检查一个数是否为素数,如果在 C 中证明是素数,则将其存储在数组中

c - 如何在C中编辑特定的二进制数据而不删除/覆盖整个文件?

c - Bison 规范和优先顺序

c++ - Visual Studio 2015 : Extern "C" and the "export" keyword

c - 为什么运行它不会发出段错误?

c - 扫描字符导致 devC 出现问题