Please simplify my code.

Started by
42 comments, last by rip-off 11 years, 10 months ago
I've been interested in game programming recently. So I've started learning C++
I'm a total beginner. Please review my code and also help me to improve it.
I want a slot machine with: 1000$ to start with. It's seems to be working. Please help me to make it more simpler.



// A Slotting Machine implementation
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int Random(int low, int high);
int main()
{
int low = 2; // Random number range.
int high = 7; // Random number range.
int r0, r1, r2;
int money = 1000; // Initial money to start with.
int reply = 0;
char exit;
int bet;
char restart;
char cont;
srand( time (0) ); // Default Random Number function call.
cout << "Players chips: $1000"<< endl;
while(true)
{
cout << "1) Play slot. 2) Exit. ";
cin >> reply;
if(reply == 1)
{
while(1)
{
while(true)
{
cout << "Enter your bet: ";
cin >> bet;
if(bet > 0 && bet <= money)
break;
else cout << "Please enter a valid bet.\n";
}
r0 = Random(low, high); // Function call
cout << r0 <<" ";
r1 = Random(low, high); // Function call
cout << r1 << " ";
r2 = Random(low, high); // Function call
cout << r2 << endl;
if (r0 == r1 == r2 == 7) // Machine's number sequence.
{
money = (10 * bet);
cout << "Lucky Bucky!\n";
cout << "Players chips: $" << money << endl;
}
if((r0 == r1 == r2) && (r0 != 7 && r1 != 7 && r2 != 7)) // Machine's number sequence.
{
money = (5 * bet);
cout << "Not Bad!\n";
cout << "Players chips: $" << money << endl;
}
if(r0 == r1 || r1 == r2 || r0 == r2) // Machine's number sequence.
{
money = (3 * bet);
cout << "Go Happy!\n";
cout << "Players chips: $" << money << endl;
}
else
{ cout << "You Lose!\n";
money -= bet; // Get Previous amount back.
cout << "Your Money: " << money << endl;
if(money == 0)
{
cout << "You just got flushed!\n";
cout << "Exiting...\n";
break;
}
}
cout << "1) Play slot. 2) Exit. ";
cin >> cont;
if(cont == '1')
continue;
else cout << "Exiting\n";
break;
}
}
else if (reply == 2)
cout << "Exiting\n";
break;
}
}
int Random(int low, int high)
{
int r0 = low + rand() % ((high + 1) - low);
return r0;
int r1 = low + rand() % ((high + 1) - low);
return r1;
int r2 = low + rand() % ((high + 1) - low);
return r2;
}
Advertisement
EDIT: OK, fine, no one liked this comment. *grump* tongue.png I obviously must've stated this the wrong way.
Why do you return three times in Random()? the r1 and r2 lines dont do anything, return r0 ends the function. Also, shouldnt all these lines "money = (3 * bet);" be "money += 3*bet;" ?
@ zacaj. Thanks for your reply. I'm calling it 3 times to generate 3 different random nos. Is it possible to generate 3 different nos. in 1 call? If so, please let me know.
If all 3 nos. are the same, i get 10times the money. if 2 nos. are same, i get 3 times the money. If all 3 nos. are different, I lose the money i put in. And yeah, it should be money +=.. Thanks a lot.

int Random(int low, int high)
{
int r0 = low + rand() % ((high + 1) - low);
return r0; //the function quits here, all code below this never runs
int r1 = low + rand() % ((high + 1) - low);
return r1;
int r2 = low + rand() % ((high + 1) - low);
return r2;
}
Just to help you with desplaying your code on this site there is a red code button {....} that will maintain all of your code indentation when you use it to desplay your code.it will help us read it a little better. Some bits of advice if you are trying to learn C/C++ with your game is try using some containers to organize your data such as arrays, enums, and vectors. I promise you if you try to learn these concepts more deeply at the beggining it will help you Immensely in the long run. Also you should add way more comments that reffer to what your functions and expression are doing. this is good for both yourself when reviewing your code as well as for others trying to fix or understand your code.
J-GREEN

Greenpanoply
Now my function looks like this. The code still runs. What coresponding changes should I make in the main function? Sorry for such a dumb question but I seem to be confused.


int Random(int low, int high)
{
int r0 = low + rand() % ((high + 1) - low);
return r0;

}
@ greenzone. Hey thanks for your reply. Yeah, I've just began learning. I'll get into all those topics soon. I've already learnt a bit of arrays
First I recommend you dont use r0 since there aren't multiple numbers going on, it's a bit less confusing

int Random(int low, int high)
{
int randomNumber = low + rand() % ((high + 1) - low);
return randomNumber ;
}


You don't need to make any changes to main() because of the changes in Random(). Each time you call random it's running it separate from the other times. You can call it three times, and each time it will make a new random number and return it.

OK, let me start by saying this, very clearly: This is not C++. This is C. You should learn C++ instead. What book or reference are you using, which claims that this is C++? Throw it out the window, whether literal or virtual, and get a reference of C++ instead.

C++ is an object-oriented programming language. This means that you should look at things as real-world object, created from blueprints called classes (or structs). If you have a game with a slot-machine, then that slot machine is an object, I think you'd agree. (In fact, the game itself is an object, isn't it?) So what you need to do is to write a class called SlotMachine, and create an object from that class. And pulling the lever on a slot machine should be represented as a member function (or a "method", as it's called in some languages) of the SlotMachine class. So when you want to pull the lever, you write slotMachine.pullLever().

You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.

I'd love to re-write it all in C++ for you, but... well, I think you should do it, frankly. Otherwise you'd not be learning, I think.

EDIT:

Oh, don't get me wrong, I'll help you if you need it, and I'm sure others here will aswell. Just try some on your own, with a decent C++ reference in your back, and if you run into problems, we're here.
Just... start over, and do it in C++.


I think you are confused...
He uses an object of class osteam called "cout" instead of "printf".

Ok, let me say this clearly this is not C. This is C++
You can create a new file, and call it "main.c". Then copy the code above, and compile it... What do you get? A lot of errors!!
C can not use objects, and iostream isn't a C library!

This topic is closed to new replies.

Advertisement