View Single Post
  #7 (permalink)  
Old 10-06-2008, 10:52 AM
serjant's Avatar
serjant serjant is offline
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 484
Rep Power: 2
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
Default
I made for my friend some time ago this stuff,the same homework as you have so here is your class dice,but the other stuff do by yourselves buddy


Code:
import java.util.Scanner; //scanner class
import java.util.Random; //Random class

/**
	This Program will input numbers between 1 and 6 from the user, and compare them to the output of 
	the dice being rolled within 3 rolls. 
*/

public class dice
{
	
	
	public void dieImage(int dice1)
	{
		switch (dice1)
		{
		case 1:
			System.out.print("     \n");
			System.out.print("  *  \n");
			System.out.println("     ");
			break;
		case 2:
			System.out.println("*    ");
			System.out.println("     ");
			System.out.println("    *");
			break;
		case 3:
			this.printThree();
			break;
		case 4:
			System.out.print("*   *\n");
			System.out.print("     \n");
			System.out.println("*   *");
			break;
		case 5:
			System.out.print("*   *\n");
			System.out.print("  *   \n");
			System.out.println("*   *");
			break;
		case 6:
			this.printSix();
			break;			
		}
					
	
	}
	public void printSix(){
		int i=0,j;
		for(;i<3;i++){
			for(j=0;j<2;j++){
				System.out.print("*   ");
			}
			System.out.println("");
		}
	}
	
	
	
	public void printThree(){
		for(int i=0;i<3;i++){
			for(int j=0;j<1;j++){
				for(int k=0;k<i;k++){
					System.out.print("  ");
				}
				System.out.print("*");
			}
			System.out.println();	
		}
	}
	
	
	
	public void play(int userGuess){
		int times=1,sum=0;
		int dice1,dice2;
		do{	
			//create a Random class object. 
			Random randomNumbers = new Random();
	
			//Get two random numbers.
			dice1 = randomNumbers.nextInt(6)+1;
			dice2 = randomNumbers.nextInt(6)+1;
			System.out.println(dice1);
			System.out.println(dice2);
			//output the number of each die with asteriks by calling the method
			System.out.println("Die 1      ");
			
			this.dieImage(dice1);
			System.out.println("Die 2       ");
			this.dieImage(dice2);
			
			System.out.println("-----------------");
	  		//Sum the two die numbers
			sum = (dice1 + dice2);
		
			
			times++;
		} while (userGuess != sum&&times<4);
		if (userGuess == sum)
		{
		System.out.println("YOU WIN!!!");
		}
		else 
		{
		System.out.println("SORRY YOU LOSE!");
		}
	}
	
	public static void main(String[] args)
	{
		
		boolean exit=false;
		int userGuess;
		String answer;
		dice d=new dice();
		//Create a scanner object for keyboard input.
		Scanner keyboard = new Scanner(System.in);
		
		//get number from user
		System.out.println("Please enter a number between " +
								"2 and 12.");
	
		userGuess = keyboard.nextInt();
		d.play(userGuess);
		
			
		
		
		
		do{
			System.out.println("Wanna continue laying?Press Y/N");
			answer=keyboard.next();
			if(answer.matches("Y".toLowerCase())||answer.matches("Y")){
				//get number from user
				System.out.println("Please enter a number between " +
										"2 and 12.");
			
				userGuess = keyboard.nextInt();
				d.play(userGuess);
			}
			else{
				exit=true;
			}
		}while(!exit);
		System.exit(0);
	}
		
	
}
Reply With Quote