简体   繁体   English

棋盘游戏程序中的数组

[英]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} ). 我想对一个数组进行此操作,所以我做了下面可以看到的事情( 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); 但是以某种方式我得到了,当我使用score [relplayers(数组中的一个计数器,因此程序通过转弯)]] = score [relplayers](旧分数)+抛出(我掷出的骰子的值); 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++. 这是C,而不是C ++。 I assume you actually want C. 我认为您实际上想要C。

I´ve found following problems: 我发现以下问题:

You´re using scanf wrong. 您使用的scanf错误。 Eg. 例如。 if allscore is an integer, scanf("%d", allscore); 如果allscore是整数,则scanf("%d", allscore); should be scanf("%d", &allscore); 应该是scanf("%d", &allscore); ( scanf is different from printf here). scanf与此处的printf不同)。

srand seeds the PRNG. srand 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); 如果您没有充分的理由使用exit(EXIT_FAILURE); in main, don´t do it. 总的来说,不要这样做。 It´s exit , and your case isn´ta failure in my opinion. 它已经exit ,我认为您的案件并非失败。 Use return EXIT_SUCCESS; 使用return EXIT_SUCCESS; . And at the end of main, there should be return too (for the case the loops finish without being aborted). 并且在main的末尾也应该有返回(对于循环结束而不会中止的情况)。

In badfields and goodfields , you´re doing something very strange. badfields和好goodfields ,您做的事情很奇怪。

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. 如果要由10个随机数组成的数组,则初始化是错误的,更重要的是,您将返回一个地址而不是一些生成的随机数。 The next problem is that numbers is local; 下一个问题是numbers是本地numbers if you intend to return multiple values you´ll get problems here too. 如果您打算返回多个值,您也会在这里遇到问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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