所以,我写了一个函数(以及一个RNG函数,前面提到的函数调用)来打印一个随机数量的星号到控制台窗口,直到它达到90个空格。星号代表汽车的运动,90个空间代表赛道的长度。假设在main中调用fnvMoveSpaces()函数并且用户按下一个键以在每个系统之后恢复循环(" PAUSE& #34;)直到达到90个空格。
我的问题是,查看提供的代码,如何在控制台窗口的同一页面上打印四个完全独立的 RNG 字符的单独行? 它需要在同一个屏幕上看起来像一个合法的种族。
我尝试过的事情:
1)每行的单独函数,在main中调用:
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个空格,程序才会关闭。另外请记住,每当用户在系统之后按下一个键(&#34; PAUSE&#34;),它就会以随机的增量打印,直到90.所以,不是所有的星号都会立刻打印出来。
我希望它输出的是这样的:
... * **
* ......... **
** ........................ *
** .............. *
每一行随机生成自己独立的移动增量,直到命中90个空格。
希望有所帮助。
谢谢,
巴格
答案 0 :(得分:0)
好的,我明白了。请记住,在不久的将来,我打算用一些文件I / O替换结构。此外,fniRandGenMove与问题中的相同,只是将种子移动到main,因此它只播种一次。
但它完美地运作......汽车&#39;在控制台窗口比赛!它实际上非常整洁。
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);
}