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?
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;
}
}