So, I wrote a function (and an RNG function, which the aforementioned function calls) to print a random number of asterisks to the console window, until it hits 90 spaces. The asterisks represent the movement of a car, and the 90 spaces is the length of the track. The code I've included below prints a random number of asterisks until it hits 90 spaces, assuming the fnvMoveSpaces() function is called in main and the user presses a key to resume the loop after each system("PAUSE") until 90 spaces is hit.
My question is, looking at the provided code, how would I get four separate lines of totally independent RNG character printing on the same page of the console window? It needs to look like a legitimate race, on the same screen.
What I've tried:
1) Separate functions for each line, called in main:
- Won't work, as they don't happen at the same time. Results in four different pages. I.e. the user has to press a key to get through system("PAUSE") until it hits 90 spaces, then the next function does the same, then the next, and the next. Also, if the loop/function call is outside of the fnvMoveSpaces() main loop, they don't print to the same page.
2) Putting four of the same for loops in the fnvMoveSpaces() function:
- This prints four lines to the same screen, but they all move the same increment, because they are pulling from the same RNG value.
Basically, the RNG values for each line need to be totally independent of one another. Would having a different seed value for each line be the answer? I have no idea...
/* - - 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");
}
}
Thanks,
Bagger
Aucun commentaire:
Enregistrer un commentaire