Tortoise and Hare Race
May 14, 2015 at 2:56am UTC
I can't get the hare to move at all. I checked and I can't find anything wrong in the code. Anyone willing to help me out here
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <windows.h>
using namespace std;
const int FinishLine = 101;
void Tortoise(int *);
void Hare(int *);
void positions(int *,int *);
int main()
{
int T = 1;
int H = 1;
cout << "Welcome to Eric's Game world! Enjoy your race! " << endl;
unsigned n;
cout << "enter seed : " ;
cin >> n ;
srand(n);
cout << "ON YOUR MARK, GET SET\nGO!!!!\n" ;
while (T != FinishLine && H != FinishLine) {
Tortoise(&T);
Hare(&H);
positions(&T,&H);
}
if (T<H){
cout << "\nTortoise Wins!\n" ;
}
else if (T>H){
cout << "\n Hare Wins!\n" ;
}
else if (T=H){
cout << "It's a tie!\n" ;
}
system("pause" );
return 0;
}
void Tortoise(int *tortoisepointer)
{
int space = ((rand() % 6));
if ( space = 0 )
*tortoisepointer += 7;
else if ( space == 1 )
*tortoisepointer += 5;
else if (space == 2 )
*tortoisepointer += 3;
else if (space == 3 )
*tortoisepointer -= 2;
else if (space == 4 )
*tortoisepointer -= 2;
else if (space == 5 )
*tortoisepointer -= 6;
if ( *tortoisepointer < 1 )
*tortoisepointer = 1;
else if ( *tortoisepointer > FinishLine )
*tortoisepointer = FinishLine;
}
void Hare(int *harepointer)
{
int space = ((rand()% 6));
if (space == 0)
*harepointer += 7;
else if (space == 1)
*harepointer += 5 ;
else if (space == 2 )
*harepointer += 3;
else if (space == 3)
*harepointer -= 2;
else if (space == 4)
*harepointer -= 2;
else if (space == 5 )
*harepointer -= 6;
if ( *harepointer < 1 )
*harepointer = 1;
else if (*harepointer > FinishLine){
*harepointer = FinishLine;
}
}
void positions(int *HarePoint,int *TortoisePoint)
{
if ( *HarePoint == *TortoisePoint )
cout << setw( *HarePoint ) << "X!"
<< setw(101-*TortoisePoint) << '|' ;
else if ( *HarePoint < *TortoisePoint )
cout << setw( *HarePoint ) << "H"
<< setw( *TortoisePoint - *HarePoint ) << "T"
<< setw(101-*TortoisePoint) << '|' ;
else
cout << setw( *TortoisePoint ) << "T"
<< setw( *HarePoint - *TortoisePoint ) << "H"
<< setw(101-*HarePoint) << '|' ;
cout << "\n" ;
}
Last edited on May 14, 2015 at 2:58am UTC
May 14, 2015 at 4:35am UTC
> I can't get the hare to move at all.
1 2
positions(&T,&H);
void positions(int *HarePoint,int *TortoisePoint)
Hare is moving, Tortoise not.
you are printing them backwards.
> I checked and I can't find anything wrong in the code
get used to read the warnings
49: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
note: use '==' to turn this assignment into an equality comparison
67: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
note: use '==' to turn this assignment into an equality comparison
May 14, 2015 at 4:45am UTC
Found it. What it says is that for an if statement, use "==" instead of '='.
May 14, 2015 at 4:54am UTC
thank you guys. My bad.. really appreciate it
Topic archived. No new replies allowed.