Stuck with Writing a C++ Candyland game

Nov 5, 2010 at 12:49am
I've been assigned to write a candyland game in which there are 85 squares. The game just couts a table that looks like this...
Move #    	Current Position     Card    New Position 	Candy Bonus
-------------------------------------------------------------------------------------
1	  1	      2	      3
-------------------------------------------------------------------------------------
2	  3	      3	     40		      Rainbow Trail
-------------------------------------------------------------------------------------
3	 40	      5	     45
-------------------------------------------------------------------------------------
etc.



Here's my problem, I'm new at C++ programming and this is the most difficult program we have written by far. I'd like someone to take a look at my code and maybe point out some problems and make some suggestions. As of now my program is in an state in which it doesn't run. I actually have a headache from writing this program and I could really use some suggestions. Thanks guys, here's my code...
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <string>
#include <cstdilib>
#include <iomanip>
using namespace std;

//Function Prototypes
void displayHeading();
int drawCard();
int movePlayer(int, int);
void displayMove(int, int, int, int, int);

int main ()
{
//Variables
int playerTurn;
int playerOne = 1;
int playerTwo = 1;
int moveNumber = 1;
int card;
int currentPlayer;
int newLocation;

	//Draw Card function
	int drawCard ()
	{
		return rand() % 5 + 1;
	}


//Heading Function	
display Heading ();
currentPlayer = playerOne;

while (checkWin(currentPlayer) == false)
{
card = drawCard();
playerTurn = playerTurn + 1;
if (playerTurn > 3)
playerTurn = 1;	

//whos turn is it and calling functions move() and printMove
if (playerTurn == 1)
{ 
newLocation = displayMove (playerOne, card);
movePlayer(playerTurn, moveNumber, playerOne, card, newLocation);	
playerOne = newLocation;
currentPlayer = playerOne;
}
if (playerTurn == 2)
{
newLocation = displayMove (playerTwo, card);
displayMove(playerTurn, moveNumber, playerTwo, card, newLocation);
playerTwo = nextPosition;
currentPlayer = playerTwo;
moveNumber = moveNumber++;
}
}


//Candy Bonuses
int candyBonus(int position)
{
if (position == 6)
{
	position = 40;
	cout << "Rainbow Trail" << endl;
}

if (position == 9)
{
	position = 24;
	cout << "Lord Licorice" << endl;
}

if (position == 32)
{
	position = 43;
	cout << "Gumdrop Pass" << endl;
}

if (position == 40)
{
	position = 50;
	cout << "Hot Chocolate Gap" << endl;
}


if (position == 46)
{
	position = 58;
	cout << "Mr. Mint" << endl;
}


if (position == 62)
{
	position = 73
	cout << "Queen Frostine" << endl;
}

return position; 
} 


//Display heading for table
void displayHeading ()
{
	cout << "Candyland Game" << endl; 
	cout << "Name" << endl;
	cout << "Class Time" << endl;
	cout << " " << endl;
	cout << "Move #" << setw(10) << "Current Position" << setw(8) << "Card" << setw(8) "New Position" << setw (10) << "Candy Bonus" << endl;
return;
}


//Move the player
int movePlayer (int currentPlayer, int card)
{
int newLocation = 0;
if (newLocation < 85)
{
	newLocation = currentPlayer + card;
	newLocation = candyBonus(newLocation);
}
if (newLocation == 85)
{
	newLocation = 85;
	newLocation = candyBonus(newLocation);
}
if (newLocation > 85)
{
	newLocation = currentPlayer; 
	newLocation = candyBonus(newLocation);
}
return newLocation;
}	


//Display the move
void displayMove (int playerTurn, int moveNumber, int playerOne, int card, int 
newLocation)
{


cout << "Player " << playerTurn << " " << moveNumber << " "; 
cout << playerOne << " " << card << " " << newLocation << endl;

if (playerTurn == 3)
{cout << 
"-----------------------------------------------------------------" << 
endl;
}

if (newLocation >= 85)
	cout <<"Player " << playerTurn << " wins" << endl;

return;
}

return 0;
}

Last edited on Nov 7, 2010 at 9:24pm
Nov 7, 2010 at 11:45pm
I'm getting an issue using #include <cstdilib>, Visual Studio won't even show me the errors, it tells me [quote][/quotepgm6.cpp(3) : fatal error C1083: Cannot open include file: 'cstdilib': No such file or directory]. Only when I take #include <cstdilib> out of the program does Visual Studio even show me the errors.
Nov 8, 2010 at 12:20am
You're spelling it wrong. it's <cstdlib>, not <cstdilib>
Nov 8, 2010 at 1:03am
Wow thanks. I just copy and pasted off the program document the instructor gave us so I assumed it was correct.

Any other suggestions are welcomed, I'm going to need them.
Nov 8, 2010 at 3:01am
closed account (S6k9GNh0)
I would highly suggest that you use a switch statement instead of an incredibly long list of if statements (such as in candyBonus()). This will help organize your code and possibly be a quick and easy optimization if I'm not mistaken.
Nov 8, 2010 at 3:12am
Care to explain a little on switch statements? We haven't learned those yet and maybe I can get a few extra points if I try to use one, I've already come to terms that I probably won't have this program working by the time it's due.
Topic archived. No new replies allowed.