Splitting a Piece of Code into 2 Classes

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!


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
#include <iostream>
#include <string> // Needed to use strings
#include <cstdlib> // Needed to use random numbers
#include <ctime>
#include <fstream>
using namespace 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 
Last edited on
First, fix the code tags in your post. See http://www.cplusplus.com/articles/jEywvCM9/


A class is encapsulation. It is a way to pack some data and related procedures together.

What attributes does one player have?
I think I may have gotten the solution. Thanks for the tip with the code tags.
Topic archived. No new replies allowed.