playing PIG

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 10
Reputation: estefania is an unknown quantity at this point 
Solved Threads: 0
estefania estefania is offline Offline
Newbie Poster

playing PIG

 
0
  #1
33 Days Ago
This program is a game which allows the user to play against a computer in a dice game called ``Pig''.
Rules are:
* Players alternate turns and try to be the first to reach 100 points.
* A turn consists of rolling a die repeatedly until either a 1 is rolled or the player chooses to quit rolling.
* If a turn ends with the roll of a 1 then 0 points are earned on that turn.
* If a turn ends by the player choosing to hold then the number of points earned is the sum of the rolls for that turn.
* The points earned in a turn are added to a player's total score for the game.

my problem: How do i get the total score for the round to add up and each time it passes thru the methods?

  1. import java.util.Scanner;
  2.  
  3. public class PlayPig
  4. {
  5. public static void main(String [] args)
  6. {
  7. int usertotal, comptotal;
  8.  
  9. do
  10. {
  11. usertotal=userTurn();
  12. comptotal=compTurn();
  13. }while(usertotal<100 || comptotal<100);
  14.  
  15. }
  16.  
  17. public static int userTurn()
  18. {
  19. Scanner scan= new Scanner(System.in);
  20. int random, current, usertotal;
  21. String ans="n";
  22.  
  23. current=0;
  24.  
  25. do
  26. {
  27. random= (int) (Math.random()*6.0)+1;
  28. System.out.println("Your roll is "+random);
  29.  
  30.  
  31. if (random==1)
  32. {
  33. current = 0;
  34. System.out.println("Your score is: "+current);
  35. }
  36.  
  37. else if (random !=1 && random<=6)
  38. {
  39. current= current+random;
  40. System.out.println("Your current score is: "+current);
  41. System.out.println("Do you want to roll again? [Y/N]");
  42. ans=scan.nextLine();
  43. }
  44. }while(ans.equals("Y")&& random!= 1);
  45.  
  46. System.out.println("Your total score is: "+current);
  47.  
  48. usertotal=current;
  49. return usertotal;
  50. }
  51.  
  52. public static int compTurn()
  53. {
  54. Scanner scan= new Scanner(System.in);
  55. int times, random, current, comptotal=0;
  56.  
  57. current=0;
  58.  
  59. times= (int) (Math.random()*5.0)+1;
  60.  
  61. for(int i=0; i< times; i++)
  62. {
  63. random= (int) (Math.random()*6.0)+1;
  64. System.out.println("Comp roll is "+random);
  65.  
  66. if(random == 1)
  67. {
  68. current=0;
  69. i= times;
  70. System.out.println("Comp score is: "+current);
  71. }
  72.  
  73. else if(random !=1 && random<=6)
  74. {
  75. current= current+random;
  76. }
  77.  
  78. comptotal =current;
  79. System.out.println("Comp total score is: "+comptotal);
  80. }
  81. return comptotal;
  82. }
  83.  
  84. }
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC