因此,我编写了一个函数(以及上述函数调用的RNG函数),将随机数量的星号打印到控制台窗口,直到达到90个空格为止。星号代表汽车的运动,90个车位是轨道的长度。下面我所包含的代码打印了任意数量的星号,直到它到达90个空格,假设fnvMoveSpaces()函数在main中被调用,并且用户按下一个键在每个系统(“暂停”)之后恢复循环,直到90空格被击中。
我的问题是,看看提供的代码,我如何在控制台窗口的同一页上得到四行完全独立的RNG字符打印?它需要看起来像一个合法的种族,在同一个屏幕上。
我试过的是:
1)每一行的单独功能,主要调用如下:
2)在fnvMoveSpaces()函数中放置四个相同的for循环:
基本上,每一行的RNG值都需要完全独立于彼此。对每一行都有不同的种子值是答案吗?我不知道..。
/* - - DEFINED - - */
// Constants of RNG for spaces moved
#define TRACK_LENGTH 90
#define MAX_MOVE_SPACES 10
#define MIN_MOVE_SPACES 1
// Assume fnvMoveSpaces call in main
// Function to create random number for car movement
int fniRandGenMove()
{
// Declare
int randInt;
// Initialize random seed
srand(time(NULL));
// Formula for RNG (1-10) based on global-defined numbers
randInt = (rand() % (MAX_MOVE_SPACES - MIN_MOVE_SPACES + 1) + MIN_MOVE_SPACES);
return (randInt);
}
void fnvMoveSpaces()
{
// Declare
int i;
int iMoveSum;
// Outer for loop to maintain the sum of asterisks
for(iMoveSum = 0; iMoveSum <= TRACK_LENGTH; iMoveSum += fniRandGenMove())
{
// Inner for loop to print asterisks
for(i = 0; i < iMoveSum; i++)
{
putchar('*');
}
// Newline for next line of asterisks
printf("\n");
/*
I'm assuming three more for loops... I tried a bunch of
combinations of things, even making new iMoveSums
(2, 3 and 4) and doing for loops.
But, no luck.
I should also not that making four separate functions for each
line of asterisks will not work, unless there is a way to call all
four at once in main. Separate functions results in separate screens
in the console window. In addition, if the four 'putchar' blocks
are not in the same loop as a whole, the first one will print, hit
90 spaces, then the second, etc... They aren't on the same screen.
*/
// System pause to wait for user
system("PAUSE");
// Clear screen
system("CLS");
}
}为了澄清,控制台窗口中的当前输出如下:
*
(以枚举的形式写入;没有句点实际输出。)
直到90个空格被击中,程序才会关闭。还请记住,每次用户按下系统后的键(“暂停”),直到90,它都会以随机递增的方式打印。所以,并不是所有的星号都一次打印出来。
我想要它输出的是这样的东西:
***
***
**.*
*
每一行随机产生自己的独立运动增量,直到90个空格被击中。
希望这能有所帮助。
谢谢,
巴格
发布于 2016-11-22 02:59:26
好吧,我搞定了。请记住,在不久的将来,我打算用一些文件I/O替换这些结构。而且,fniRandGenMove和问题一样,只需将种子移到main,所以它只能种子一次。
但它运作得很完美..。通过控制台窗口的“汽车”赛跑!其实真的很干净。
void fnvMoveSpaces(int iAutoManual)
{
// Declare
int i, j;
// Declare structures
struct Car stCars[4];
stCars[0].iPosition = 0;
stCars[1].iPosition = 0;
stCars[2].iPosition = 0;
stCars[3].iPosition = 0;
stCars[0].iCarNumber = 1;
stCars[1].iCarNumber = 2;
stCars[2].iCarNumber = 3;
stCars[3].iCarNumber = 4;
struct Car furthestCar;
furthestCar.iPosition = 0;
furthestCar.iCarNumber = 0;
do
{
for(i = 0; i < 4; i++)
{
if(stCars[i].iPosition <= TRACK_LENGTH)
{
stCars[i].iPosition += fniRandGenMove();
}
printf("Car %d\n\t", stCars[i].iCarNumber);
for(j = 0; j < stCars[i].iPosition; j++)
{
printf("*");
}
if (stCars[i].iPosition > furthestCar.iPosition)
{
furthestCar.iPosition = stCars[i].iPosition;
furthestCar.iCarNumber = stCars[i].iCarNumber;
}
printf("\n");
}
system("PAUSE");
system("CLS");
} while(furthestCar.iPosition < TRACK_LENGTH);
printf("The winning car is #%d.\n", furthestCar.iCarNumber);
}https://stackoverflow.com/questions/40707073
复制相似问题