I don't know alot about C++ but I know almost enough to make a slot machine game. The only thing I don't know is how to generate the random numbers. If you could give me some code or explain how that would be great.
I don't know alot about C++ but I know almost enough to make a slot machine game. The only thing I don't know is how to generate the random numbers. If you could give me some code or explain how that would be great.
I have done a bit of work on it, it hasn't got the random conponent and isn't fully functional but why doesn't it compile? Here it isCode:#include <iostream> using namespace std; int main() { int return1=5; int return2=5; int return3=5; int coins=10; int input; cout << "Welcome to Slot Machine" << endl; cout << "You have 10 coins" << endl; cout << "Please press 1 to play, or 2 to exit and then hit enter"; cin >> input; if(input==1) { cout << "You have chosen to play. Hit enter to input a coin and play";cin.get(); cout << return1 << " " << return2 << " " << return3 << endl; if(return1==return2&&return1==return3) { coins=coins+10; cout << "You got the jackpot of 10 coins" << endl; cout << "You now have " << coins << " coins.";cin.get(); cout << "Congratulations, you won."; } else if(return1!=return2||return1!=return3||return2!=return3) { cout << "You lose, I am sillyI am sillyI am sillyI am sillyI am silly"; } else { cout << "You have chosen to exit, please play again." << endl; } return 0; }
That still means I need to learn how to make it random.
this is a example:
Code:#include<cstdio> #include<ctime> main() { srand((unsigned)time(0)); //use time() function as seed of rand() function int randomnum=rand()%1000; //define a int,and initialize it to a 'random number' // %1000 means this number wouldn't beyond 1000 }
blow me ... ...
The FAQ has a great tutorial on random numbers
essentially, what you will want to do.. is to use the rand() function.. which will return an unsigned random number ranging from zero to RAND_MAX (which is a very large number). Anywhoo.. use the modulus operator as an upper bound on the range of numbers you wish to use...
rand() may return a very large random number, but using the modulus operator will continously divide this larger number by 5 (in this example) until 5 is no longer able to fit into the randomly generated number.. and will return the remainder (if any)... and the remainder cannot be larger than 5Code:slot_output1 = rand()%5 //will return a random integer between 0 and 4
also, don't forget to seed your random number generator.. as per the faq.
edit: Hermitsky wins! (getting slow at me old age)
Last edited by The Brain; 09-21-2004 at 07:03 AM.
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
Thankyou, but that still doesn't answer why it wont compile.
Here's the edit with random, still bad but should work. Why won't compile?the debugger says that there is a syntax error at line 39, a line that doesn't exist in my compiler, devc++Code:#include <iostream> using namespace std; int main() { srand((unsigned)time(0)); int return1=rand()%10; int return2=rand()%10; int return3=rand()%10; int coins=10; int input; cout << "Welcome to Slot Machine" << endl; cout << "You have 10 coins" << endl; cout << "Please press 1 to play, or 2 to exit and then hit enter"; cin >> input; if(input==1) { cout << "You have chosen to play. Hit enter to input a coin and play";cin.get(); cout << return1 << " " << return2 << " " << return3 << endl; if(return1==return2&&return1==return3) { coins=coins+10; cout << "You got the jackpot of 10 coins" << endl; cout << "You now have " << coins << " coins.";cin.get(); cout << "Congratulations, you won."; } else if(return1!=return2||return1!=return3||return2!=return3) { cout << "You lose";cin.get(); } else { cout << "You have chosen to exit, please play again." << endl; } return 0; }
Last edited by pizzapie; 09-21-2004 at 05:56 AM.
it works now! try it if you care, its still bad but if you like it that much im sure you can fix it up, ill post the perfected version when it is finished.Code:#include <iostream> using namespace std; int main() { srand((unsigned)time(0)); int return1=rand()%10; int return2=rand()%10; int return3=rand()%10; int coins=10; int input; cout << "Welcome to Slot Machine" << endl; cout << "You have 10 coins" << endl; cout << "Please press 1 to play, or 2 to exit and then hit enter"; cin >> input; if(input==1) { cout << "You have chosen to play. Hit enter to input a coin and play"<<endl;cin.get(); cout << return1 << " " << return2 << " " << return3 << endl;cin.get(); } if(return1==return2&&return1==return3) { coins=coins+10; cout << "You got the jackpot of 10 coins" << endl; cout << "You now have " << coins << " coins.";cin.get(); cout << "Congratulations, you won.";cin.get(); } else if(return1!=return2||return1!=return3||return2!=return3) { cout << "You lose, I am sillyI am sillyI am sillyI am sillyI am silly";cin.get(); } else { cout << "You have chosen to exit, please play again." << endl; } return 0; }
absolutly, you missed something,
this works fine:
Code:#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand((unsigned)time(0)); int return1=rand()%10; int return2=rand()%10; int return3=rand()%10; int coins=10; int input; cout<<"Welcome to Slot Machine"<<endl; cout<<"You have 10 coins"<<endl; cout<<"Please press 1 to play, or 2 to exit and then hit enter"<<endl; cin>>input; if(input==1) { cout <<"You have chosen to play. Hit enter to input a coin and play"<<endl; cin.get(); cout <<return1<<" "<<return2<<" "<<return3<<endl; if(return1==return2&&return1==return3) { coins=coins+10; cout<<"You got the jackpot of 10 coins"<<endl; cout<<"You now have "<<coins<<" coins."<<endl; cin.get(); cout<<"Congratulations, you won."<<endl;; } else if(return1!=return2||return1!=return3||return2!=return3) { cout <<"You lose, I am sillyI am sillyI am sillyI am sillyI am silly"<<endl; } else { cout<<"You have chosen to exit, please play again."<<endl; } return 0; } }
blow me ... ...
why include those two libraries? it works without them
time() is defined in ctime,rand() is defined in cstdlib
blow me ... ...