Sinisterly
Craps Game - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: Craps Game (/Thread-Craps-Game)

Pages: 1 2 3


RE: Craps Game - ArkPhaze - 11-04-2013

(11-04-2013, 12:54 AM)chmod Wrote:
(11-04-2013, 12:30 AM)ArkPhaze Wrote: I guess I'll post this here since you didn't get what I was hinting at. I just wanted to see if you could find it on your own. Smile

Code:
int bank = 1000; int bet; scanf("%d", &bet); while (bet > bank || bet == 0) {} std::cout << "Your bet: " << bet << std::endl;

Result:
Code:
-234 Your bet: -234

This is critical here, and something most often forgotten about. 'int' is a signed datatype; it extends into integer values below 0 as well.

Your completely right and it's something I overlooked, fixed now. Thanks again for your input I appreciate it, while we are on the subject the program doesn't really parse things efficiently anyway and entering anything but an integer breaks it so I'm going to play around with the code tomorrow and try to fix this.

And this is why experience C programmers try to avoid scanf(), along with the fact that it leaves junk in the buffer from user input. The way around this is to use fgets() typically (do not use gets()), and parse the string manually for correct input, and parse to the type you need it to be. For that there are functions like atoi() from the <stdlib.h> header which return an integer from a c-string. If you felt like re-inventing the wheel for learning purposes, try to create your own function to do this. (*It is NOT too difficult, if needed, I could show you.)


RE: Craps Game - chmod - 11-04-2013

(11-04-2013, 01:27 AM)ArkPhaze Wrote:
(11-04-2013, 12:54 AM)chmod Wrote:
(11-04-2013, 12:30 AM)ArkPhaze Wrote: I guess I'll post this here since you didn't get what I was hinting at. I just wanted to see if you could find it on your own. Smile

Code:
int bank = 1000; int bet; scanf("%d", &bet); while (bet > bank || bet == 0) {} std::cout << "Your bet: " << bet << std::endl;

Result:
Code:
-234 Your bet: -234

This is critical here, and something most often forgotten about. 'int' is a signed datatype; it extends into integer values below 0 as well.

Your completely right and it's something I overlooked, fixed now. Thanks again for your input I appreciate it, while we are on the subject the program doesn't really parse things efficiently anyway and entering anything but an integer breaks it so I'm going to play around with the code tomorrow and try to fix this.

And this is why experience C programmers try to avoid scanf(), along with the fact that it leaves junk in the buffer from user input. The way around this is to use fgets() typically (do not use gets()), and parse the string manually for correct input, and parse to the type you need it to be. For that there are functions like atoi() from the <stdlib.h> header which return an integer from a c-string. If you felt like re-inventing the wheel for learning purposes, try to create your own function to do this. (*It is NOT too difficult, if needed, I could show you.)

I've edited my code to use fgets() and atoi() and it's done the job only allowing valid input now. How would I go about creating my own function for this the way I'm thinking would be to use switch statements to check input but I'm sure there are better ways of doing this.


RE: Craps Game - ArkPhaze - 11-04-2013

For a cstring to an integer? I'll post my method of doing it when I get home. In case I forget, if you see me online remind me via PM.

Grab chars right to left and hold a power of 10's variable that gets multiplied by 10 every char you evaluate. To go from a char to an integer, just subtract the integral value of '0' from the current char's integer. Use that value multiplied by your variable to make sure it holds a value in its proper place, multiply your variable by 10, and keep going until you reach char at index 0. Summing these digits multiplied by the powers of 10 as you go along. strlen() - 1 is your ones place digit that you start with. You CAN start with the first digit, left to right, but then you'd be dividing by 10, after figuring out what power of 10 that first digit should be multiplied by. You can use logarithms to help with that btw. Excuse my posdibly incoherent post, it's unusual for me, but i'm posting from work on my iphone.


RE: Craps Game - ArkPhaze - 11-05-2013

Here's a mini test program demonstrating what I was talking about:
Code:
#include <stdio.h> #include <string.h> int str_toint(char *s) { int result = 0; int pow10 = 1; size_t i = strlen(s); do { result += pow10 * (s[--i] - '0'); pow10 *= 10; } while (i > 0); return result; } int main(void) { char *s = "234567"; int result = str_toint(s); printf("%d\n", result); return 0; }

Now you have an integer of the value: 234567.

NOTE: If you're okay with the way it is, then fine. But I could also include a data overflow check and change the return type to a boolean for success/failure in that case, and have an out param for the resulting integer to be used if the function returns true. What I mean by this, is *obviously* a string like "9999999999999999999999" won't fit into a 32 bit integer datatype.

Although, there's even more ways aside from atoi(), and even more if this was C++ and not C.


RE: Craps Game - Madhatter - 11-09-2013

Hello to everyone. I started learning C++ about one month ago and gained only basic understanding of that language. However when i saw this program i wondered if i could make this program my own way. I gave it a try and here's what i got:
Spoiler: Program
Code:
#include <iostream> #include <time.h> #include <stdlib.h> using namespace std; int main() { int p,s,m,m2,t; //Named variables to be comfortable for me srand(time(NULL)); p=1000; while(p>0 and p<200000){//Gameplay m2=0; cout << "Currently you have "<<p<<" amount of money" << endl; cout<<"Write amount of money you want to bid or type 0 to view rules of the game"; cin>>s; if (s==0){cout<<"Two die are rolled and the numbers are summed up.If player rolls 7 or 11 on the first roll player wins.If player rolls 2, 3 or 12 on the first roll player looses.If player rolls any other number the total become your points.To win after the first roll you must roll a total equal to the number of points you have.After the first roll 7 is game over";} else if(s<0 or s>p){cout<<"stop pissing around and bid.";} else {p=p-s; m=rand()%12 + 1; //First rool if (m==7 or m==11) {cout<<"You rolled"<<m<<"and won"<<s<<"$";s=s*2;p=p+s;} else if(m==2 or m==3 or m==12) {cout<<"you rolled "<<m<<" and lost"<<s<<"$";} //Game continues else {cout<< "You rooled "<<m<<" and continued rooling.";t = m;cout<<"Your rools: "; while (m2!=7 and m2!=t){m2=rand()%12 + 1; cout<<" "<<m2; if (m2==7){cout<<endl<<" You rolled "<<m2<<" and won "<<s<<"$"<<endl; s=s*2;p=p+s; } else if(m2==t){cout<<endl<<" you rolled "<<m2<<" and lost "<<s<<"$"<<endl; } }}}} //End game phrases if (p<=0){cout<<"Your wallet is empty.You lost";} else {cout<<"You was kicked from casino for winning too much.Lucky bastard!";} return 0;}
Tell me what you think.


RE: Craps Game - Ex094 - 11-14-2013

@Madhatter The first thing I always advice to beginners is that "Have a clean code", Your's look pretty messed up


RE: Craps Game - Madhatter - 11-14-2013

How could i do that exactly?I can't use any more simple ways which would shorten my code cause i dont know them yet.


RE: Craps Game - Madhatter - 11-14-2013

How could i do that exactly?I can't use any more simple ways which would shorten my code cause i dont know them yet.


RE: Craps Game - Madhatter - 11-14-2013

How could i do that exactly?I can't use any more simple ways which would shorten my code cause i dont know them yet.


RE: Craps Game - Slarek - 11-14-2013

@Madhatter
Instead of using stdlib.h and time.h, you could use ctime and cstdlib.
And instead of using namespace std; you should use std::