Coin Toss Game: Improvements / Feedback?!

Hi guys,

This is the first program i have written myself, so be gentle :)

Its just a basic coin toss game, where the user guesses each toss, and they get the option to cash in or continue when they reach a certain streak.

I am basically looking for a little feedback, on how i could improve the code.

It would also be great if someone could explain to me how i could get every user input to replace the text currently displayed with the updated outcomes (coin tosses etc), as opposed to just streaming relentlessly down the page.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int main()
{
	int guess;
	int playsLeft = 10;
	int streak = 0;
	char playAgain = 'y';
	char cashIn = 'n';
	
		cout << "			Welcome to Packerstars Coin Toss!!" <<endl;
	
		cout <<"\n\nRules:\nGuess 10 coin tosses in a row to win the 1,000,000 pound Jackpot.";
		cout <<"\nGuess 7 in a row, and cash in to win 100 pounds.";
		cout <<"\nGuess 5 in a row, and cash in to win 20 pounds.";
		cout <<"\nGuess 3 in a row, and cash in for 1 pound.";
		
	while (playAgain =='y')
	{

		do
		{
		
		
			srand(static_cast<unsigned int>(time(0)));
			int coinFace = rand() % 2+1;
		
			cout << "\n\nHeads or Tails? (1 or 2)" <<endl;
			cin >> guess;
			--playsLeft;

				
	
					if(guess == coinFace & coinFace == 1)
					{
					++streak;
					cout << "\nThe coin landed Heads up!";
					cout << "\n\nYou guessed correct! "<< playsLeft << " plays left, with a streak of " << streak << "!";
					

					}
					else if(guess == coinFace & coinFace == 2)
					{
					++streak;
					cout << "\nThe coin landed Tails up! ";
					cout << "\n\nYou guessed correct! "<< playsLeft << " plays left, with a streak of " << streak << "!";
					}
					else if(guess != coinFace & coinFace == 1)
					{
					streak = 0;
					cout << "\nThe coin landed Heads up! ";
					cout << "\n\nUnlucky, you were wrong! " << playsLeft << " plays left!";
					}
					else if(guess != coinFace & coinFace == 2)
					{
					streak = 0;
					cout << "\nThe coin landed Tails up! ";
					cout << "\n\nUnlucky, you were wrong! "<< playsLeft<< " plays left!";
					}

			if(streak == 3)
			{
				cout << "\n\nYou have reached a streak of three!! Cash in?(y/n)";
				cin >>cashIn;
			}
			else if(streak == 5)		
			{
				cout << "\n\nYou have reached a streak of five!! Cash in?(y/n)";
				cin >>cashIn;
			}	
			else if(streak == 7)		
			{
				cout << "\n\nYou have reached a streak of seven!! Cash in?(y/n)";
				cin >>cashIn;
			}
			else if(streak == 10)		
			{
				cashIn = 'y';
			}

		if(playsLeft == 0)
		{
			cashIn = 'y';
		}

		}while (cashIn != 'y');

	
		if(streak==10)
			{
				cout << "\n\nCONGRATULATIONS!!";	
				cout << "\nYou win the Jackpot of 1,000,000 pounds with a streak of ten, YOU JAMMY BASTARD!"<<endl;
			}
		else if(streak == 3 && cashIn == 'y')		
			{
				cout << "\n\nCONGRATULATIONS!!";
				cout << "\nYou have cashed in for 1 pound with a streak of 3!"<<endl;	
			}	
		else if(streak == 5 && cashIn == 'y')		
			{
				cout << "\n\nCONGRATULATIONS!!";
				cout << "\nYou have cashed in for 20 pounds with a streak of 5!"<<endl;	
			}
		else if(streak == 7 && cashIn == 'y')		
			{
				cout << "\n\nCONGRATULATIONS!!";
				cout << "\nYou have cashed in for 100 pounds with a streak of 7!"<<endl;	
			}	
		else if(streak != 10 && cashIn == 'y')		
			{
				cout << "\n\nYou have run out of plays! Better luck next time, loser!"<<endl;	
			}
	
		
		
	cout << "\n\nDo you wish to play again?(y/n)" ;
	cin >> playAgain;
	
	playsLeft = 10;
	cashIn = 'n';
	streak = 0;
	}
	
	
	cout << "\n\nOk, cya!!"<<endl;

		return 0;
}
Cool game. Ive done some programs on PC, and doing a text that doesnt go down and changes with inputs, at least at Turbo C++ where Ive learned C programming, was clearing the screen with the command CLRSC(); (which clears all outputs and put the next output to the first line) every end of the cycle of the "do" and rewriting at the beginning of it.And if you want the rules to always be shown, you should put the rules ouput inside the "do" .Since Im a beginner I'm not sure if the command is the same,in Borland Turbo C++ it was clrsc(); and in DEV C++ it was system('cls');.
Hope it helps.
The easy way is system("cls") but, for many a reason (just google it), system calls are frowned upon.

You can write your own cls function: http://support.microsoft.com/kb/99261

EDIT: Upon reading that Microsoft article, there's a couple of things you'll need to do that it doesn't mention. If you elect the option of writing your own cls function and are having trouble, let me know and I'll help out.
Last edited on
Thanks guys!
I just put a system("cls") in line 35 and it has the desired effect -ish!!

@iHutch
Yeah i see a lot of people flaming the use of system calls, i think i need some more experience before i can fully understand why?
I had a look at that article, and i would be very interesting in trying to implement / create a cls function of my own, but i really don't understand it at all!

I'm three chapters into the book 'Beginning C++ through game programming' and i haven't come across any of the code shown in that article!
Help would be greatly appreciated though!

Thanks again! :)
Yeah i see a lot of people flaming the use of system calls, i think i need some more experience before i can fully understand why?

Mainly a security issue. If you ever feel curious, Google it. There'll be no end of articles about it.


I had a look at that article, and i would be very interesting in trying to implement / create a cls function of my own, but i really don't understand it at all!

That's probably because the code there is slightly abstract, even in C++ terms. It's proprietary Windows code so there's little in the way of regular C++ data types. You're likely to understand this more when you cover classes.


I'm three chapters into the book 'Beginning C++ through game programming' and i haven't come across any of the code shown in that article!

Nor are you likely to. The closest you get is classes and function calls. The rest, as mentioned in the statement above this, is purely Windows-based.


Help would be greatly appreciated though!

Any time.


I am basically looking for a little feedback, on how i could improve the code.

Given where you say you are in your current studies, the next logical step, to me, would be to add functions into your code. I'm sure you'll come across these in a chapter or two. :-)
@Eyes

I added in a couple of gotoXY() functions. No need to use a clear screen function. After a Sleep(1500), or 1.5 seconds, a part of the screen is cleared, and new text is written. Hope this of help. And to echo Ankardo, yeah, cool game..

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
// Coin Toss Game.cpp : main project file.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <windows.h>

using namespace std;

void gotoXY(int x, int y);
void gotoXY(int x, int y, string text);

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

int main()
{
	int guess, coinFace, playsLeft = 10, streak = 0, x;
	char playAgain = 'y', cashIn = 'n';
	string erase(80,' '); // To delete one complete line on screen

	srand(static_cast<unsigned int>(time(0)));

	gotoXY(23, 3,"Welcome to Packerstars Coin Toss!!");

	gotoXY(10, 6,"Rules:");
	gotoXY(10, 8,"Guess 10 coin tosses in a row to win the 1,000,000 pound Jackpot.");
	gotoXY(10, 9,"Guess 7 in a row, and cash in to win 100 pounds.");
	gotoXY(10,10,"Guess 5 in a row, and cash in to win 20 pounds.");
	gotoXY(10,11,"Guess 3 in a row, and cash in for 1 pound.");

	while (playAgain =='y')
	{
		do
		{
			coinFace = rand() % 2+1;

			gotoXY(20,13,"Do you pick Heads or Tails? (1 or 2) : _\b"); // A '\b' is a backspace. Now it erases last input, and waits for next
			cin >> guess;
			--playsLeft;

			if(guess == coinFace && coinFace == 1)
			{
				++streak;
				gotoXY(28,16,"The coin landed Heads up!");
				gotoXY(13,18);
				cout << "You guessed correct! "<< playsLeft << " plays left, with a streak of " << streak << "!";
			}
			else if(guess == coinFace && coinFace == 2)
			{
				++streak;
				gotoXY(28,16,"The coin landed Tails up!");
				gotoXY(13,18);
				cout << "You guessed correct! "<< playsLeft << " plays left, with a streak of " << streak << "!";
			}
			else if(guess != coinFace && coinFace == 1)
			{
				streak = 0;
				gotoXY(28,16,"The coin landed Heads up!");
				gotoXY(21,18, "Unlucky, you were wrong!");
				if (playsLeft!=0)
				{
					cout <<  " " <<  playsLeft << " play";
					if (playsLeft>1)
						cout << "s";
					cout << " left!";
				}
				else
					cout <<  " Game OVER!!";
			}
			else if(guess != coinFace && coinFace == 2)
			{
				streak = 0;
				gotoXY(28,16,"The coin landed Tails up!");
				gotoXY(21,18, "Unlucky, you were wrong!");
				if (playsLeft!=0)
				{
					cout <<  " " <<  playsLeft << " play";
					if (playsLeft>1)
						cout << "s";
					cout << " left!";
				}
				else
					cout <<  " Game OVER!!";
			}

				if(streak == 3)
				{
					gotoXY(13,20,"You have reached a streak of three!! Cash in?(y/n) : _\b");
					cin >>cashIn;
				}
				else if(streak == 5)		
				{
					gotoXY(14,20,"You have reached a streak of five!! Cash in?(y/n) : _\b");
					cin >>cashIn;
				}	
				else if(streak == 7)		
				{
					gotoXY(13,20,"You have reached a streak of seven!! Cash in?(y/n) : _\b");
					cin >>cashIn;
				}
				else if(streak == 10)		
				{
					cashIn = 'y';
				}
				if(playsLeft == 0)
				{
					cashIn = 'y';
				}
			
			Sleep(1500);
			if (playsLeft!=0)
			{
				for(x=16;x<23;x++)
				gotoXY(0,x,erase);
			}

	}while (cashIn != 'y');

		if(streak==10)
		{
			gotoXY(31,18,"CONGRATULATIONS!!");	
			gotoXY(10,20,"You win the Jackpot of 1,000,000 pounds with a streak of ten, YOU JAMMY BASTARD!");
		}
		else if(streak == 3 && cashIn == 'y')		
		{
			gotoXY(31,18,"CONGRATULATIONS!!");
			gotoXY(10,20,"You have cashed in for 1 pound with a streak of 3!");	
		}	
		else if(streak == 5 && cashIn == 'y')		
		{
			gotoXY(31,18,"CONGRATULATIONS!!");
			gotoXY(10,20,"You have cashed in for 20 pounds with a streak of 5!");	
		}
		else if(streak == 7 && cashIn == 'y')		
		{
			gotoXY(31,18,"CONGRATULATIONS!!");
			gotoXY(10,20,"You have cashed in for 100 pounds with a streak of 7!");	
		}	
		else if(streak != 10 && cashIn == 'y')		
		{
			gotoXY(10,20,"You have run out of plays! Better luck next time, LOSER!");	
		}

		gotoXY(17,22,"Do you wish to try your luck again? (y/n) : _\b");
		cin >> playAgain;
		for(x=16;x<23;x++)
				gotoXY(0,x,erase);
		playsLeft = 10;
		cashIn = 'n';
		streak = 0;
	}

	gotoXY(37,18, "Ok, cya!!");
	gotoXY(30,24);
	return 0;
}

void gotoXY(int x, int y) 
{ 
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition); 
}

void gotoXY(int x, int y, string text) 
{ 
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition);
	cout << text;
}
Last edited on
@whitenite

Thanks very much for taking an interest and for the feedback!
I just compiled the additions / alterations you made to the code, and it looks great :)

I'll shall play around with what you've done to try and get my head around it some more, and maybe expect a few more posts from me on it lol!!

I really appreciate the help!!! Thanks again!
@Eyes

I'll be happy to answer any questions you have on the program additions, etc.

whitenite1
Topic archived. No new replies allowed.