Codex

A dice game

Jul 28th, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. /*
  2. The first player to reach 50 points win
  3. */
  4. import javax.swing.JOptionPane;
  5. class Dice
  6. {
  7. public void game()
  8. {
  9. int n=0,random;
  10. int s1=0,s2=0;
  11. String s;
  12. while(s1<50 && s2<50)
  13. {
  14. random=(int)(Math.random()*100);
  15. while(random>6)
  16. random=random-4;
  17. if((n%2)==0)
  18. {
  19. s1+=random;
  20. JOptionPane.showMessageDialog(null,"Player 1 \n"+random+"\nTotal = "+s1);
  21. }
  22. else
  23. {
  24. s2+=random;
  25. JOptionPane.showMessageDialog(null,"Player 2 \n"+random+"\nTotal = "+s2);
  26. }
  27. JOptionPane.showMessageDialog(null,"Press ok to continue");
  28. n++;
  29. }
  30. if(s1>50 && s1>s2)
  31. JOptionPane.showMessageDialog(null,"Player 1 wins");
  32. else if(s2>50 && s2>s1)
  33. JOptionPane.showMessageDialog(null,"Player 2 wins");
  34. else
  35. JOptionPane.showMessageDialog(null,"Tie");
  36. }
  37. public static void main(String args[])
  38. {
  39.     Dice ob=new Dice();
  40.     ob.game();
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment