School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,148 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,725 people online right now. Registration is fast and FREE... Join Now!




8Ball Game C++

 
Reply to this topicStart new topic

> 8Ball Game C++

computerfox
*****



post 31 Aug, 2009 - 06:46 AM
Post #1


We've seen this at least once in JAVA, now in this tutorial we will take a look at how to make a text version game of the popular 8 Ball, but instead of JAVA code, you'll see C++ code.

First, as usual we need imports:

CODE


#include <iostream>    //input/output
#include <string>     //strings
#include <cstdlib>   //random
using namespace std;    //no need for std::



Okay, so not much to import, well, more than the JAVA version, but it's still only 3 imports.

Next step, we will have to make the methods. One to make a random integer and another to to get your answer.

The random number generator is pretty simple:

CODE


int randomNum(){
    
    int NUM=(rand()%5);     //the modulus enables it to stop at a certain number, giving you a result in a range
    
    return NUM;            //returns that number
}



The other method:

CODE


string check(string line,int NUM){
    int randomCheck=NUM;
    string answer,tmp;
                                            
    
    //since there is no such thing as "forever" we have to take out any thought of that word
    
    
    for(int i=0;i<line.length();i++){
        if(line[i]=='f'){
            for(int j=0;j<7;j++){
                tmp+=line[i];
                i++;
            }
        }
    }
    if(tmp=="forever"){
        answer="Hell no...\n";    
    }
            
    else
        
    if(randomCheck==0){
        answer="NO...\n";
    }
    
    else
        
    if(randomCheck==1){
        answer="Yes...\n";
    }
    
    else
        
    if(randomCheck==2){
        answer="Maybe...\n";
    }
    
    else
        
    if(randomCheck==3){
        answer="Only time can tell...\n";
    }
    
    else
        
    if(randomCheck==4){
        answer="Not enough information...\n";
    }
    return answer;
}





We have everything, cept for the driver function. How would that look? Well, it would look something like this:

CODE


int main () {
    
    string line;
    int NUM=randomNum(),again;
    
//This section isn't necessary, it just makes the program look nicer and a bit more professional.
    cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
    cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
    cout<<"                        WELCOME TO THE NUMBER 8 BALL GAME                            \n";
    cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
    cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
    
    cout<<"Enter question: ";    //prompt for question
    getline(cin,line);   //input of question
    
    cout<<check(line,NUM)<<endl;  //output of the answer to your question
    
    cout<<"1-Again 2-NO: ";  //prompt to continue
    cin>>again;   //input of answer
    
    //loop
    while(again==1){
        
        cout<<"Enter question: ";
        cin.get();
        getline(cin,line);
        
        NUM=randomNum();
        cout<<check(line,NUM)<<endl;
        
        cout<<"1-Again 2-NO: ";
        cin>>again;
    }
    
    cout<<"\nThank you for playing.  Please come agian soon...\n\n";   //exit note
    
    return 0;   //returns 0 since main is int
}



We have all the parts, but how would the code look altogether?

CODE


#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int randomNum();
string check(string line,int NUM);
int main () {
    
    string line;
    int NUM=randomNum(),again;
    
    cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
    cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
    cout<<"                        WELCOME TO THE NUMBER 8 BALL GAME                            \n";
    cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
    cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
    
    cout<<"Enter question: ";
    getline(cin,line);
    
    cout<<check(line,NUM)<<endl;
    
    cout<<"1-Again 2-NO: ";
    cin>>again;
    
    while(again==1){
        
        cout<<"\nEnter question: ";
        cin.get();
        getline(cin,line);
        
        NUM=randomNum();
        cout<<check(line,NUM)<<endl;
        
        cout<<"1-Again 2-NO: ";
        cin>>again;
    }
    
    cout<<"\nThank you for playing.  Please come agian soon...\n\n";
    
    return 0;
}

int randomNum(){
    
    int NUM=(rand()%5);
    
    return NUM;
}
string check(string line,int NUM){
    int randomCheck=NUM;
    string answer,tmp;
                                            
    
    //since there is no such thing as "forever" we have to take out any thought of that word
    
    
    for(int i=0;i<line.length();i++){
        if(line[i]=='f'){
            for(int j=0;j<7;j++){
                tmp+=line[i];
                i++;
            }
        }
    }
    if(tmp=="forever"){
        answer="Hell no...\n";    
    }
            
    else
        
    if(randomCheck==0){
        answer="NO...\n";
    }
    
    else
        
    if(randomCheck==1){
        answer="Yes...\n";
    }
    
    else
        
    if(randomCheck==2){
        answer="Maybe...\n";
    }
    
    else
        
    if(randomCheck==3){
        answer="Only time can tell...\n";
    }
    
    else
        
    if(randomCheck==4){
        answer="Not enough information...\n";
    }
    return answer;
}




What about the results?

Attached Image




******WARNING******** PLEASE DON'T TAKE ANSWERS TOO SERIOUSLY. IT'S ONLY A GAME.

I guess that's all I can put down. Thank you for stopping by and I hope all of this was helpful. If you are looking for the JAVA version, please visit http://www.dreamincode.net/forums/showtopic119859.htm.

This post has been edited by computerfox: 31 Aug, 2009 - 08:05 PM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

sepir0th
*



post 9 Oct, 2009 - 08:13 AM
Post #2
tq bro!!it was so usefull!
Go to the top of the page
+Quote Post

ctil
*



post 27 Oct, 2009 - 04:06 PM
Post #3
I do not know if it is just my computer or not but when i ran this and i put in "am i 22" as the third question, it created an infinite loop. Maybe someone else should check it out and see if it does the same thing.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 04:11PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month