Please help improve my code.

End of the year project completed! I'm so happy and proud. I'm wondering if there is away to make it more efficient and organized. Also is there a way to fix the bold part of my code? I'm trying to make a border that goes around my selected text.

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//********************************************************
//Author: Charles Smith
//Class: 126 CSC
//Name: Final Project (Lab 8; Completed Game)
//Credits: (cplusplus.com)chrisname, (cplusplus.com)Duoas
//         It was their code that I used to clear the
//         screen.
//********************************************************
//Header file
#include <iostream>
#include <string>
#include <iomanip>
#include<ctime>
#include<cmath>
#include<cstdlib>
#include <windows.h>

using namespace std;

int playagain = 0;

void ClearScreen()

{
	HANDLE                     hStdOut;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	DWORD                      count;
	DWORD                      cellCount;
	COORD                      homeCoords = { 0, 0 };

	hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdOut == INVALID_HANDLE_VALUE)
		return;
	// Get the number of cells in the current buffer:
	if (!(GetConsoleScreenBufferInfo(hStdOut, &csbi)))
		return;

	cellCount = csbi.dwSize.X *csbi.dwSize.Y;

	// Fill the buffer with empty whitespace :P

	if (!(FillConsoleOutputCharacter(hStdOut,
		(TCHAR) ' ',
		cellCount,
		homeCoords,
		&count)))
		return;

	// Fill the buffer with the current colours and attributes:
	if (!(FillConsoleOutputAttribute(hStdOut,
		csbi.wAttributes,
		cellCount,
		homeCoords,
		&count)))
		return;
	// Now place the console cursor to the first position in the console:
	SetConsoleCursorPosition(hStdOut, homeCoords);

}

void math();
void psychic();
void guessing();
void treasure();
void tictactoe();

int main()


{
	int choice; //First choice given to player
	string username = "Guest"; //Defualting name to Guest

	cout << endl << endl << "***************************************************************"
		 << "*****************" << endl << endl;
	cout << "\t\t    CCCCCCCCC       ++             ++            " << endl;
	cout << "\t\t    CC              ++             ++          " << endl;
	cout << "\t\t    CC         ++++++++++++   ++++++++++++    " << endl;
	cout << "\t\t    CC              ++             ++          " << endl;
	cout << "\t\t    CCCCCCCCC       ++             ++            " << endl << endl;

	cout << "\t        GGGGG         AA            MM     MM      EEEEEEEEE" << endl;
	cout << "\t      GG     GG      AAAA          MMMM   MMMM     EE" << endl;
	cout << "\t    GGG             AA  AA        MM  MM MM  MM    EEEEEE" << endl;
	cout << "\t    GGG    GGGGG   AAAAAAAA      MM    MMM    MM   EEEEEE" << endl;
	cout << "\t      GG     GG   AA      AA    MM     MMM     MM  EE" << endl;
	cout << "\t       GGGGGG    AA        AA  MM       M       MM EEEEEEEEE" << endl << endl;
	cout << "********************************************************************************" 
		 << endl;
	cout << " Hello Player, before we begin would you like to enter your name?" << endl;
	cout << " Press 1 for Yes or 2 for No, followed by the Enter/Return key. (By defualt you" 
		 << " will be known as Guest.)" << endl << endl;
	cout << "\n\t1.  Yes.";
	cout << "\n\t2.  No." << endl << endl;
	cin >> choice;

	if (choice == 1) //When Yes is choosen...
	{
		cout << "\n\n Please enter your first name or whatever you would like to be known as," 
			 << "\n then press enter/return." << endl << endl;
		cin >> username; //Input overwrites username defined as Guest

	}

	ClearScreen(); //Clears the screen which was previously filled with input datas

	int choice2; //Players 2nd choice

	char b = '*'; //Defaulting the border character to *



	cout << endl << endl << " Loading..." << endl;
	cout << " Loading complete!" << endl << endl << endl;
	cout << " Would you like to personalize the game menu border?" << endl;
	cout << " Press 1 for Yes or 2 for No, followed by the Enter/Return key."
		 << "\n (By defualt the border will consist of the (*) char.)" << endl << endl;
	cout << "\n\t1.  Personalize the game menu border..";
	cout << "\n\t2.  Skip and start the game." << endl << endl;
	cin >> choice2;

	if (choice2 == 1)
	{
		cout << "\n Personalize the game menu border by selecting any character followed" 
			 << "\n by the Enter/Return key." << endl << endl;
		cin >> b;

	}

	ClearScreen();

	//int i = 81;
	int choice3; //3rd choice given to player



	//std::cout << std::string(i, b) << endl << b;
	do
	{
		cout << setfill(b) << setw(89);

		cout << "\n       Welcome " << username << "!" << setfill(' ');
		cout << "\n\n\t1.  Play the math game!";
		cout << "\n\t2.  Play the psychic game!";
		cout << "\n\t3.  Play the guessing game!";
		cout << "\n\t4.  Play the treasure game!";
		cout << "\n\t5.  Play the tic-tac-toe game!";
		cout << "\n\t6.  Exit.";
		cout << "\n\n       Please choose a number from the options above: ";
		cin >> choice3;
		cout << endl << "You have selected " << choice3 << ". Please wait..." 
			 << endl << endl;

		if (choice3 == 1)
		{
			math();
		}

		else if (choice3 == 2)
		{
			psychic();
		}

		else if (choice3 == 3)
		{
			guessing();
		}

		else if (choice3 == 4)
		{
			treasure();
		}

		else if (choice3 == 5)
		{
			tictactoe();
		}

		else if (choice3 == 6)
		{
			return 0;
		}

		else
		{
			choice3 = 0;
		}
	} while (choice3 != 1 || choice3 != 2 || choice3 != 3 || choice3 != 4 || choice3 != 5 || choice3 != 6);
	
	system("pause"); //stops the slow writing error
	
}

void guessing()
{
	cout << "Sorry, the game is out of order.";
}

void tictactoe()
{
	cout << "Sorry! Game not ready.";
}

void math()
{
	//Prompting user for name and assigning it to the string name
	string name;
	do
	{
		cout << " Hello, my name is Gilletta. What is your name?" 
			 << endl << endl;
		cout << " Hello Gilletta, my name is "; cin >> name; 
		cout << endl << endl << endl << endl;

		//Talking about the game nad what not

		cout << "\t Well " << name << " I'm pretty bored right now. "
			 << "\n Oh I know, lets play a game." 
			 << endl << endl << endl << endl;
		cout << "\t I'll explain the rules of the game I want to play with you."
			<< "\n\t Together, we'll pick a total of 5 five digit numbers."
			<< "\n\t I win if I correctly guess the total sum of the 5 five digit number "
			<< "\n\t only after recieving your first five digit number."
			<< "\n\t I lose if I don't. "
			<< "\n\t Yeaaah! I'm getting excited just thinking about playing this game,"
			<< "\n\t hurry and pick your first 5 digit number." << endl << endl << endl;

		//getting first 5 digit number and assigning it to their int along with a few others
		int digit1;
		int digit2;
		int digit4;

		cout << " Alright Gilletta, I'll choose the five digit number... "; 
		cin >> digit1;

		//computing and printing final sum

		cout << endl << endl << " Thats a pretty unique number; "
			 << "\n I have already figured out the total sum." << endl;
		cout << " The total sum will be..." << (digit1 - 2) + 200000;
		cout << "\n Hard to believe right? Pick your second five digit number." 
			 << endl << endl;
		cin  >> digit2;
		cout << endl << " " << digit2 << "? In that case I'll pick..." 
			 << endl << endl;

		//declaring digit3 value and whatnot
		int digit3 = (99999 - digit2);

		cout << digit3 << endl << endl << endl;
		cout << " It wont be long till we find out if I'm right or wrong."
			<< "\n Pick the fourth five digit number and I'll pick the last." 
			<< endl << endl;
		cin >> digit4;
		cout << "\n And I'll pick..." << endl << endl;

		//declaring digit5 value and whatnot
		int digit5 = (99999 - digit4);

		cout << digit5 << endl << endl << endl;

		
Last edited on
Second half of 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//end game!
		cout << " Only one way to see if I was correct, lets add "
			 << "\n all these numbers together!" << endl << endl;
		cout << digit1 << endl << "\t+" << endl << digit2 << endl 
			 << "\t+" << endl << digit3 << endl << "\t+" << endl 
			 << digit4 << endl << "\t+" << endl 
			 << digit5 << endl << endl;
		cout << " Total: " << (digit1 + digit2 + digit3 + digit4 + digit5) 
			 << endl << endl;
		cout << " Guess I win ^_^, Good bye...unless you wish to play again (1)." 
			 << endl << endl;
		cin >> playagain;
	} while (playagain == 1);
}
void treasure()
{
	do
	{
		int x = 30, y = 30;                       // Explorer’s coordinates
		int x1, y1;                                // Treasure’s coordinates
		char dir = 'a';
		float distance;
		bool treasure = false;

		srand(time(0));                      // secretly seed the rand function !
		x1 = rand() % 30 + 1;            // x1 is randomly set to a number between 1 //and 30
		y1 = rand() % 30 + 1;           // y1 is randomly set to a number between //1and 30;


		//write loop to find the treasure
		char d; //direction user is heading
		int z; //for while loop
		int count; //how many steps were taken
		z = 0;
		count = 0;

		//directions for the game
		cout << " Argh maties! There is hidden booty around here, I can smell it!"
			<< "\n I'll tell you what if you find the treasure you can keep it!"
			<< endl
			<< "\n To help you start I'll let you know that your coordinates are (30,30)."
			<< "\n You can only move North, South, West or East."
			<< endl
			<< "\n Everytime you move in a direction I'll let you know your coordinates and"
			<< "\n how close you are to the treasure."
			<< "\n When your coordinate is the same as the Treasures, you win! Now Lets Start!"
			<< endl << endl << endl;

		//loop statement starts
		while (z != 1)
		{
			cout << " Please enter (n,s,e,w) to go towards the corresponding direction or" 
				 << "\n (x) to exit: ";
			cin >> d;

			//increasing y or x depending on direction (d) input with ifs
			if (d == 'n')
			{
				y++;
			}

			else if (d == 's')
			{
				y--;
			}

			else if (d == 'e')
			{
				x++;
			}

			else if (d == 'w')
			{
				x--;
			}

			//calc distance and increase step count
			distance = sqrt(static_cast<float>((x - x1)*(x - x1) + (y - y1)*(y - y1)));
			count++;

			//giving player hints
			cout << " Your location is: (" << x << "," << y << ";" << endl << endl;
			cout << " Your distance from the treasure is " << distance << "." << endl << endl;

			if (distance > 8)
			{
				cout << " You are too far from the treasure." << endl << endl;
			}

			else if (4 < distance && 8 <= distance)
			{
				cout << " You are far from the treasure." << endl << endl;
			}

			else if (4 <= distance && distance > 0)
			{
				cout << " You are getting closer to the treasure." << endl << endl;
			}

			//found treasure, end loop
			else if (distance == 0)
			{
				cout << " Congratulations, you have reached the treasure!"
					 << "\n You have reached the treasure in: "
					<< count << " steps." << endl << endl;
				break;
			}

			cout << " Do you wish to play again (1)?" << endl << " ";
			cin >> playagain;
			cout << endl << endl;
		} //end of while loop
	} while (playagain == 1);
}

void psychic()
{
	unsigned seed;
	char life, response;
	bool again = true;

	cout << " Welcome to the Matrix.\n"
		<< " You have been automaticly connected to the Psychic Computer Network.\n"
		<< " My name is Dionne, and I will be your psychic computer guide.\n"
		<< endl << " Before we continute please enter a number: ";

	std::cin >> seed;
	srand(seed);

	while (!std::cin)
	{
		std::cin.clear();
		cout << "Please, enter only an INTEGER> " << flush;
		std::cin >> seed;
	}

	cout << "\n Thank you. Lets move on.\n";

	do
	{

		cout << " Please enter an 'R' for a prediction on your relationships.\n"
			<< " Please enter an 'E' for a prediction on school life.\n"
			<< " And finally you can enter 'D' for a prediction on your financial situation.\n"
			<< endl << " ";
		std::cin >> life;

		if (life == 'R' || life == 'r')
		{
			int	x = rand() % 5 + 1;

			switch (x)
			{
			case 1:
				cout << "\n Roses are red, voilets are not blue,"
					 << "\n smile today if you want your true love to find you.";
				break;
			case 2:
				cout << "\n Walk up to your crush, smile, extend your hand and say" 
					 << "\n hello and your day will never be the same.";
				break;
			case 3:
				cout << "\n Don't be shy, take some risk in your relationships, today is the day.";
				break;
			case 4:
				cout << "\n Go to a comedy club, watch something funny or listen to something funny, "
					 << "\n this will help strengthen your relationship.";
				break;
			case 5:
				cout << "\n Step outside your comfort zone if you want to meet new people.";
				break;
			}
		}

		else if (life == 'E' || life == 'e')
		{
			int	y = rand() % 5 + 1;
			switch (y)
			{
			case 1:
				cout << "\n You will get a B + if you study for that upcoming test.";
				break;
			case 2:
				cout << "\n School will be closed soon.";
				break;
			case 3:
				cout << "\n Your teachers will assign homework for thanksgiving.";
				break;
			case 4:
				cout << "\n Find a study buddy, they will be the reason you start to excel in class.";
				break;
			case 5:
				cout << "\n Review your previous class notes so you wont be surprise of the pop quiz.";
				break;
			}
		}

		else if (life == 'D' || life == 'd')
		{
			int	z = rand() % 5 + 1;
			switch (z)
			{
			case 1:
				cout << "\n Invest in yourself and in life if you wish to be succesful.";
				break;
			case 2:
				cout << "\n Donate a dollar, you will find a bill worth way more later.";
				break;
			case 3:
				cout << "\n You can not be cheap with everything,"
					 << "\n especially when it comes to your health and needs.";
				break;
			case 4:
				cout << "\n Put your needs over your wants if you wish to be financly regret free.";
				break;
			case 5:
				cout << "\n Learn to cook, the fast food you eat is doing a lot "
					 << "\n more than satisfying your hunger.";
				break;
			}
		}

		else
		{
			std::cin.clear();
			cout << "Please, enter only an R, E or D. " << flush;
			std::cin >> life;
		}

		cout << "\n\n Would you like another prediction? Y/N" << endl << endl << " ";
		std::cin >> response;
		cout << endl;

		if (response == 'n' || response == 'N')
		{
			cout << " You have been disconnected from the Psychic Computer Network..." << endl;
			again = false;
		}

		else again = true;
	} while (again == true);

}
Last edited on
If you really want to make it better, try using classes to organize your functions so that your main reads like a story.

EDIT - A simple story. That is, mostly method calls. I can already see that each game could be it's own class, perhaps deriving from a base game class.

Nitpicks/Slickness -
You could make use of the toupper() or to lower() std class methods to reduce life == 'E' || life == 'e' to tolower(life) == 'e'

You don't need break statements at the end of a switch, instead use a default. You can however, use the default anywhere in the switch. It's just generally used at the end.

You could change int x = 30, y = 30; to x = y = 30;


If you are looking for glaring changes, it would just be to go the OOP route and make a modular, extendable program.
Last edited on
Topic archived. No new replies allowed.