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);
}
}
}
}
}
|