简体   繁体   中英

Arrays in board game program

So, I have been trying to program this game, in which the user has to enter the score he wants to play to. I would like to do this with an array so I did what you can see below ( score[4] = {0, 0, 0, 0} ).

But somehow I get, when I use score[relplayers(a counter in the array, so the programm passes turns)] = score[relplayers](the old score) + thrown (value of the dice i rolled); I have no idea why this doesn't work.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>



int throwing () {

srand(time(NULL));

int value = rand() % 6 + 1;

return value;

}



int main() {

int abplayers, relplayers, thrown, playersshown, abscore, rounds;
printf("Enter the number of fields: ");
scanf("%d", abscore);
int score [4] = {0, 0, 0, 0};

for (rounds = 0; rounds < 50; rounds++)
{
    for(relplayers = 0; relplayers < 4; relplayers++)

    {

        int playershown = relplayers + 1;
        getchar();
        thrown = throwing();
        printf("\nPlayer nº%d threw a %d", playershown, thrown);
        score[relplayers] = score[relplayers] + thrown;
        printf("\nPlayer nº%d is in %d field", playershown, score);
        if (score[relplayers] >= abscore)
        {
            printf("Player nº%d won", playershown);
            exit(EXIT_FAILURE);
        }
    }

}
}

Another problem with that program are special fields. I wanted to include these fields so that the player gets trapped in these fields or gets boosted up in these. I have tried to put this in my program, but it doesn't seem to work.

int enter() {
int allscore;
printf("Number of fields: ");
scanf("%d", allscore);
return allscore;
}

int badfields () {
  int abscore;
  srand(time(NULL));
  abscore = enter();
  int numbers[10] = {rand() % abscore + 1};
  return numbers;
}
int goodfields () {
 int abscore;
 srand(time(NULL));
 abscore = enter();
 int fields[10] = {rand() % abscore + 1};
 return fields;
}

int main()
...




if (score[relplayers] == goodfields())
        {
            printf("Player %d is boosted 5 fields backwards", playershown);
            score[relplayers] = score[relplayers] - 5;
        }
         if (score[relplayers] == goodfields())
        {
            printf("Player %d is boosted 5 fields forwards", playershown);
            score[relplayers] = score[relplayers] + 5;
        }

This is C, not C++. I assume you actually want C.

I´ve found following problems:

You´re using scanf wrong. Eg. if allscore is an integer, scanf("%d", allscore); should be scanf("%d", &allscore); ( scanf is different from printf here).

srand seeds the PRNG. In many cases, this is something which should be done exactly one time in the whole program. Not in each function using random numbers. Change that, because it affects the randomness of your numbers.

If you don´t have a good reason to use exit(EXIT_FAILURE); in main, don´t do it. It´s exit , and your case isn´ta failure in my opinion. Use return EXIT_SUCCESS; . And at the end of main, there should be return too (for the case the loops finish without being aborted).

In badfields and goodfields , you´re doing something very strange.

int badfields () {
  ...
  int numbers[10] = {rand() % abscore + 1};
  return numbers;
}  

What should this even do? If you want an array of 10 random numbers, the initialization is wrong, and even more important you´re returning an address instead of some generated random number. The next problem is that numbers is local; if you intend to return multiple values you´ll get problems here too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM