Answer to Question #8990 in Java | JSP | JSF for java dice
2012-05-08T10:50:04-04:00
simple java dice game
Each player starts-out with a cumulative score of zero. Then each player rolls three
dices and a random number appears for each dice. The summation of each number is
added in each player’s score. The winner is the player that has the highest score in x
turns based on what the players initially decided.
1
2012-05-11T07:13:45-0400
import java.util.Random; public class DiceGame { /** & * @param args & */ public static void main(String[] args) { & // TODO Auto-generated method stub & Player p1 = new Player("Player 1"); & Player p2 = new Player("Player 2"); & p1.play(); & p2.play(); & if(p1.getScore() > p2.getScore()){ & System.out.println("Player 1 win with: " + p1.getScore()); & } else & if(p1.getScore() == p2.getScore()){ & System.out.println("Draw"); & } else & if(p1.getScore() < p2.getScore()){ & System.out.println("Player 2 win with: " + p2.getScore()); & } & } } import java.util.Random; public class Player { private int score = 0; public String name; Player(String name){ & this.name = name; } private void shuffle(){ & Random rand = new Random(); & int p = 0; & while(p==0){ & p = rand.nextInt(7); & } & score += p; & System.out.println(name + " scored " + p); } public void play(){ & for(int i=0; i<3; i++) & shuffle(); } public int getScore(){ & return score; } }
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS !
Learn more about our help with Assignments:
Java JSP JSF
Comments
Leave a comment