simple text based car racing game

Ok so I have to make a simple racing game where the user inputs 1, 0, or -1 for the increments in horizontal and vertical acceleration/deceleration to guide the "car" (which is represented as a "0" in the text game) to the finish line.

The part that I'm having trouble with is when the "car" crashes into the side of the wall or barrier. The code is supposed to be so that the "car" is embedded in the side of the wall or barrier but I'm not sure how to make it work. Will someone help me?

The project information is here:
http://www.umich.edu/~engr101/Bielajew/a6.pdf


and what I have so far is here:

the code that is specifically giving me trouble is from lines 116 to 140. Everything above that is working fine I believe.


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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cmath>
using namespace std;


const int row = 52;
const int column = 72;


void printRace(char array1[row][column])   //prints 52 by 72 grid
{
   for (int i = 0; i < row; i++)
   {
      for (int j = 0; j < column; j++)
      {
         cout << array1[i][j];
      }
      cout << endl;
   }
}


void initRace(char array2[row][column])  //prints track layout
{

	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < column; j++)
		{
			if (i == 0)     //top boundary
			{
				array2[i][j] = 'X';
			}
			else if (i == 51 && j < 65)   //bottom boundary
			{
				array2[i][j] = 'X';
			}
			else if (i == 51 && j < 71)  //prints finish line
			{
				array2[i][j] = 'F';
			}
			else if ((i >= 1 && i <= 51) && j == 0)   //left boundary
			{
				array2[i][j] = 'X';
			}
			else if ((i >= 1 && i <= 51) && j == 71)   //right boundary
			{
				array2[i][j] = 'X';
			}
			else if ((i >= 1 && i <= 35) && (j >= 10 && j <= 29))  //left barrier
			{
				array2[i][j] = 'X';
			}
			else if ((i >= 16 && i <= 50) && (j >= 40 && j <= 64))  //right barrier
			{
				array2[i][j] = 'X';
			}
			else
			{
				array2[i][j] = ' ';
			}
		}
	}
}




int main()
{
   char track[row][column];
   initRace(track);
   track[1][1] = '0';
   printRace(track);

   int xVelocity = 0, xAcceleration = 0;
   int yVelocity = 0, yAcceleration = 0;
   int xPosition = 1, yPosition = 1;
   int counter = 0;   //counter for seconds

   bool ontrack;

   while(1)
   {
      cout << "Horizontal and vertical acceleration (-1, 0, 1): ";
      cin >> xAcceleration >> yAcceleration;
      counter++;

      if (xAcceleration < -1 || xAcceleration > 1)
      {
			printRace(track);
         cout << "Crashed after " << counter << " seconds" << endl;
			return -1;
      }
      if (yAcceleration < -1 || yAcceleration > 1)
      {
			printRace(track);
         cout << "Crashed after " << counter << " seconds" << endl;
			return -1;
      }


      xVelocity = xVelocity + xAcceleration;
      yVelocity = yVelocity + yAcceleration;

      xPosition = xPosition + xVelocity;
      yPosition = yPosition + yVelocity;


      track[yPosition][xPosition] = '0';
      printRace(track);


      if ((yPosition == 0 && xPosition < 10) || (yPosition == 0 && xPosition > 30))   //when car is embedded in top boundary
      {
         track[0][xPosition] = '0';
         printRace(track);
         cout << "Crashed after " << counter << " seconds" << endl;
      }

      if (yPosition == 51 && xPosition < 41)  //when car is embedded in bottom boundary
      {
         track[51][xPosition] = '0';
         printRace(track);
         cout << "Crashed after " << counter << " seconds" << endl;
      }

      if ((yPosition >= 1 && yPosition <= 51) && xPosition == 0)  //when car is embedded in left boundary
      {
         track[yPosition][0] = '0';
         printRace(track);
         cout << "Crashed after " << counter << " seconds" << endl;
      }

      if ((yPosition >= 1 && yPosition <= 51) && xPosition == 71)   //when car is embedded in right boundary
      {
         track[yPosition][71] = '0';
         printRace(track);
         cout << "Crashed after " << counter << " seconds" << endl;
      }


   return 0;

}


Last edited on
Topic archived. No new replies allowed.