Skip to content

Instantly share code, notes, and snippets.

Created September 19, 2014 21:25
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/539d7db1a3db17f05b0d to your computer and use it in GitHub Desktop.
Save anonymous/539d7db1a3db17f05b0d to your computer and use it in GitHub Desktop.
Craps game
/*
Name: craps.cpp
Copyright: free
Author: Alf
Date: 18.09.14 17:31
Description: class Craps realization
*/
#include "craps.h"
#include <iostream>
using std::cout;
using std::cin;
#include <cstdlib> // rand()
using std::rand;
// constructor
Craps::Craps()
{
sumOfDice = 0;
myPoint = 0;
for (short i = 0; i < SIDE_OF_DICE; i++){
sideOfDice[i] = 0;
}
gameStatus = BEGIN;
} // end Craps
// set sum of dice
void Craps::setSumOfDice(short sum)
{
sumOfDice = sum;
} // end setSumOfDice
// get sum of dice
short Craps::getSumOfDice() const
{
return sumOfDice;
} // getSumOfDice
// set my "point"
void Craps::setMyPoint(short sum){
myPoint = sum;
} // end setMyPoint
// get my "point"
short Craps::getMyPoint() const
{
return myPoint;
} // end getMyPoint
// checking MyPoint to be equal to sumOfDice
void Craps::checkMyPoint()
{
if (getMyPoint() == getSumOfDice()){
setGameStatus(WON);
}
else if (7 == getSumOfDice()){
setGameStatus(LOST);
}
} // end checkMyPoint
// casting die
short Craps::rollDie()
{
int result;
return result = 1 + rand() % 6;
} // end rollDie
// casting dice
void Craps::rollDice()
{
resetSumOfDice();
short sum = 0;
for (short i = 0; i < SIDE_OF_DICE; i++){
sideOfDice[i] = rollDie();
sum += sideOfDice[i];
}
setSumOfDice(sum);
} // end rollDice
// checking game status and "point"
void Craps::checkGameStatus()
{
switch(getSumOfDice()){
case 2: case 3: case 12: setGameStatus(LOST);
setMyPoint(0); break;
case 7: case 11: setGameStatus(WON);
setMyPoint(0); break;
default: setGameStatus(CONTINUE);
setMyPoint(getSumOfDice());
break;
}
} // end checkGameStatus
// set status of the game
void Craps::setGameStatus(const Status stat){
gameStatus = stat;
} // end setGameStatus
// get status of the game
Status Craps::getGameStatus() const
{
return gameStatus;
} // end getGameStatus
// controling status of the game
bool Craps::controlStatus() const
{
return CONTINUE == getGameStatus();
} // end controlStatus
// show dice
void Craps::showDice(void (*pshow[])()) const
{
for (short i = 0; i < SIDE_OF_DICE; i++){
cout << "Die " << i + 1 << ":\n";
(*pshow[sideOfDice[i] - 1])();
cout << '\n';
}
} // end showDice
// show result of cast
void Craps::showCastResult() const
{
cout << "Sum: " << getSumOfDice();
if (getMyPoint()){
cout << "\nPoint: " << getMyPoint() << '\n';
}
else{
cout << '\n';
}
} // end showCastResult
// show results
void Craps::showResults() const
{
if (WON == getGameStatus()){
cout << "\nPLAYER WINS.\n";
}
else{
cout << "\nPLAYER LOSES.\n";
}
} // end showResults
// END Craps CLASS /////////////////////////////////////////////////////////////////////////////////
void rules()
{
system("cls");
cout << "ПРАВИЛА ИГРЫ \"КРЕПС\"\n\n"
" Бросте кубики первый раз. Если при первом броске\n"
"сумма чисел выпавших на кубиках равна 7 или 11 -\n"
"вы выиграли, если сумма составила 2, 3 или 12 -\n"
"вы проиграли.\n"
" В случае выпадения другой суммы, выпавшая сумма\n"
"становится вашим \"очком\". Вы продолжаете бросать\n"
"кости до тех пор, пока сумма выпавших костей не\n"
"будет равна \"очку\". В этом случае вы выиграли.\n"
"В случае выпадения суммы равной 7 -- вы проиграли.\n"
"УДАЧИ !!!\n\n<Enter>";
cin.get();
system("cls");
} // end rules
void input(string * str)
{
do{
getline(cin, *str);
if (*str != "0" && *str != "1"){
cout << "Make correct choice: ";
}
}while (*str != "0" && *str != "1");
} // end input
// the first die
void one()
{
cout << " \n * \n \n";
}
// the second die
void two()
{
cout << " * \n \n *\n";
}
// the third die
void three()
{
cout << " * \n * \n *\n";
}
// the fourth die
void four()
{
cout << " * *\n \n * *\n";
}
// the fifth die
void five()
{
cout << " * *\n * \n * *\n";
}
// the sixth die
void six()
{
cout << " * *\n * *\n * *\n";
}
/*
Name: craps.h
Copyright: free
Author: Alf
Date: 18.09.14 17:18
Description: class Craps defenition
*/
#ifndef CRAPS_H_
#define CRAPS_H_
#include <string>
using std::string;
enum Status{BEGIN = -1, WON, LOST, CONTINUE, SIDE_OF_DICE = 2};
class Craps
{
private:
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
void resetSumOfDice() { sumOfDice = 0; }
public:
// constructor
Craps();
// set sum of dice
void setSumOfDice(short sum);
// get sum of dice
short getSumOfDice() const;
// set my "point"
void setMyPoint(short sum);
// get my "point"
short getMyPoint() const;
// checking MyPoint to be equal to sumOfDice
void checkMyPoint();
// casting die
short rollDie();
// casting dice
void rollDice();
// checking game status and "point"
void checkGameStatus();
// set status of the game
void setGameStatus(const Status stat);
// get status of the game
Status getGameStatus() const;
// controling status of the game
bool controlStatus() const;
// show dice
void showDice(void (*pshow[])()) const;
// show cast result
void showCastResult() const ;
// display results
void showResults() const;
}; // end Craps
// rules
void rules();
// correct input
void input(string * str);
// show one
void one();
// show two
void two();
// show three
void three();
// show four
void four();
// show five
void five();
// show six
void six();
#endif // CRAPS_H_
/*
Name: mainCraps.cpp
Copyright: free
Author: Alf
Date: 18.09.14 18:11
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;
int main()
{
setlocale(0, "");
string choice; // переменная ввод
do{
cout << "1. Game\n0. Rules\nYour choice: ";
input(&choice);
if ("0" == choice){
rules();
}
}while("0" == choice);
system("cls");
cout << "Cast the dice?\n1. Yes 0. No\nYour choice: ";
input(&choice); // проверка корректного ввода
if ("1" == choice){
// массив указателей на функции отображающие кости
void (*pshow[6])() = {one, two, three, four, five, six};
Craps craps; // создание объекта
srand(static_cast<unsigned int>(time(0))); // засеять время
do{
craps.rollDice(); // бросить кости
craps.showDice(pshow); // отобразить кости
craps.checkGameStatus(); // проверить статус игры (проиграл, выиграл, продолжить)
craps.showCastResult(); // отобразить результат броска
while (craps.controlStatus()){ // проверка игры на продолжение
cout << "Next cast <Enter>:\n";
cin.get();
system("cls");
craps.rollDice(); // сделать следующий бросок
craps.showDice(pshow); // отобразить кости
craps.showCastResult(); // отобразить результат броска
craps.checkMyPoint(); // сравнить "очко" с результатом броска
}
craps.showResults(); // отобразить окончательный результат
cout << "\nLet's play again?\n1. Yes 0. No\nYour choice: ";
input(&choice); // корректный ввод
system("cls");
}while (choice != "0");
cout << "Thanks for the game.";
}
cout << "\nBye!\n";
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment