Explanation of this code

Ok. I am teaching myself C++ mostly by looking at tuts online and such. I tried to make a simple command line calculator(success) and am finshing a unit converter(almost done). I started to make a bigger project like a craps game just expand on it and change the game to suit my likes and dislikes. I will not explain what i want, but i thought that a good way to make a "CRAPS" game was to look at the source code of another. I did and i had no idea what it meant. I will post the VERY, VERY ,VERY long code. I was wondering if any good citizen would put in comments explaining what everything does. NOTE: i do know switch, and while stuff. I'm up to as some describe as the steep learning code or some other bullshit where it gets really hard and every textbook i read or tut wont explain it simply in my langauge(english lol:)_) Could somebody please comment and explain each of the elements of the code. For instance i dont know what an array, most stuff bout classes, static or something like that. So, please could someone explain anything. I don't know if it'll fit, so you can send the explained code to philbelder@yahoo.com. I know it looks like im asking a pointless task, but i want to learn all this stuff and this is a way that works for me. Thank you:)
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
#include <Windows.h>
#include <Time.h>
#include <StdIO.h>

// -------------------------------------------------------------------------
#define MAX_INPUT_CHARS	127
#define ARRSIZE(arr)	(sizeof (arr) / sizeof (arr [0]))
#define CHAR_DOT	0x04
#define CHAR_SIDE	0xB3
#define CHAR_TOP	0xC4
#define CHAR_BOTTOM	0xC4
#define CHAR_RIGHT_TOP	0xBF
#define CHAR_LEFT_BOTTOM	0xC0
#define CHAR_RIGHT_BOTTOM	0xD9
#define CHAR_LEFT_TOP	0xDA
#define CHAR_NULL	'\0'
#define CHAR_BLANK	0x20
#define LIGHT_GRAY_TEXT	(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
#define LIGHT_RED_TEXT	(FOREGROUND_RED | FOREGROUND_INTENSITY)
#define LIGHT_GREEN_TEXT	(FOREGROUND_INTENSITY | FOREGROUND_GREEN)
#define LIGHT_CYAN_TEXT (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY)
#define CYAN_TEXT (FOREGROUND_BLUE | FOREGROUND_GREEN)
#define WHITE_TEXT (LIGHT_GRAY_TEXT | FOREGROUND_INTENSITY)
#define YELLOW_TEXT	(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY)

// -------------------------------------------------------------------------
static CHAR* arrYouHaveNoMoney [] = {
	"\nCASINO: Hey! Security! Why did you let that bum in?!..\n",
	"\nCASINO: What the... Get him, boys!..\n",
	"\nCASINO: Sir, are you trying to pick on our fine establishment?\n",
	"\nCASINO: I see... Get the hell out!\n",
	"\nCASINO: Funny guy...\n"
	};

// -------------------------------------------------------------------------
static CHAR str_OneRightDot [] = {
	CHAR_BLANK, CHAR_BLANK, CHAR_BLANK, CHAR_BLANK, CHAR_DOT, CHAR_NULL };

static CHAR str_OneLeftDot [] = {
	CHAR_DOT, CHAR_BLANK, CHAR_BLANK, CHAR_BLANK, CHAR_BLANK, CHAR_NULL };

static CHAR str_TwoDots [] = {
	CHAR_DOT, CHAR_BLANK, CHAR_BLANK, CHAR_BLANK, CHAR_DOT, CHAR_NULL };

static CHAR str_NoDots [] = {
	CHAR_BLANK, CHAR_BLANK, CHAR_BLANK, CHAR_BLANK, CHAR_BLANK, CHAR_NULL };

static CHAR str_CentralDot [] = {
	CHAR_BLANK, CHAR_BLANK, CHAR_DOT, CHAR_BLANK, CHAR_BLANK, CHAR_NULL };

static UCHAR str_DiceTop [] = {
	CHAR_LEFT_TOP,
	CHAR_TOP, CHAR_TOP, CHAR_TOP, CHAR_TOP, CHAR_TOP,
	CHAR_RIGHT_TOP, CHAR_NULL };

static UCHAR str_DiceBottom [] = {
	CHAR_LEFT_BOTTOM,
	CHAR_BOTTOM, CHAR_BOTTOM, CHAR_BOTTOM, CHAR_BOTTOM, CHAR_BOTTOM,
	CHAR_RIGHT_BOTTOM, CHAR_NULL };

static UCHAR str_DiceSide [] = { CHAR_SIDE, CHAR_NULL };

// -------------------------------------------------------------------------
typedef struct
	{
	CHAR* m_pstrRows [3];
	}
	DICE_TEXT_IMAGE;

// -------------------------------------------------------------------------
static DICE_TEXT_IMAGE AllSix [] = {
		{ str_NoDots, str_CentralDot, str_NoDots }, // Face with 1 dot
		{ str_OneRightDot, str_NoDots, str_OneLeftDot }, // Face with 2 dots
		{ str_OneRightDot, str_CentralDot, str_OneLeftDot }, // Face with 3 dots
		{ str_TwoDots, str_NoDots, str_TwoDots }, // Face with 4 dots
		{ str_TwoDots, str_CentralDot, str_TwoDots}, // Face with 5 dots
		{ str_TwoDots, str_TwoDots, str_TwoDots } // Face with 6 dots
	};

// -------------------------------------------------------------------------
class apiConsole
	{
	private:
		HANDLE m_hInput;
		HANDLE m_hOutput;
		CHAR m_strInputBuffer [MAX_INPUT_CHARS+1];

	public:
		// Console constructor:
		apiConsole () {
			// Retrieve the I/O handles:
			m_hInput = GetStdHandle (STD_INPUT_HANDLE);
			m_hOutput = GetStdHandle (STD_OUTPUT_HANDLE);
			}

		// Console destructor:
		~apiConsole () {
			// Apply default text color:
			SetConsoleTextAttribute (m_hOutput, LIGHT_GRAY_TEXT);
			}

		// Just dump it on console:
		void Msg (CHAR* pstrText, WORD wAttribute) {
			DWORD dwCharsStored;

			SetConsoleTextAttribute (m_hOutput, wAttribute);
			WriteConsole (m_hOutput, pstrText, strlen (pstrText), &dwCharsStored, NULL);
			}

		// Format the integer:
		void PutInt (CHAR* pstrFormatting, int iValue, WORD wAttribute) {
			CHAR strOut [256];

			sprintf (strOut, pstrFormatting, iValue);
			Msg (strOut, wAttribute);
			}

		// Returns the input text or NULL, if empty string was entered:
		CHAR* Ask4Input (CHAR* pstrPrompt) {
			DWORD dwCharsStored;

			// Prompt text:
			Msg (pstrPrompt, LIGHT_GREEN_TEXT);

			// Get the string from user:
			SetConsoleTextAttribute (m_hOutput, LIGHT_GRAY_TEXT);
			if (ReadConsole (m_hInput, m_strInputBuffer, MAX_INPUT_CHARS, &dwCharsStored, NULL)) {
				if (dwCharsStored == 2) {
					return NULL; // Only CR-LF was entered.
					}
				m_strInputBuffer [dwCharsStored-2] = CHAR_NULL;
				return m_strInputBuffer;
				}
			return NULL; // 'ReadConsole()' failed.
			}

		// Returns TRUE if input was not cancelled:
		BOOL Ask4Integer (CHAR* pstrPrompt, int& iResult) {
			CHAR* pstrInput;

			if ((pstrInput = Ask4Input (pstrPrompt)) != NULL) {
				iResult = atoi (pstrInput);
				return TRUE;
				}
			iResult = 0;
			return FALSE;
			}

		// Dump a message from array:
		void RandomMsg (CHAR* arrMessages [], int iArrSize, WORD wAttribute) {
			Msg (arrMessages [rand () % iArrSize], wAttribute);
			}

		// Returns TRUE if user answered "y" or "Y":
		BOOL Verify (CHAR* pstrPrompt) {
			CHAR* pstrInput;

			while (1) {
				if ((pstrInput = Ask4Input (pstrPrompt)) != NULL) {
					strupr (pstrInput);
					if (strcmp (pstrInput, "Y") == 0) {
						break;
						}
					if (strcmp (pstrInput, "N") == 0) {
						return FALSE;
						}
					}
				}
			return TRUE;
			}
	};

// -------------------------------------------------------------------------
typedef struct
	{
	int m_arrGamesLost [20];
	int m_arrGamesWon [20];
	int m_iGamesLostAfter20th_throw;
	int m_iGamesWonAfter20th_throw;
	int m_iGamesPlayed;
	int m_iGamesLost;
	int m_iGamesWon;
	int m_iMoneyWon;
	int m_iMoneyLost;
	}
	STATISTICS;

// -------------------------------------------------------------------------
class crapsGame
	{
	private:
		apiConsole m_Console;
		STATISTICS m_Stats;
		int m_iBalance;
		int m_iBet;

	public:
		// Application constructor:
		crapsGame () {
			// Set up random generator:
			srand (time (NULL));

			// Clean the statistics:
			memset (&m_Stats, 0, sizeof (m_Stats));
			}

		// Set up the bet:
		void Ask4Bet () {
			int iBetValue;

			while (1) {
				m_Console.PutInt ("\nCASINO: Your balance is: %d dollars.", m_iBalance, LIGHT_GREEN_TEXT);
				if (m_Console.Ask4Integer ("\nCASINO: What is your bet amount for the game: ", iBetValue)) {
					if ((iBetValue > 0) && (iBetValue <= m_iBalance)) {
						m_iBet = iBetValue;
						break;
						}
					else {
						if (iBetValue > m_iBalance) {
							m_Console.Msg ("\nCASINO: This bet will not be covered by your balance.\n", LIGHT_RED_TEXT);
							}
						else {
							m_Console.Msg ("\nCASINO: Please, enter the corrected amount.\n", LIGHT_RED_TEXT);
							}
						}
					}
				}
			}


	
Not enough room :)

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
// Roll once:
		void Roll (int& iDice1, int& iDice2)
			{
			// Ask to roll it:
			CHAR* pstrRoll;
			DWORD dwTickCount = GetTickCount ();

			while (1) {
				if ((pstrRoll = m_Console.Ask4Input ("\nCASINO: Enter 'r' to roll dice: ")) != NULL) {
					if (strcmp (pstrRoll, "r") == 0) {
						break;
						}
					}
				}

			// Skip some random values, depending on human waiting
			// time, so the rolls will not be repetitive:
			DWORD dwTicks = (GetTickCount () - dwTickCount) % 127;

			while (dwTicks--) {
				rand ();
				}

			// Do it already!..
			iDice1 = 1 + (rand () % 6);
			iDice2 = 1 + (rand () % 6);

			// Display the dice:
			m_Console.Msg ("\n", CYAN_TEXT);
			m_Console.Msg ((CHAR*) str_DiceTop, CYAN_TEXT);
			m_Console.Msg ((CHAR*) str_DiceTop, CYAN_TEXT);

			DICE_TEXT_IMAGE* pImage1 = &(AllSix [iDice1-1]);
			DICE_TEXT_IMAGE* pImage2 = &(AllSix [iDice2-1]);

			for (int iRow=0; iRow<3; iRow++) {
				m_Console.Msg ("\n", CYAN_TEXT);

				m_Console.Msg ((CHAR*) str_DiceSide, CYAN_TEXT);
				m_Console.Msg (pImage1->m_pstrRows [iRow], LIGHT_CYAN_TEXT);
				m_Console.Msg ((CHAR*) str_DiceSide, CYAN_TEXT);

				m_Console.Msg ((CHAR*) str_DiceSide, CYAN_TEXT);
				m_Console.Msg (pImage2->m_pstrRows [iRow], LIGHT_CYAN_TEXT);
				m_Console.Msg ((CHAR*) str_DiceSide, CYAN_TEXT);
				}

			m_Console.Msg ("\n", CYAN_TEXT);
			m_Console.Msg ((CHAR*) str_DiceBottom, CYAN_TEXT);
			m_Console.Msg ((CHAR*) str_DiceBottom, CYAN_TEXT);
			}

		// A single game from the series of games:
		void PlayOneTime () {
			int iDice1, iDice2;
			BOOL bHouseWins = TRUE; // He-he! Good one!

			Ask4Bet ();
			Roll (iDice1, iDice2);
			switch (iDice1+iDice2) {
				case 7:
				case 11:
					// First throw wins!
					(m_Stats.m_arrGamesWon [0])++;
					bHouseWins = FALSE;
					m_Stats.m_iGamesWon++;
					break;
				case 2:
				case 3:
				case 12:
					// First throw loses!
					(m_Stats.m_arrGamesLost [0])++;
					m_Stats.m_iGamesLost++;
					break;
				default:
					{
					int iSum;
					int iThrows = 1;
					int iYourPoint = iDice1 + iDice2;

					// Declare a POINT right away:
					m_Console.PutInt ("\nCASINO: Your point is: %d", iYourPoint, LIGHT_GREEN_TEXT);

					// Try to make a point before 7:
					while (1) {
						Roll (iDice1, iDice2);
						m_Console.PutInt ("\nCASINO: Your point was: %d", iYourPoint, LIGHT_GREEN_TEXT);
						m_Console.PutInt ("\nCASINO: You rolled now: %d", iDice1 + iDice2, LIGHT_GREEN_TEXT);
						iThrows++;
						if ((iSum = iDice1 + iDice2) == 7) {
							// Loss!
							if (iThrows > 20) {
								m_Stats.m_iGamesLostAfter20th_throw++;
								}
							else {
								(m_Stats.m_arrGamesLost [iThrows-1])++;
								}
							m_Stats.m_iGamesLost++;
							break;
							}

						if (iSum == iYourPoint) {
							// Win!
							bHouseWins = FALSE;
							if (iThrows > 20) {
								m_Stats.m_iGamesWonAfter20th_throw++;
								}
							else {
								(m_Stats.m_arrGamesWon [iThrows-1])++;
								}
							m_Stats.m_iGamesWon++;
							break;
							}
						}
					}
				}
			m_Console.Msg (
				bHouseWins ? "\nCASINO: House wins!" : "\nCASINO: You win.",
				WHITE_TEXT);

			// Adjust the balance:
			if (bHouseWins) {
				m_iBalance -= m_iBet;
				m_Stats.m_iMoneyLost += m_iBet;
				}
			else {
				m_iBalance += m_iBet;
				m_Stats.m_iMoneyWon += m_iBet;
				}
			m_Console.PutInt ("\nCASINO: Your balance is: %d dollars.", m_iBalance, LIGHT_GREEN_TEXT);
			}

		// Statistics per throws:
		void StatsOnThrows (CHAR* pstrTitle, int* arrGameResults, int iGamesOver20Throws) {
			int iOut = 0;

			m_Console.Msg (pstrTitle, LIGHT_GRAY_TEXT);
			for (int iResult=0; iResult<20; iResult++) {
				if (arrGameResults [iResult] > 0) {
					CHAR strFormatted [128];

					sprintf (strFormatted, "\n-- On the throw #%d - %d games", iResult+1, arrGameResults [iResult]);
					m_Console.Msg (strFormatted, YELLOW_TEXT);
					iOut++;
					}
				}

			if (iGamesOver20Throws > 0) {
				m_Console.PutInt ("\n-- On more than 20 throws: %d games", iGamesOver20Throws, YELLOW_TEXT);
				iOut++;
				}

			if (iOut == 0) {
				m_Console.Msg ("\nNone.", YELLOW_TEXT);
				}
			}

		// Lights, camera, action!..
		void Run () {
			// Ask for balance:
			if (m_Console.Ask4Integer (
					"CASINO: How much money are you willing to use in this session? -> ",
					m_iBalance)) {
				if (m_iBalance <= 0) {
					// No money, eh?!
					m_Console.RandomMsg (arrYouHaveNoMoney, ARRSIZE (arrYouHaveNoMoney), LIGHT_GREEN_TEXT);
					}
				else {
					// Start playing:
					while (1) {
						PlayOneTime ();
						m_Stats.m_iGamesPlayed++;
						if (m_iBalance == 0) {
							m_Console.Msg ("\nCASINO: Sir, maybe next time you will have more luck...", LIGHT_GREEN_TEXT);
							break;
							}

						if (! m_Console.Verify ("\n\nCASINO: Do you want another game? Y/N -> ")) {
							break;
							}
						}

					// Dump statistics:
					m_Console.Msg ("\n\nSTATISTICS:", LIGHT_GRAY_TEXT);
					m_Console.PutInt ("\n-- Games played: %d", m_Stats.m_iGamesPlayed, YELLOW_TEXT);
					m_Console.PutInt ("\n-- Games won: %d", m_Stats.m_iGamesWon, YELLOW_TEXT);
					m_Console.PutInt ("\n-- Games lost: %d", m_Stats.m_iGamesLost, YELLOW_TEXT);
					m_Console.PutInt ("\n\n-- Money won: %d dollars", m_Stats.m_iMoneyWon, YELLOW_TEXT);
					m_Console.PutInt ("\n-- Money lost: %d dollars", m_Stats.m_iMoneyLost, YELLOW_TEXT);
					m_Console.PutInt ("\n-- Money left: %d dollars", m_iBalance, YELLOW_TEXT);

					StatsOnThrows ("\n\nLOSING GAMES DETAILS:", m_Stats.m_arrGamesLost, m_Stats.m_iGamesLostAfter20th_throw);
					StatsOnThrows ("\n\nWINNING GAMES DETAILS:", m_Stats.m_arrGamesWon, m_Stats.m_iGamesWonAfter20th_throw);

					m_Console.Msg ("\n", LIGHT_GRAY_TEXT);
					}
				}
			}
	};

// -------------------------------------------------------------------------
int main ()
	{
	crapsGame app;
	app.Run ();
	return 0;
	}
Topic archived. No new replies allowed.