| | |
playing PIG
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
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?
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?
Java Syntax (Toggle Plain Text)
import java.util.Scanner; public class PlayPig { public static void main(String [] args) { int usertotal, comptotal; do { usertotal=userTurn(); comptotal=compTurn(); }while(usertotal<100 || comptotal<100); } public static int userTurn() { Scanner scan= new Scanner(System.in); int random, current, usertotal; String ans="n"; current=0; do { random= (int) (Math.random()*6.0)+1; System.out.println("Your roll is "+random); if (random==1) { current = 0; System.out.println("Your score is: "+current); } else if (random !=1 && random<=6) { current= current+random; System.out.println("Your current score is: "+current); System.out.println("Do you want to roll again? [Y/N]"); ans=scan.nextLine(); } }while(ans.equals("Y")&& random!= 1); System.out.println("Your total score is: "+current); usertotal=current; return usertotal; } public static int compTurn() { Scanner scan= new Scanner(System.in); int times, random, current, comptotal=0; current=0; times= (int) (Math.random()*5.0)+1; for(int i=0; i< times; i++) { random= (int) (Math.random()*6.0)+1; System.out.println("Comp roll is "+random); if(random == 1) { current=0; i= times; System.out.println("Comp score is: "+current); } else if(random !=1 && random<=6) { current= current+random; } comptotal =current; System.out.println("Comp total score is: "+comptotal); } return comptotal; } }
![]() |
Similar Threads
- computer freezes when playing games (Troubleshooting Dead Machines)
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- Playing a game of PIG, while loop not working properly? (C++)
- no sound when playing mp3 or wma files (Windows NT / 2000 / XP)
- Computer freezes while playing GAMES (Troubleshooting Dead Machines)
- Computer generated lag while playing MOHAA (Troubleshooting Dead Machines)
Other Threads in the Java Forum
- Previous Thread: Help with hw...
- Next Thread: Fetch and display online images.
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows





