Turtle vs Hare

Hi i want to make a program that simulates a hare racing a rabbit. The actual program would look like this on the screen.

|H===================|

|R===================|

the letters represent rabbit and hare respectivly and advance down the track based on a random number genearator from 1 to 50. If it geneartes a 25 or greater the letter moves a space and if its lower it stays in place. It does this for both tracks until one finishes.

the only problem is i cant get the letters to move. help?







Post your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	string track = "|===============|";
	int randNum;
	

	//rand  num gen
	srand(int(time(0)));
    randNum = 50 + rand() % (50 - 1 + 1);

	//determine whether rabbit moves
	while ( randNum >= 25)
	{
		for(int x = 1; x < 17; x++)//This is the part i cant figure :/
		{
		track.insert(x, 'R');//<--- no idea what to do
		}
	}

	cout << track << endl;
	
	return 0;
}


i didnt include the headers

theres all i have, just working on the rabbit track. im a beginner. like i said i just dont know how to get the letter to move. Ive been experimenting with different things for a couple days.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <windows.h>

using namespace std;


int main() {

  // Create Vars
  int   trackLength = 10;
  int   rabbitPos   = 0;
  char  rabbit      = 'R';
  char  track       = '=';
  char  finishLine  = '|';

  // Clear console initially
  system("cls");


  while (rabbitPos <= trackLength) {
    for (int i = 0; i <= trackLength; ++i) {
      if (i == rabbitPos)
        cout << rabbit;
      else if (i == trackLength)
        cout << finishLine;
      else
        cout << track;
    }

    cout << endl;
    rabbitPos++;

    // Move cursor back to beginning to redraw the race track
    COORD coord = { 0, 0 };
    SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );

    // Delay
    Sleep(600);
  }

  return 0;
}
nice but they only are supposed to move if the random number generated is > 25. In that code the rabbit and hare would tie everytime, right?
Yes. I'm not going to code the whole solution for you. I've provided you with a basic rundown on how to achieve the moving across the track as you have inquired about.

oh ok i appreciate the help. i just thought u missed that part
Topic archived. No new replies allowed.