Jump to content

  • Log In with Google      Sign In   
  • Create Account


Please simplify my code.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
43 replies to this topic

#1 Sid_TheBeginner   Members   -  Reputation: 157

Like
0Likes
Like

Posted 23 June 2012 - 08:42 PM

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;
}

Edited by Sid_TheBeginner, 24 June 2012 - 01:33 AM.


Sponsor:

#2 antiHUMANDesigns   Members   -  Reputation: 58

Like
-6Likes
Like

Posted 23 June 2012 - 08:58 PM

EDIT: OK, fine, no one liked this comment. *grump* Posted Image I obviously must've stated this the wrong way.

Edited by antiHUMANDesigns, 24 June 2012 - 09:48 AM.


#3 zacaj   Members   -  Reputation: 643

Like
1Likes
Like

Posted 23 June 2012 - 09:04 PM

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;" ?

#4 Sid_TheBeginner   Members   -  Reputation: 157

Like
0Likes
Like

Posted 23 June 2012 - 09:14 PM

@ 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.

#5 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 23 June 2012 - 09:16 PM


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;

}



#6 greenzone   Members   -  Reputation: 672

Like
1Likes
Like

Posted 23 June 2012 - 09:21 PM

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.

Edited by greenzone, 23 June 2012 - 09:22 PM.

J-GREEN

Greenpanoply

#7 Sid_TheBeginner   Members   -  Reputation: 157

Like
0Likes
Like

Posted 23 June 2012 - 09:22 PM

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;

}


#8 Sid_TheBeginner   Members   -  Reputation: 157

Like
0Likes
Like

Posted 23 June 2012 - 09:24 PM

@ 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

#9 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 23 June 2012 - 09:26 PM

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.

#10 riverreal   Members   -  Reputation: 616

Like
0Likes
Like

Posted 23 June 2012 - 09:27 PM

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!

#11 Sid_TheBeginner   Members   -  Reputation: 157

Like
0Likes
Like

Posted 23 June 2012 - 09:29 PM

Hey Thank You It works perfectly! Posted Image

#12 zacaj   Members   -  Reputation: 643

Like
0Likes
Like

Posted 23 June 2012 - 09:29 PM


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!

Yes I think for something like this objects are just going to seem needlessly complicated really. Any time you're only making one instance of an object you're really just using it for categorization

#13 Sid_TheBeginner   Members   -  Reputation: 157

Like
0Likes
Like

Posted 23 June 2012 - 09:33 PM

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++.


Sorry I've no clue what your talking about. Seems its advanced stuff.

#14 zacaj   Members   -  Reputation: 643

Like
2Likes
Like

Posted 23 June 2012 - 09:37 PM


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++.


Sorry I've no clue what your talking about. Seems its advanced stuff.


He thinks you should be structuring your program the same way you would structure a much more advanced program, so that you get into the proper habits. Probably something you should do soon, but right now it looks like you're still getting a grasp of basic programming to me.

@antiHUMANDesigns

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

This isn't the C way either. This is the way you write stuff that fits in a single function, on not much more than a single page of code

#15 antiHUMANDesigns   Members   -  Reputation: 58

Like
-2Likes
Like

Posted 23 June 2012 - 09:59 PM

OK, got downvoted because I'm trying to help? I dunno what's up with people, but OK, he wanted the code fixed for him, so here's a quick (!) example of how it could look, in C++.


EDIT:

OK, so it can only show 26 lines of code? That seems weird.

Can't get the code tag working, so I jus attached a txt file of it instead.

Attached File  slotmachine example.txt   1.99KB   78 downloads

Edited by antiHUMANDesigns, 23 June 2012 - 10:06 PM.


#16 riverreal   Members   -  Reputation: 616

Like
2Likes
Like

Posted 23 June 2012 - 10:11 PM

OK, got downvoted because I'm trying to help? I dunno what's up with people, but OK, he wanted the code fixed for him, so here's a quick (!) example of how it could look, in C++.


EDIT:

OK, so it can only show 26 lines of code? That seems weird.

Can't get the code tag working, so I jus attached a txt file of it instead.

Attached File  slotmachine example.txt   1.99KB   78 downloads


The people downvoted you because the code above is written in C++...
He wrote a C++ code but without implementing the object oriented programming paradigm style. But is C++
The OOP is only a paradigm not the complete language

#17 antiHUMANDesigns   Members   -  Reputation: 58

Like
0Likes
Like

Posted 23 June 2012 - 10:12 PM


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++.


Sorry I've no clue what your talking about. Seems its advanced stuff.


I don't know about "advanced", it's what C++ was created for. It's pretty much the whole point of it, as an improvement upon C. If you want to learn C++, you need to start right away. If you want to use C instead, then that's a choice you need to make, but you need to be aware of the difference. I don't know if all these other guys thinks I'm just messing with you or something, but I think that once you look at the code example I attached to my other post, you'll understand that C++ is slightly different from C. Hopefully you other guys would care to listen to me and see the truth in what I'm saying instead of jsut downvoting me as if I was trolling. I, for one, am trying to teach a newbie the proper way to code, and good practices. I don't know what the rest of you are trying to do.

#18 antiHUMANDesigns   Members   -  Reputation: 58

Like
-2Likes
Like

Posted 23 June 2012 - 10:17 PM


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!


You're going to start being snide? Can we not do that, please?

Ask mr Stroustroup (spelling?) if this is how C++ was designed to look. Yes, he may use one of C++'s objects, but if he still codes his programs as if it was C, then he gains no benefits of C++'s improvements. That is not something I should have to argue about.

#19 antiHUMANDesigns   Members   -  Reputation: 58

Like
-3Likes
Like

Posted 23 June 2012 - 10:20 PM


OK, got downvoted because I'm trying to help? I dunno what's up with people, but OK, he wanted the code fixed for him, so here's a quick (!) example of how it could look, in C++.


EDIT:

OK, so it can only show 26 lines of code? That seems weird.

Can't get the code tag working, so I jus attached a txt file of it instead.

Attached File  slotmachine example.txt   1.99KB   78 downloads


The people downvoted you because the code above is written in C++...
He wrote a C++ code but without implementing the object oriented programming paradigm style. But is C++
The OOP is only a paradigm not the complete language


I dunno, really... you mean it's C++ because the file is called .cpp, and because he uses std::cout? I would say the lack of OOP is a stronger argument than that, since that was the whole point of C++. The creator himself explains that very clearly. He wanted to merge OOP with the performance of C.

#20 Sid_TheBeginner   Members   -  Reputation: 157

Like
2Likes
Like

Posted 23 June 2012 - 10:22 PM



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++.


Sorry I've no clue what your talking about. Seems its advanced stuff.


I don't know about "advanced", it's what C++ was created for. It's pretty much the whole point of it, as an improvement upon C. If you want to learn C++, you need to start right away. If you want to use C instead, then that's a choice you need to make, but you need to be aware of the difference. I don't know if all these other guys thinks I'm just messing with you or something, but I think that once you look at the code example I attached to my other post, you'll understand that C++ is slightly different from C. Hopefully you other guys would care to listen to me and see the truth in what I'm saying instead of jsut downvoting me as if I was trolling. I, for one, am trying to teach a newbie the proper way to code, and good practices. I don't know what the rest of you are trying to do.


Your code works amazingly well Posted Image Actually I'm learning C++ as my first programming language. So I called your stuff advanced. I'm an altogether beginner programmer. I'm saving your code buddy. When I jump into these topics, I'll understand your code and would be able to admire it much more.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS