| | |
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.
Views: 186 | Replies: 0
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation binary bluetooth c# capture chat chatprogramusingobjects class classes client code color component count database design draw eclipse eclipsedevelopment encryption error event exception file fractal game givemetehcodez graphics gridlayout gui helpwithhomework high html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jmf jni jpanel julia keyword linux list loop macintosh map method methods mobile netbeans newbie number object oracle os print problem producer program programming project projectideas read recursion replaysolutions scanner screen server set size sms socket sort sql string swing test threads time transfer transforms tree ui unicode windows





