c - Arrays in board game program -


so, have been trying program game, in user has enter score wants play to. array did can see below (score[4] = {0, 0, 0, 0}).

but somehow get, when use score[relplayers(a counter in array, programm passes turns)] = score[relplayers](the old score) + thrown (value of dice rolled); have no idea why 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 number of fields: "); scanf("%d", abscore); int score [4] = {0, 0, 0, 0};  (rounds = 0; rounds < 50; rounds++) {     for(relplayers = 0; relplayers < 4; relplayers++)      {          int playershown = relplayers + 1;         getchar();         thrown = throwing();         printf("\nplayer nº%d threw %d", playershown, thrown);         score[relplayers] = score[relplayers] + thrown;         printf("\nplayer nº%d in %d field", playershown, score);         if (score[relplayers] >= abscore)         {             printf("player nº%d won", playershown);             exit(exit_failure);         }     }  } } 

another problem program special fields. wanted include these fields player gets trapped in these fields or gets boosted in these. have tried put in program, doesn't seem 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 boosted 5 fields backwards", playershown);             score[relplayers] = score[relplayers] - 5;         }          if (score[relplayers] == goodfields())         {             printf("player %d boosted 5 fields forwards", playershown);             score[relplayers] = score[relplayers] + 5;         } 

this c, not c++. assume want c.

i´ve found following problems:

you´re using scanf wrong. eg. if allscore integer, scanf("%d", allscore); should scanf("%d", &allscore); (scanf different printf here).

srand seeds prng. in many cases, should done 1 time in whole program. not in each function using random numbers. change that, because affects randomness of numbers.

if don´t have reason use exit(exit_failure); in main, don´t it. it´s exit, , case isn´t failure in opinion. use return exit_success;. , @ end of main, there should return (for case loops finish without being aborted).

in badfields , goodfields, you´re doing strange.

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

what should do? if want array of 10 random numbers, initialization wrong, , more important you´re returning address instead of generated random number. next problem numbers local; if intend return multiple values you´ll problems here too.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -