Page 1 of 1

8 Ball in JAVA text based game version of an 8 Ball

#1 computerfox  Icon User is offline

  • straight vegetarian kid

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

Posted 12 August 2009 - 11:21 AM

In this tutorial we will take a look at how to make a text version game of the popular 8 Ball.

First, as usual we need imports:


import java.util.*;




Okay, so not much to import.

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:


public static int randomNum(){				//since it's outside of the driver function, it must be public static
		Random gen=new Random();
		int NUM=gen.nextInt(5);		 //picks a random number from 0-4
		
		return NUM;
	}





The other method:


public static String check(String line,int NUM){			 
		int randomCheck=NUM;
		String answer=" ";					 //Declaration and initialization
		
		
		System.out.println(randomCheck);	  //checks what the number is
		
		
		//this section can be edited however you'd wish, but for this version it is how it is.
		if(randomCheck==0)
			answer="NO...\n";
		
		
		if(randomCheck==1)
			answer="Yes...\n";
		
		
		if(randomCheck==2)
			answer="Maybe...\n";
		
		
		if(randomCheck==3)
			answer="Only time can tell...\n";
		
		
		if(randomCheck==4)
			answer="Not enough information...\n";
		
		return answer;  //returns a string
	}
	



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


public static void main (String args[]) {
		
		String line; //variable used to enter string.  it will only check the first string, but that's not 
								// important since the only thing that's being checked is the random number.
								//you can also edit your program to check if it says forever, since nothing is forever
							   //but we might do that at a later time.
		int NUM=randomNum();
		Scanner fox=new Scanner(System.in);	 //input since JAVA doesn't work with cin>>
		

//This section isn't necessary, it just makes the program look nicer and a bit more professional.	
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		System.out.println("						WELCOME TO THE NUMBER 8 BALL GAME							");
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		
		//gets question
		System.out.print("Enter question: ");
		line=fox.nextLine();
		
				//outputs the answer with the variables being inputted.
		System.out.println(check(line,NUM));
	}







And all together the code will look like this:


import java.util.*;


public class number8ball {
	
	public static int randomNum(){
		Random gen=new Random();
		int NUM=gen.nextInt(5);
		
		return NUM;
	}
	public static String check(String line,int NUM){
		int randomCheck=NUM;
		String answer=" ";
		
		
		System.out.println(randomCheck);
		
		
		
		if(randomCheck==0)
			answer="NO...\n";
		
		
		if(randomCheck==1)
			answer="Yes...\n";
		
		
		if(randomCheck==2)
			answer="Maybe...\n";
		
		
		if(randomCheck==3)
			answer="Only time can tell...\n";
		
		
		if(randomCheck==4)
			answer="Not enough information...\n";
		
		return answer;
	}
	

	public static void main (String args[]) {
		
		String line;
		int NUM=randomNum();
		Scanner fox=new Scanner(System.in);
		
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		System.out.println("						WELCOME TO THE NUMBER 0 BALL GAME							");
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		
		
		System.out.print("Enter question: ");
		line=fox.nextLine();
		
		System.out.println(check(line,NUM));
	}
}





You can also make this into a loop, asking the user if he/she would like to try again. If you wish for this feature, all you would need is a while loop with the parameter being until the user says no or something to that extent. so something like this:

 
	
	public static void main (String args[]) {
		
		String line; //variable used to enter string.  it will only check the first string, but that's not 
		// important since the only thing that's being checked is the random number.
		//you can also edit your program to check if it says forever, since nothing is forever
		//but we might do that at a later time.
		int NUM=randomNum();
		Scanner fox=new Scanner(System.in);	 //input since JAVA doesn't work with cin>>
		int again=1;
		
		
		//This section isn't necessary, it just makes the program look nicer and a bit more professional.	
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		System.out.println("						WELCOME TO THE NUMBER 8 BALL GAME							");
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		
			//gets question
			System.out.print("Enter question: ");
			line=fox.nextLine();
			
			//outputs the answer with the variables being inputted.
			System.out.println(check(line,NUM));
			
			System.out.print("Again? ");
			again=fox.nextInt();
			
		if(again==1){
			
			while(again==1){
				System.out.print("Enter question: ");
				line=fox.nextLine();
				line=fox.nextLine();  //this is necessary because if you don't then it won't let you ask
				
				//outputs the answer with the variables being inputted.
				System.out.println(check(line,NUM));
				
				System.out.print("Again? ");
				again=fox.nextInt();
			}
		}
		
		System.out.println("Thank you for playing...");
	}




I guess that's all I can put down. Thank you for stopping by and I hope all of this was helpful.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1