I am supposed to be making a game of craps in java, but I have a problem. You see, technically the game is finished, but it will not say if you loose the game or not even if you do. It will only continue to roll til you win. I tried a couple workarounds but it only seems to trap it in an infinite loop. Here is my code as follows:
/* Class: CS1301
* Section: 8
* Term: Fall 2015
* Name: Matthew Woolridge
* Instructor: Mr. Robert Thorsen
* Assignment: Assignment 8
* Program: 4
* ProgramName: CrapsGame
* Purpose: Allows the user to play a game of craps
* Operation: The information is statically instantiated in the code and
* the data is output to the screen.
* Input(s): The input is the next roll
* Output(s): The output is correct or incorrect and points
* Methodology: The program uses booleans, integers, loops and methods to play a game of craps
*
*/
import java.util.*;
public class CrapsGame
{
public static void main (String[]args)
{
/******************************************************************************
* Declarations Section *
******************************************************************************/
/****************************CONSTANTS********************************/
String restart = "y";
Scanner scan = new Scanner(System.in);
int sum=rollDice();
int points = points(sum);
boolean youWin=youWin(sum, points);
/******************************************************************************
* Inputs Section *
******************************************************************************/
/****************************variables********************************/
//********************Calculations in the processing*****************//
/******************************************************************************
* Processing Section *
******************************************************************************/
while(restart.equals("y")){
youWin=false;
while(youWin==false){
rollDice();
sum=rollDice();
points=points(sum);
youWin=youWin(sum, points);
}
/******************************************************************************
* Outputs Section *
******************************************************************************/
System.out.print("\nWould you like to play again? y or n: ");
restart = scan.next();
}
System.out.print("The program has ended!");
}
public static int rollDice()
{
int num1= (int)(6.0*Math.random() + 1.0); //first die
int num2= (int)(6.0*Math.random() + 1.0); //second die
int sum= num1 + num2; //sum of roll
System.out.printf("\nYou have rolled %d + %d = %d\n", num1, num2, sum); //Prints the sum
return sum;
}
/*
* ProgramName: points
* Purpose: calculates points
* Operation: The information is statically instantiated in the code and
* the data is output to the screen.
* Input(s): none
* Output(s): The output is the points
* Methodology: The program uses if statements to calculate points
*
*/
public static int points(int sum)
{
int points=0;
if (sum>=4 && sum<=6) { //Counts points based on your rolls
points = points + 1;
System.out.print("Your points are: " + points);
}
else if (sum>=8 && sum<=10){
points = points + 1;
System.out.print("Your points are: " + points);
}
return points;
}
/*
* ProgramName: youWin
* Purpose: Determines if the user won or not
* Operation: The information is statically instantiated in the code and
* the data is output to the screen.
* Input(s): There is no input from the user
* Output(s): The output is correct or incorrect and points
* Methodology: The program uses booleans, integers, and if statements to determine if the user won or not
*
*/
public static boolean youWin(int sum, int points)
{
boolean youWin=false;
if (sum==2 || sum==3 || sum == 12) {
youWin=false;
System.out.print("You lost with a " + sum); //Determines if you win or loose based on the sum and points and returns the youWin boolean
}
else if (sum==7 || sum==11) {
youWin=true;
System.out.print("You won with a " + sum);
}
else if (points==7){
youWin=true;
}
return youWin;
}
}
Aucun commentaire:
Enregistrer un commentaire