(Rest of the code and a more expanded explanation)
i have to create a simulated race between a hare and tortoise based on a random set of moves. The two race across the screen but i can't figure out how to keep both objects on the screen at once. i'm currently trying "\r" in different places but so far it only manages to keep the tortoise on screen and erases the hare position. It's for uni coursewrokHere is the coursework. the program must simulate two objects taking turns and display this in the console window. I need to find a way to display a kind of race track on the screen with a H representing the hare and T for tortoise. each time one moves i need to delete the previous H. so far i've managed to have the console completely refresh and display the track + 1 object, but i need to have both on the screen.
i'm programming in visual studio 2015 on windows 10, and just need to have it compile correctly and display a winner. simulating a random race.
Code:
#include <iostream>
#include "console.h" // holds commands to change the console display
#include "Hare.h" // holds the hare class
#include "Tortoise.h" // holds the tortoise class
using namespace std;
// function prototype to display race track
void displayDots(int x, int y);
int main()
{
const int END_RACE = 100;
const int *endptr = &END_RACE;
int hareGo = 8; // holds the y position for the hare
int tortGo = 10; // holds the y position for the tortoise
int turns = 0; // count how many turns were done
// initialize the objects
Hare h1;
Tortoise t1;
// pointer to the object x positions
int &HposPtr = *h1.xptr;
int &TposPtr = *t1.xptr;
displayDots(HposPtr, 8);
do
{
h1.getMove(); // get the x value for the hare
Sleep(500);
t1.getMove(); // get the x value for the tortoise
Sleep(500);
displayDots(HposPtr, hareGo); // this currently refreshes only the hare line (line 8)
Console::setColour(Console::GREEN);
cout << "H"; // displays H along the race track
displayDots(TposPtr, tortGo); // refreshes the tortoise line and places a T at x position
Console::setColour(Console::LIGHT_RED);
cout << "T";
turns++;
} while (h1.xptr <= endptr); // ends the loop at the finish
if (h1.xptr >= endptr)
{
cout << "turns taken " << turns << endl;
cout << "winner" << endl;
}
system("pause");
return 0;
}
/* function to display the race track on the window
and set the position for each object after it takes
its move */
void displayDots(int x, int y)
{
//Console::clear();
Console::setCursorPosition(x, y); // refreshes the current line
cout << "\r";
Console::setCursorPosition(6, 98);
Console::setColour(Console::AQUA);
cout << "~~~~~" << endl;
Console::setColour(Console::LIGHT_PURPLE); // sets the cursor position to the end of the
Console::setCursorPosition(7, 100); // track. then displays finish vertically with
cout << "F" << endl; // "finish poles" at either side to simulate
Console::setCursorPosition(8, 100); // a finish line
cout << "I" << endl;
Console::setCursorPosition(9, 100);
cout << "N" << endl;
Console::setCursorPosition(8, 0);
for (int i = 0; i < 100; i++)
{
// display dots across the console for the race track
Console::setColour(Console::LIGHT_AQUA);
cout << ".";
}
cout << endl; // space in between race tracks
cout << endl;
for (int i = 0; i < 100; i++)
{
// display dots again
cout << ".";
}
Console::setColour(Console::LIGHT_PURPLE);
Console::setCursorPosition(10, 100); // other half of the finish line
cout << "I" << endl;
Console::setCursorPosition(11, 100);
cout << "S" << endl;
Console::setCursorPosition(12, 100);
cout << "H" << endl;
Console::setCursorPosition(13, 98);
Console::setColour(Console::AQUA);
cout << "~~~~~" << endl;
cout << endl;
Console::setCursorPosition(y, x); // sets the cursor position back to the current objects
// x and y coordinates
}