Skip to content

Instantly share code, notes, and snippets.

Created September 23, 2014 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0b494ac5edad94df61e0 to your computer and use it in GitHub Desktop.
Save anonymous/0b494ac5edad94df61e0 to your computer and use it in GitHub Desktop.
/*
Name: craps.cpp
Copyright: free
Author:
Date: 22.09.14 23:10
Description: class Craps realization
*/
#include "craps.h"
#include <iostream>
using std::cout;
using std::cin;
#include <cstdlib> // rand()
using std::rand;
// default constructor
Craps::Craps()
{
sumOfDice = 0;
myPoint = 0;
for (short i = 0; i < SIDE_OF_DICE; i++){
sideOfDice[i] = 0;
}
gameStatus = BEGIN;
totalWin = 0;
totalLose = 0;
} // end Craps
// casting dice
void Craps::rollDice()
{
sumOfDice = 0;
for (short i = 0; i < SIDE_OF_DICE; i++){
sideOfDice[i] = 1 + rand() % 6;
sumOfDice += sideOfDice[i];
}
} // end rollDice
// checking game status and "point"
void Craps::checkGameStatus()
{
switch(sumOfDice){
case 2: case 3: case 12: gameStatus = LOST; myPoint = 0;
totalLose++; break;
case 7: case 11: gameStatus = WON; myPoint = 0;
totalWin++; break;
default: gameStatus = CONTINUE;
myPoint = sumOfDice; break;
}
} // end checkGameStatus
// controling status of the game
bool Craps::controlStatus() const
{
return CONTINUE == gameStatus;
} // end controlStatus
// checking MyPoint to be equal to sumOfDice
void Craps::checkMyPoint()
{
if (myPoint == sumOfDice){
gameStatus = WON;
totalWin++;
}
else if (7 == sumOfDice){
gameStatus = LOST;
totalLose++;
}
} // end checkMyPoint
// show dice
void Craps::showDice() const
{
void (*pshow[DICE])() = {one, two, three, four, five, six};
for (short i = 0; i < SIDE_OF_DICE; i++){
cout << "Die " << i + 1 << ":\n";
(*pshow[sideOfDice[i] - 1])();
cout << '\n';
}
} // end showDice
// turning dice
void Craps::turnDice() const
{
void (*pshow[DICE])() = {one, two, three, four, five, six};
int temp;
cout << "Die 1:\n";
temp = 1 + rand() % 6;
(*pshow[temp - 1])();
cout << "\nDie 2:\n";
temp = 1 + rand() % 6;
(*pshow[temp - 1])();
} // end turnDice
// show result of cast
void Craps::showCastResult() const
{
cout << "Sum: " << sumOfDice;
if (myPoint){
cout << "\nPoint: " << myPoint << '\n';
}
else{
cout << '\n';
}
} // end showCastResult
// show results
void Craps::showResults() const
{
if (WON == gameStatus){
cout << "\nPLAYER WINS.\n";
}
else{
cout << "\nPLAYER LOSES.\n";
}
} // end showResults
// display total results
void Craps::showTotalResults() const
{
cout << "You won: " << totalWin << " times.\n"
"You lose: " << totalLose << " times.\n";
} // end showTotalResults
// the first die
void Craps::one()
{
cout << " \n o \n \n";
}
// the second die
void Craps::two()
{
static short i = 0;
if (i > 9)
i = 0;
if (i++ % 2)
cout << " o \n \n o\n";
else
cout << " o\n \n o \n";
}
// the third die
void Craps::three()
{
static short i = 0;
if (i > 9)
i = 0;
if (i++ % 2)
cout << " o \n o \n o\n";
else
cout << " o\n o \n o \n";
}
// the fourth die
void Craps::four()
{
cout << " o o\n \n o o\n";
}
// the fifth die
void Craps::five()
{
cout << " o o\n o \n o o\n";
}
// the sixth die
void Craps::six()
{
static short i = 0;
if (i > 9)
i = 0;
if (i++ % 2)
cout << " o o\n o o\n o o\n";
else
cout << " o o o\n \n o o o\n";
}
// END Craps CLASS /////////////////////////////////////////////////////////////////////////////////
// display rules
void rules()
{
cout << "ПРАВИЛА ИГРЫ \"КРЕПС\"\n\n"
" Бросте кубики первый раз. Если при первом броске\n"
"сумма чисел выпавших на кубиках равна 7 или 11 -\n"
"вы выиграли, если сумма составила 2, 3 или 12 -\n"
"вы проиграли.\n"
" В случае выпадения другой суммы, выпавшая сумма\n"
"становится вашим \"очком\". Вы продолжаете бросать\n"
"кости до тех пор, пока сумма выпавших костей не\n"
"будет равна \"очку\". В этом случае вы выиграли.\n"
"В случае выпадения суммы равной 7 -- вы проиграли.\n"
"УДАЧИ !!!\n\n<Enter>";
cin.get();
cin.clear();
cin.sync();
} // end rules
/*
Name: craps.h
Copyright: free
Author:
Date: 22.09.14 23:09
Description: class Craps defenition
*/
#ifndef CRAPS_H_
#define CRAPS_H_
#include <string>
using std::string;
enum Status{BEGIN = -1, WON, LOST, CONTINUE}; // status of the game
const int SIDE_OF_DICE = 2; // two playing sides of dice
const int DICE = 6; // number of dice
//
class Craps
{
private:
// array of the pointers to the functions which displaying dice
static void (*pshow[DICE])();
short sumOfDice; // two dice' sum
short myPoint; // "point" -- game is't won or lost
short sideOfDice[SIDE_OF_DICE]; // die's side
Status gameStatus; // it can contain CONTINUE, WON or LOST
short totalWin;
short totalLose;
// show one
static void one();
// show two
static void two();
// show three
static void three();
// show four
static void four();
// show five
static void five();
// show six
static void six();
public:
// default constructor
Craps();
// casting dice
void rollDice();
// checking game status and "point"
void checkGameStatus();
// controling status of the game
bool controlStatus() const;
// checking MyPoint to be equal to sumOfDice
void checkMyPoint();
// turning dice
void turnDice() const;
// show dice
void showDice() const;
// show cast result
void showCastResult() const ;
// display results
void showResults() const;
// display total results
void showTotalResults() const;
}; // end Craps
// rules
void rules();
#endif // CRAPS_H_
/*
Name: mainCraps.cpp
Copyright: free
Author:
Date: 22.09.14 23:10
Description: using class Craps
*/
#include "craps.h"
#include <iostream>
using std::cout;
using std::cin;
#include <string>
using std::string;
#include <cstdlib> // srand()
using std::srand;
#include <ctime> // time()
using std::time;
#include <windows.h> // Sleep()
void input(string & str); // correct input
void turn(const Craps & c); // turning dice
void sysCls(); // screen clearing
int main()
{
setlocale(0, "");
string choice; // переменная ввод
do{
cout << "1. Game\n0. Rules\nYour choice: ";
input(choice); // выбор меню
if ("0" == choice){
sysCls(); // очистка экрана
rules(); // отобразить правила игры
sysCls(); // очистка экрана
}
}while("0" == choice);
sysCls();
cout << "Cast the dice?\n1. Yes 0. No\nYour choice: ";
input(choice); // проверка корректного ввода
if ("1" == choice){
Craps craps; // создание объекта
srand(static_cast<unsigned int>(time(0))); // засеять время
do{
craps.rollDice(); // бросить кости
turn(craps);
craps.showDice(); // отобразить кости
craps.checkGameStatus(); // проверить статус игры (проиграл, выиграл, продолжить)
craps.showCastResult(); // отобразить результат броска
while (craps.controlStatus()){ // проверка игры на продолжение
cout << "Next cast <Enter>";
cin.get(); // задержка экрана
cin.clear(); // очистка от некорректного ввода
cin.sync();
sysCls(); // очистка экрана
craps.rollDice(); // сделать следующий бросок
turn(craps);
craps.showDice(); // отобразить кости
craps.showCastResult(); // отобразить результат броска
craps.checkMyPoint(); // сравнить "очко" с результатом броска
}
craps.showResults(); // отобразить окончательный результат
cout << "\nLet's play again?\n1. Yes 0. No\nYour choice: ";
input(choice); // корректный ввод
sysCls(); // очистить экран
}while (choice != "0");
craps.showTotalResults();
cout << "Thanks for the game.";
}
cout << "\nBye!\n";
cin.get();
return 0;
}
// correct input
void input(string & str)
{
do{
getline(cin, str);
if (str != "0" && str != "1"){
cout << "Make correct choice: ";
}
}while (str != "0" && str != "1");
} // end input
// turnig dice
void turn(const Craps & c)
{
for (int i = 1; i < 520; i *= 2){
c.turnDice();
Sleep(i);
sysCls();
}
} // end turn
// screen clearing
void sysCls()
{
system("cls");
} // end screen clearing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment