Thread: Tortoise and hare race (track not refreshing correctly)

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    Question Tortoise and hare race (track not refreshing correctly)

    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. Here is the code.
    Code:
    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
    Here is the refresh line.
    Code:
        Console::setCursorPosition(x, y); // refreshes the current line
        cout << "\r";
    The function then goes on to display the race track and the x position of the current objects turn. but it only displays a T and not a H.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You haven't really given enough information for us to help you. If you can you should post your entire program. You should also explicitly mention the OS you are using (presumably Windows) and whether you want it to be able to run on other OS's (e.g., linux).

    There's some possibly strange stuff in the code you've posted, although that depends on exactly what is going on in the code you didn't post. For instance, it doesn't seem to make sense to have a delay (Sleep) after the getMove calls if all those are doing is generating a random move and not actually moving anything on the screen.

    If you don't want to publicize your code because you don't want other people to see it, you could put it on a file hosting site and PM me a link to it. TinyUpload seems pretty quick and easy, no sign-up required, 50MB max. Only people that you send a link to can access the file and it will be deleted if no one accesses it for 100 days.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    (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
    
    
    }

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Well, I still can't run it.
    Where's console.h, Hare.h, and Tortoise.h?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tortoise and Hare Race
    By leejhadee in forum C Programming
    Replies: 1
    Last Post: 09-09-2014, 05:59 AM
  2. The hare and the tortoise
    By Messi 10 in forum C Programming
    Replies: 3
    Last Post: 12-08-2012, 08:56 AM
  3. Tortoise and Hare Project
    By fenixataris182 in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2005, 12:05 PM
  4. Race between the hare and the tortoise: not working...
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-14-2002, 12:29 PM
  5. Tortoise and the Hare
    By ghettoman in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2001, 01:26 PM

Tags for this Thread