Page 1 of 1

8Ball Game C++

#1 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Posted 31 August 2009 - 07:46 AM

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:


#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:


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:


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:


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?


#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.dreaminco...topic119859.htm.

This post has been edited by computerfox: 31 August 2009 - 09:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: 8Ball Game C++

#2 sepir0th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-October 09

Posted 09 October 2009 - 09:13 AM

tq bro!!it was so usefull!
Was This Post Helpful? 0
  • +
  • -

#3 ctil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 20-October 09

Posted 27 October 2009 - 05:06 PM

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.
Was This Post Helpful? 0
  • +
  • -

#4 Aazor  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 23-November 09

Posted 23 November 2009 - 05:23 PM

no forever huh?? hahahaha
Was This Post Helpful? 0
  • +
  • -

#5 fundriver  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-December 09

Posted 24 December 2009 - 07:24 AM

it isn´t really important but i would add :D

a sleep function after
cout<<"\nThank you for playing.  Please come agian soon...\n\n";


like:
cout<<"\nThank you for playing.  Please come agian soon...\n\n";
Sleep(3000); //this is there that the programm waits 3 seconds before it closes otherwise you can´t read the text


but if you add the sleep function recognise that there must be
#include <windows.h>

added the at the top of the cpp file

after it the one who uses the programm could read the Text :ph34r:

This post has been edited by fundriver: 24 December 2009 - 07:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1