Hi, I have written a fully functional "game" but I need to split the code into two classes. I was thinking about having a machine class and a player class. I need the code to work the same as it does now. Any tips or help would be greatly appreciated. Thanks!
#include <iostream>
#include <string> // Needed to use strings
#include <cstdlib> // Needed to use random numbers
#include <ctime>
#include <fstream>
usingnamespace std;
void drawLine(int n, char symbol);
int rules();
int playGame();
int main()
{
playGame();
}
int playGame()
{
string playerName;
int amount; // hold player's balance amount
int bettingAmount;
int guess;
int number; // hold computer generated number
char choice;
srand(time(0)); // "Seed" the random generator
drawLine(60,'_');
cout << "\n\n\n\t\t\tCASINO GAME\n\n\n\n";
drawLine(60,'_');
cout << "\n\nEnter Your Name : ";
cin >> playerName;
cout << "\n\nEnter Deposit amount to play game : $";
cin >> amount;
do
{
rules();
cout << "\n\nYour current balance is $ " << amount << "\n";
// Get player's betting amount
do
{
cout <<playerName<<", enter money to bet : $";
cin >> bettingAmount;
if(bettingAmount > amount)
{
cout << "Your don't have enough money to bet\n";
}
}while(bettingAmount > amount);
// Get the player's guess
do
{
cout << "Guess your number to bet between 1 to 10 :";
cin >> guess;
if (guess <= 0 || guess > 10)
{
cout << "Your guess is not between 1 and 10\n";
}
}while(guess <= 0 || guess > 10);
number = rand()%10 + 1; // Will hold the randomly generated integer between 1 and 10
if(number == guess)
{
cout << "\n\nNice Job!! You won $ " << bettingAmount * 2;
amount = amount + bettingAmount * 2;
}
else
{
cout << "Sorry you did not guess correctly, You lost $ "<< bettingAmount <<"\n";
amount = amount - bettingAmount;
}
cout << "\nThe winning number was : " << number <<"\n";
cout << "\n"<<playerName<<", You have $ " << amount << "\n";
if(amount == 0)
{
cout << "You have no money to play ";
break;
}
cout << "\n\n-->Do you want to play again (y/n)? ";
cin >> choice;
}while(choice =='Y'|| choice=='y');
cout << "\n\n\n";
drawLine(70,'=');
cout << "\n\nThanks for playing game. Your balance amount is $ " << amount << "\n\n";
drawLine(70,'=');
ofstream resultsFile;
resultsFile.open("ProjectResults");
resultsFile << "Name: " << playerName << " " << "Balance: " << amount << endl;
return 0;
}
void drawLine(int n, char symbol)
{
for(int i=0; i<n; i++)
cout << symbol;
cout << "\n" ;
}
int rules()
{
ifstream dataFile;
string line;
dataFile.open("ProjectRules");
if (dataFile.fail()) //If the file doesn't open
{
cout << "Input file opening failed.\n";
return -1;
}
else
{
string line;
while (getline(dataFile, line))
{
cout << line << endl;
}
}
}
// END OF PROGRAM