Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Pig game in java with classes:fixing errors in code

$
0
0
I need help with pig game with two dice. if someone rolls a one the round score is 0, if someone rolls two ones than the total score is 0. this is the same for the computer

code for the die class

public class Die {
	
	private int faceValue;
	private int numFaces;
	
	public Die(){
		numFaces = 6;
		Roll();
	}
	
	public void Roll(){
		faceValue = (int)(Math.random()*6)+1;
	}
	
	public int GetNum(){
		return faceValue;
	}

}




code for pair of dice(two dice)

public class PairOfDice {
	
	private Die a;
	private Die b;
	
	public PairOfDice(){
		Die a=new Die();
		Die b=new Die();
	}
	
	public void roll(){
		a.GetNum();
		b.GetNum();
	}
	
	public int rollTotal(){
		return (a.GetNum())+(b.GetNum());
	}
	
	public Die getA(){
		return a;
	}
	
	public Die getB(){
		return b;
	}
	
	public String toString(){
		return a.toString()+"\n"+b.toString();
	}

}




code for pig main method

import java.util.Scanner;

public class PIG {

	public static void main(String[] args) {
		
		int totalscore=0;
		int roundscore=0;
		int computerroundscore=0;
		int computertotalscore=0;
		
		Scanner scan = new Scanner (System.in);
		Die d1 = new Die();
		Die d2 = new Die();
		PairOfDice pd1 =new PairOfDice();
		
		String user = "y";
		String computer = "y";
		
		System.out.println("Roll the dice!!!");
		
		while (totalscore < 100 && computertotalscore < 100){
			
			while (user.equalsIgnoreCase("y")){
			
				d1.Roll();
				d2.Roll();
			
				if(d1.GetNum()==1 || d2.GetNum()==1){
					roundscore =0;
			
				}
			
				if(pd1.rollTotal()==2){
				totalscore=0;
			
				}
			
				roundscore = pd1.rollTotal() + roundscore;
			
				System.out.println("Your round score is: " + roundscore + " and your total score is: " + totalscore);
				System.out.println("Roll the dice!!! Enter \" Y\" to roll again, and enter \"N\" to end your turn" );
			
				user = scan.nextLine();
			}
			
			user = "Y";
			
			totalscore = totalscore + roundscore;
			
			while (computerroundscore <20){
				d1.Roll();
				d2.Roll();
				
				if(d1.GetNum()==1 || d2.GetNum()==1){
					computerroundscore=0;
					break;
				}
				
				if(pd1.rollTotal()==2){
					computertotalscore=0;
					break;
				}
				
				computerroundscore= pd1.rollTotal() + computerroundscore;
				
				System.out.println("Computer round score is: " + computerroundscore + 
						" and the computer total score is: " + computertotalscore);
			}
			
			computertotalscore = computerroundscore + computertotalscore;
			
			System.out.println("Computer total score is " + computertotalscore);
		}
		
		if (totalscore >= 100)
			System.out.println("You win!!!");
		else if (computertotalscore >= 100)
			System.out.println("Im sorry, you lost!!!");
	
	}
	
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>