I am trying to create a program to assign cards a value and then depending which player gets the highest card then they win a point now the area which i cant get to work is the last if statement as it does it after every round not the 7 as needed.
import java.util.*;
public class Card {
public static void main(String[] args) {
int player1= 0;
int player2 = 0;
int i = 1;
while ( i <= 7) {
int player1Card = (int) (Math.random() * 13) + 1;
int player2Card = (int) (Math.random() * 13) + 1;
System.out.println("player 1 = " + player1Card);
System.out.println("player 2 = " + player2Card);
if (player1Card > player2Card) {
System.out.println("Player 1 wins!!!");
player1 = player1 + 1;
} else if (player1Card == player2Card){
System.out.println("It's a bore draw");
player1 = player1 + 0;
player2= player2 + 0;
} else {
System.out.println("Player 2 wins!!!!!");
player2 = player2 + 1;
}
System.out.println("Player 1 points " + player1);
System.out.println("Player 2 points " + player2);
i++;
if (player1 > player2) {
System.out.println("The winner is player 1 with " + player1 + " points");
} else if (player1 == player2) {
System.out.println("Its a draw");
} else {
System.out.println("The winner is Player 2 with " + player2 + " points");
}
}
Your if statement needs to come after the end of the while loop. Move the last } to be before the if statement you want to be executed after all 7 times have run.
Related
for(int i=0; i<5; i++){
if (winnerPlayer <= sumPlayer[i]){
runnerupPlayer = winnerPlayer;
winnerPlayer = sumPlayer[i];
} else if (runnerupPlayer <= sumPlayer[i]){
runnerupPlayer = sumPlayer[i];
}
}
for(int i=0; i<5; i++){
if (winnerPlayer == sumPlayer[i]){
winnerPlayerNumber = i+1;
}
if (runnerupPlayer <= winnerPlayer){
runnerupPlayerNumber = i+1;
}
}
System.out.println("\nWinner: Player " + winnerPlayerNumber + " by winning " + winnerPlayer + " win sets.");
System.out.println("Runner-up: Player " + runnerupPlayerNumber + " by winning " + runnerupPlayer + " win sets.");
For example if player 1 and player 5 have 4 points, the output reads:
Winner: Player 5 by winning 4 win sets.
Runner-up: Player 5 by winning 4 win sets.
I want it to be like this:
Winner: Player 1 by winning 4 win sets.
Runner-up: Player 5 by winning 4 win sets.
How do I call both player 1 and player 5 at the same time in the output?
Here's my full code:
import java.util.Scanner;
public class pingPongTour {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[][] player = new int[5][5];
int[][] playerPoints = new int[5][5];
System.out.println("Welcome to PingPong Tournament Point System!");
System.out.println("\nPlayer 1 vs Player 2");
System.out.print("Player 1 score:");
player[0][1] = in.nextInt();
System.out.print("Player 2 score:");
player[1][0] = in.nextInt();
if (player[0][1] > 0) {
playerPoints[0][1] = 2;
}
else {
playerPoints[1][0] = 2;
}
System.out.println();
System.out.println("Player 3 vs Player 4");
System.out.print("Player 3 score:");
player[2][3] = in.nextInt();
System.out.print("Player 4 score:");
player[3][2] = in.nextInt();
if (player[2][3] > 0) {
playerPoints[2][3] = 2;
}
else {
playerPoints[3][2] = 2;
}
System.out.println();
System.out.println("Player 5 vs Player 1");
System.out.print("Player 5 score:");
player[4][0] = in.nextInt();
System.out.print("Player 1 score:");
player[0][4] = in.nextInt();
if (player[4][0] > 0) {
playerPoints[4][0] = 2;
}
else {
playerPoints[0][4] = 2;
}
System.out.println();
System.out.println("Player 2 vs Player 3");
System.out.print("Player 2 score:");
player[1][2] = in.nextInt();
System.out.print("Player 3 score:");
player[2][1] = in.nextInt();
if (player[1][2] > 0) {
playerPoints[1][2] = 2;
}
else {
playerPoints[2][1] = 2;
}
System.out.println();
System.out.println("Player 4 vs Player 5");
System.out.print("Player 4 score:");
player[3][4] = in.nextInt();
System.out.print("Player 5 score:");
player[4][3] = in.nextInt();
if (player[3][4] > 0) {
playerPoints[3][4] = 2;
}
else {
playerPoints[4][3] = 2;
}
System.out.println();
System.out.println("Player 1 vs Player 3");
System.out.print("Player 1 score:");
player[0][2] = in.nextInt();
System.out.print("Player 3 score:");
player[2][0] = in.nextInt();
if (player[0][2] > 0) {
playerPoints[0][2] = 2;
}
else {
playerPoints[2][0] = 2;
}
System.out.println();
System.out.println("Player 2 vs Player 4");
System.out.print("Player 2 score:");
player[1][3] = in.nextInt();
System.out.print("Player 4 score:");
player[3][1] = in.nextInt();
if (player[1][3] > 0) {
playerPoints[1][3] = 2;
}
else {
playerPoints[3][1] = 2;
}
System.out.println();
System.out.println("Player 5 vs Player 3");
System.out.print("Player 5 score:");
player[4][2] = in.nextInt();
System.out.print("Player 3 score:");
player[2][4] = in.nextInt();
if (player[4][2] > 0) {
playerPoints[4][2] = 2;
}
else {
playerPoints[2][4] = 2;
}
System.out.println();
System.out.println("Player 1 vs Player 4");
System.out.print("Player 1 score:");
player[0][3] = in.nextInt();
System.out.print("Player 4 score:");
player[3][0] = in.nextInt();
if (player[0][3] > 0) {
playerPoints[0][3] = 2;
}
else {
playerPoints[3][0] = 2;
}
System.out.println();
System.out.println("Player 2 vs Player 5");
System.out.print("Player 2 score:");
player[1][4] = in.nextInt();
System.out.print("Player 5 score:");
player[4][1] = in.nextInt();
if (player[1][4] > 0) {
playerPoints[1][4] = 2;
}
else {
playerPoints[4][1] = 2;
}
System.out.println();
int[] sumPlayer = new int[5];
int[] sumPlayerPoints = new int[5];
int winnerPlayer = 0;
int runnerupPlayer = 0;
int winnerPlayerNumber = 0;
int runnerupPlayerNumber = 0;
sumPlayer[0] = (player[0][1] + player[0][2] + player[0][3] + player[0][4]);
sumPlayer[1] = (player[1][0] + player[1][2] + player[1][3] + player[1][4]);
sumPlayer[2] = (player[2][0] + player[2][1] + player[2][3] + player[2][4]);
sumPlayer[3] = (player[3][0] + player[3][1] + player[3][2] + player[3][4]);
sumPlayer[4] = (player[4][0] + player[4][1] + player[4][2] + player[4][3]);
sumPlayerPoints[0] = (playerPoints[0][1] + playerPoints[0][2] + playerPoints[0][3] + playerPoints[0][4]);
sumPlayerPoints[1] = (playerPoints[1][0] + playerPoints[1][2] + playerPoints[1][3] + playerPoints[1][4]);
sumPlayerPoints[2] = (playerPoints[2][0] + playerPoints[2][1] + playerPoints[2][3] + playerPoints[2][4]);
sumPlayerPoints[3] = (playerPoints[3][0] + playerPoints[3][1] + playerPoints[3][2] + playerPoints[3][4]);
sumPlayerPoints[4] = (playerPoints[4][0] + playerPoints[4][1] + playerPoints[4][2] + playerPoints[4][3]);
System.out.println("Set points:-");
System.out.println("Player 1: " + sumPlayer[0]);
System.out.println("Player 2: " + sumPlayer[1]);
System.out.println("Player 3: " + sumPlayer[2]);
System.out.println("Player 4: " + sumPlayer[3]);
System.out.println("Player 5: " + sumPlayer[4]);
System.out.println();
System.out.println("Win points:-");
System.out.println("Player 1: " + sumPlayerPoints[0]);
System.out.println("Player 2: " + sumPlayerPoints[1]);
System.out.println("Player 3: " + sumPlayerPoints[2]);
System.out.println("Player 4: " + sumPlayerPoints[3]);
System.out.println("Player 5: " + sumPlayerPoints[4]);
for (int i = 0; i < 5; i++) {
if (winnerPlayer <= sumPlayer[i]) {
runnerupPlayer = winnerPlayer;
winnerPlayer = sumPlayer[i];
}
else if (runnerupPlayer <= sumPlayer[i]) {
runnerupPlayer = sumPlayer[i];
}
}
for (int i = 0; i < 5; i++) {
if (winnerPlayer == sumPlayer[i]) {
winnerPlayerNumber = i + 1;
}
if (runnerupPlayer <= winnerPlayer) {
runnerupPlayerNumber = i + 1;
}
}
System.out.println("\nWinner: Player " + winnerPlayerNumber + " by winning " + winnerPlayer + " win sets.");
System.out.println("Runner-up: Player " + runnerupPlayerNumber + " by winning " + runnerupPlayer + " win sets.");
}
}
I am trying to end this game properly but the do while loop or something I am doing is causing the game to keep going. If someone has further advice for me to end this game after either score reaches 99 I would appreciate it. I have pasted both classes sources. I am also student so do forgive me.
class
package programming2;
import java.util.Random;
import java.util.Scanner;
public class Matador {
public static void main(String[] args) {
/*The game is played between the player and the computer.
The player's score and the computer's score each begin at 0.
The object of the game is to be the first reach 99 points.
The player and the computer take turns. On each turn: */
Scanner scnr = new Scanner(System.in);
Die matador = new Die();
int playerScore = 0;
int computerScore = 0;
int playerPick = 0;
int cpu;
int turn;
Random randGen = new Random();
cpu = randGen.nextInt(6) + 2;
int cpuPick = 0;
int quit = 0;
System.out.println("Welcome to el Matador!");
System.out.println("The winner of this game is the first to score 99 points");
do {System.out.println("Player 1 please pick your number"); // start of master loop
playerPick = scnr.nextInt();
System.out.println("Player 1 you selected " + playerPick);
System.out.println("Player 2 please pick your number");
cpuPick = cpu;
System.out.println("Player 2 selects " + cpuPick);
//
do { // start of game loop - master do loop player 1
if (computerScore > 98) {
quit = 1;
break;
}
System.out.println("Player 1 rolls dice");
matador.rollDieNow();
System.out.println("Values for 1st dice is: " + matador.getValueDie1() + " and " + matador.getValueDie2());
System.out.println("Combined values are " + matador.getValue());
turn = 0; // start of loop marker for player 1
if (matador.getValueDie1() == 1 && matador.getValueDie2() == 1) {
playerScore = playerScore + -25;
break;
}
if (matador.getValueDie1() == 1 || matador.getValueDie2() == 1) {
playerScore = playerScore + 0;
System.out.println("Changing Turns!");
break;
}
if (matador.getValueDie1() != 1 || matador.getValueDie2() != 1) { // sum of dicevalue added to score
playerScore = playerScore + matador.getValue();
}
if (playerPick == matador.getValueDie1() || playerPick == matador.getValueDie2()) { // if picked number appears on at least 1
turn = turn + 1;
}
if (playerPick == matador.getValueDie1() && playerPick == matador.getValueDie2()) { // if picked number appears on both dice
System.out.println("You are the winner player 1 you matched your numbers!!");
System.out.print("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
quit = 1;
break;
}
System.out.println("Player 1 Score: " + playerScore);
}
while (turn < turn + 1 || quit != 1);
// turn changes and also end of loop marker
System.out.println("Score update! Player 1 score is: " + playerScore + " |-| player 2 score is " + computerScore);
turn = 0; // turn counter resets
do { // start of computer player's turn
if (playerScore > 98) {
quit = 1;
break;
}
System.out.println("Player 2 rolls dice");
matador.rollDieNow();
System.out.println("Values for 1st dice is: " + matador.getValueDie1() + " and " + matador.getValueDie2());
System.out.println("Combined values are " + matador.getValue());
if (matador.getValueDie1() == 1 && matador.getValueDie2() == 1) { // loop marker for cpu starts
computerScore = computerScore + -25;
break;
}
if (matador.getValueDie1() == 1 && matador.getValueDie2() == 1) { // loses turns if 1 appears on both sides
computerScore = computerScore + 0;
System.out.println("Changing Turns!");
break;
}
if (matador.getValueDie1() != 1 || matador.getValueDie2() != 1) { // computer scores value of dice
computerScore = computerScore + matador.getValue();
if (computerScore > 98) {
break;
}
}
if (cpuPick == matador.getValueDie1() || cpuPick == matador.getValueDie2()) {
turn = turn + 1;
}
if (cpuPick == matador.getValueDie1() && cpuPick == matador.getValueDie2()) {
System.out.println("You are the winner player 2 you matched your numbers!!");
System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
quit = 1;
break;
}
System.out.println("Player 2 Score: " + computerScore);
}
while (playerScore < 98 || computerScore <98 || quit != 1);
}
while (playerScore < 98 || computerScore < 98 || quit != 1); // end of master loop
System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
}
}
other class if needed but it has nothing to do with the score as its mainly for determining some values:
package programming2;
import java.util.Random;
import java.util.Scanner;
public class Die {
private int die1; //instance variable
private int die2;
private int rolledDice;
public Die() { //constructor assigns random value
die1 = (int) (Math.random()*6)+1;
die2 = (int) (Math.random()*6)+1;
}
public void rollDieNow () { //rolls dice
Random randGen = new Random();
die1 = randGen.nextInt(6) + 1;
die2 = randGen.nextInt(6) + 1;
}
public int getValue() { // gets dice value
rolledDice = die1 + die2;
return rolledDice;
}
public int getValueDie1() { // gets die1 value
return die1;
}
public int getValueDie2() { // gets die2 value
return die2;
}
}
dice class
package finalproject2;
import java.util.Random;
public class Die {
Random randGen = new Random();
private int value; // instance variable -> stores the roll
// each Die object gets its own copy of the instance variable
public Die() { // constructor rolls dice (calls rollDieNow() method)
rollDieNow();
}
public void rollDieNow() { // rolls dice
value = randGen.nextInt(6) + 1;
}
public int getValue() { // gets dice value
return value;
}
}
Matador Class
package finalproject2;
import java.util.Random;
import java.util.Scanner;
public class Matador {
public static void main(String[] args) {
/*The game is played between the player and the computer.
The player's score and the computer's score each begin at 0.
The object of the game is to be the first reach 99 points.
The player and the computer take turns. On each turn: */
Scanner scnr = new Scanner(System.in);
Die d1 = new Die();
Die d2 = new Die();
int playerScore = 0;
int computerScore = 0;
int playerPick = 0;
int cpu;
int turn;
int sum ;
Random randGen = new Random();
cpu = randGen.nextInt(6) + 2;
int cpuPick = 0;
int quit = 0;
System.out.println("Welcome to el Matador!");
System.out.println("The winner of this game is the first to score 99 points");
// use System.exit(0) to stop program execution
do {
if (playerScore >= 99 || computerScore >= 99) {
System.out.println("Game Over!");
System.exit(0);
}
System.out.println("Player 1 please pick your number"); // start of master loop
playerPick = scnr.nextInt();
System.out.println("Player 1 you selected " + playerPick);
System.out.println("Player 2 please pick your number");
cpuPick = cpu;
System.out.println("Player 2 selects " + cpuPick);
//
do { // start of game loop - master do loop player 1
System.out.println("Player 1 rolls dice");
d1.rollDieNow();
d2.rollDieNow();
sum = d1.getValue() + d2.getValue() ;
System.out.println("Values for both dice are: " + d1.getValue() + " and " + d2.getValue());
System.out.println("Combined values are " + sum);
turn = 0; // start of loop marker for player 1
if (d1.getValue() == 1 && d2.getValue() == 1) {
playerScore = playerScore + -25;
System.out.println("Ouch! Lost 25 points!");
break; // lost of turn, loop ends
}
if (d1.getValue() == 1 || d2.getValue() == 1) {
playerScore = playerScore + 0;
System.out.println("Changing Turns!");
break; // lost of turn loop ends
}
if (d1.getValue() != 1 || d2.getValue() != 1) { // sum of dice value added to score
playerScore = playerScore + (d1.getValue() + d2.getValue());
System.out.println("Gained points! " + (d1.getValue() + d2.getValue()));
}
if (playerPick == d1.getValue() || playerPick == d2.getValue()) { // if picked number appears on at least 1
turn = turn + 1;
}
if (playerPick == d1.getValue() && playerPick == d2.getValue()) { // if picked number appears on both dice
System.out.println("You are the winner player 1 you matched your numbers!!");
System.out.print("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
System.exit(0); // game ends completely
}
System.out.println("Player 1 Score: " + playerScore);
}
while (turn < 1);
// turn changes and also end of loop marker
System.out.println("Score update! Player 1 score is: " + playerScore + " |-| player 2 score is " + computerScore);
turn = 0; // turn counter resets
do { // start of computer player's turn
if (playerScore >= 99 || computerScore >= 99) {
System.out.println("Game Over!");
System.exit(0);
}
System.out.println("Player 2 rolls dice");
d1.rollDieNow();
d2.rollDieNow();
System.out.println("Values for 1st dice is: " + d1.getValue() + " and " + d2.getValue());
System.out.println("Combined values are " + (d1.getValue() + d2.getValue()));
if (d1.getValue() == 1 && d2.getValue() == 1) { // loses points and turn
computerScore = computerScore + -25;
System.out.println("Ouch! Lost 25 points!");
break;
}
if (d1.getValue() == 1 || d2.getValue() == 1) { // loses turns if 1 appears on both sides
computerScore = computerScore + 0;
System.out.println("Changing Turns!");
break;
}
if (d1.getValue() != 1 || d2.getValue() != 1) { // computer scores value of dice
computerScore = computerScore + (d1.getValue() + d2.getValue());
System.out.println("Gained points! " + (d1.getValue() + d2.getValue()));
}
if (cpuPick == d1.getValue() || cpuPick == d2.getValue()) {
turn = turn + 1;
}
if (cpuPick == d1.getValue() && cpuPick == d2.getValue()) {
System.out.println("You are the winner player 2 you matched your numbers!!");
System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
System.exit(0);
}
System.out.println("Player 2 Score: " + computerScore);
}
while (turn < 1); // for some reason on my previous revision this was set to
// a different condition than player 1, now the game ends appropiate
}
while (playerScore <= 99 || computerScore <= 99); // end of master loop
System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
}
}
I'm making a war card game. I'm only supposed to use one class. When I run this it assigns card1 and card2 values properly, runs the loops and prints fine, but it only assigns RNG values once and I need it to break the loop and reassign values each time until someone wins.
I tried breaking it but that only ends the entire code. I'm not looking for an answer, just some input and help. It should play a hand, deduct one point from the loser add to the winner, unless it's a tie. Then it runs War and deducts 3 and adds 3 instead (for the amount of cards used). One hits zero, the game stops and the winner is printed.
import java.util.Random;
public class warGame {
public static void main(String[] args) {
String p1 = "Player 1";
String p2 = "Player 2";
int s = 10;
int p1Score = s;
int p2Score = s;
Random card = new Random();
int card1 = card.nextInt(14);
int card2 = card.nextInt(14);
int t = 1;
int war1 = card.nextInt(14);
int war2 = card.nextInt(14);
System.out.println("Let's play War!");
//Do while loop that runs the entirety of the program inside.
do {
System.out.println("Turn " + t++ + " -- Player 1's card: " + card1 + " Player 2's card: " + card2);
//determines cards, calculates values and runs proper loop
if (card1 > card2) {
System.out.println("Player 1 wins!");
p1Score += 1;
p2Score -= 1;
System.out.println("Scores -- " + "Player 1: " + p1Score + " Player 2: " + p2Score);
} else if (card1 == card2) {
System.out.println("Time for war!");
System.out.println("Player 1's war card is: " + war1 + " Player 2's war card is: " + war2);
if (war1 > war2) {
System.out.println("Player 1 wins the war!");
p1Score += 3;
p2Score -= 3;
} else {
System.out.println("Player 2 wins the war!");
p1Score -= 3;
p2Score += 3;
}
} else {
System.out.println("Player 2 wins!");
p1Score -= 1;
p2Score += 1;
System.out.println("Scores -- " + "Player 1: " + p1Score + " Player 2: " + p2Score);
}
} while (p1Score > 0 && p2Score > 0);
//The following will print out the winner and end the game.
if (p1Score == 0) {
System.out.println("Player 2 wins the game!");
} else {
System.out.println("Player 1 wins the game!");
}
}
}
If you want the loop to have different outcomes, you need to provide it with different values.
When you do:
int card1 = card.nextInt(14);
int card2 = card.nextInt(14);
int war1 = card.nextInt(14);
int war2 = card.nextInt(14);
You're assigning random values to those four variables. However, these values are never changed.
I would replace these lines with:
int card1;
int card2;
int war1;
int war2;
And then:
do {
// update card values
card1 = card.nextInt(14);
card2 = card.nextInt(14);
war1 = card.nextInt(14);
war2 = card.nextInt(14);
System.out.println("Turn " + t++ + " -- Player 1's card: " + card1 + " Player 2's card: " + card2);
This time the variables' values will be different on each iteration.
Simple answer actually:
You assign the values of the cards outside your do-while loop. That means those values will never change unless you change them inside the loop.
import java.util.Random;
import java.util.Stack;
import java.util.Scanner;
public class Blackjack {
public static void main(String[] args) {
int cardValue; /* card value is from 2 to 11 */
int MaxCard = 50;
Stack < Integer > Player1 = new Stack < Integer > ();
Stack < Integer > Addition = new Stack < Integer > ();
Stack < Integer > Dealer = new Stack < Integer > ();
Scanner keyboard = new Scanner(System.in);
String input;
Random r = new Random();
System.out.println("Welcome to Mitchell's blackjack program!");
for (int a = 1; a <= 2; a++) { // Start's the game by assigning 2 cards each, to the players
int Player1draw = 2 + r.nextInt(11);
int Dealerdraw = 2 + r.nextInt(11);
//System.out.print("\nPushing in " + Player1draw + " to Player 1."); - used for checking
Player1.push(Player1draw);
//System.out.print("\nPushing in " + Dealerdraw + " to Dealer."); - used for checking
Dealer.push(Dealerdraw);
}
Integer pop1 = (int) Player1.pop();
System.out.print("\nYou get a " + pop1);
Integer pop2 = (int) Player1.pop();
System.out.print(" and " + pop2);
int sum = pop1 + pop2;
System.out.print("\nYour total is " + sum);
if (sum > 21) {
System.out.print("\nYou LOST! ");
System.exit(0);
}
Integer pop3 = (int) Dealer.pop();
System.out.print("\nThe dealer has a " + pop3 + " showing, and a hidden card.\n");
Integer pop4 = (int) Dealer.pop();
System.out.print("\nHis total is hidden, too.\n");
for (int i = 0; i < MaxCard; i++) {
System.out.print("\nWould you like to \"hit\" or \"stay\"? ");
input = keyboard.next();
if (input.equals("hit")) {
int draw2 = 2 + r.nextInt(11); // draw -> 'hit' for another card
int sum3 = sum + draw2;
System.out.print("\nYou drew a " + draw2);
System.out.print("\nYour total is " + (sum3) + ".");
if ((sum + draw2) > 21) {
System.out.print("\nThe Dealer WON and you LOST!");
System.exit(0);
}
} else if (input.equals("stay")) {
System.out.print("\nOkay, dealer's turn.");
System.out.print("\nHis hidden card was a " + pop4);
int sum2 = pop3 + pop4;
System.out.print("\nHis total was " + sum2);
if (sum2 > 21) {
System.out.print("\nThe Dealer LOST and you WON!");
System.exit(0);
}
}
}
}
}
So when the code runs, what it does is that it will draw 2 cards for Player1 (both of the card is shown) and the dealer(1 of the card is shown). It will then display the total number of points player1 has. So after i press hit, to draw another card, it calculates the total value just fine because it sums up the current drawn card value into the previous 2 card.
Now the problem is, how can I code it so whenever I type the 'hit' button again it will than add up the previous 3 cards and the newly drawn card?
int sum = 0;
for(Integer card : Player1)
sum += card;
if(sum > 21) system.err.println("Over 21; you lost");
Looking for something like that... ?
I am new to Java and was trying to learn by doing some excercises that I found online. So please excuse me if this is too naive.
This excercise was about writing a program for game of craps with the following rules:
In the game of craps, a pass line bet proceeds as follows: Two six-sided dice are
rolled; the first roll of the dice in a craps round is called the “come out roll.”
A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12
automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number
becomes “the point.” The player keeps rolling the dice until either 7 or the point is
rolled. If the point is rolled first, then the player wins the bet. If a 7 is rolled first,
then the player loses.
Write a program that simulates a game of craps using these rules without human
input. Instead of asking for a wager, the program should calculate whether the
player would win or lose. The program should simulate rolling the two dice and
calculate the sum. Add a loop so that the program plays 10,000 games. Add
c ounters that count how many times the player wins, and how many times the
player loses. At the end of the 10,000 games, compute the probability of winning
[i.e., Wins / (Wins + Losses)] and output this value. Over the long run, who
is going to win the most games, you or the house?
Here is the code that I have written :
// GAME OF CRAPS
public static void main (String[] args)
{
int dice1 = 0;
int dice2 = 0;
int scorenew = 0;
int point = 0;
int wins = 0;
int loss = 0;
for (int i = 0; i < 10000; i++)
{
System.out.println ("roll the dices");
int score = roll (dice1, dice2);
System.out.println ("\n score " + score);
if (score == 11 || score == 7)
{
System.out.println ("\n Score = " + score);
System.out.println ("you win");
wins = wins + 1;
}
if (score == 2 || score == 3 || score == 12)
{
System.out.println ("\n Score = " + score);
System.out.println ("you lose");
loss = loss + 1;
}
else if (score == 4 || score == 5 || score == 6 || score == 8 || score == 9 || score == 10)
{
point = point + score;
System.out.println ("\n Point = " + point);
do
{
scorenew = roll (dice1, dice2);
System.out.println ("\n Score new = " + scorenew);
if (scorenew == point)
{
System.out.println ("\n you win");
wins = wins + 1;
point = 0;
break;
}
if (scorenew == 7)
{
System.out.println ("\n you lose");
point = 0;
loss = loss + 1;
break;
}
} while (scorenew != point || scorenew != 7);
}
}
System.out.println ("\n number of wins = " + wins
+ " and number of loss = " + loss +
" and the probability for winning a game = " + (double) wins / (wins + loss));
}
public static int roll (int d1, int d2)
{
Random randomGenerator = new Random ();
int dice1 = randomGenerator.nextInt (6) + 1;
int dice2 = randomGenerator.nextInt (6) + 1;
System.out.println ("\n dice1 = " + dice1 + " dice2 = " + dice2);
int score = dice1 + dice2;
return score;
}
Everytime I run the code the do-while condition gets executed first, so please can anyone help me figure out where I am going wrong?
do {
} while(scorenew!=point || scorenew != 7);
This condition is always true, so you have an infinite loop. Also, why do you pass d1 and d2 into the roll() function? They are completely unused and unneeded.
Do while is doing exactly what it you should expect it to do. Executes the body first then evaluates the conditional to see if it should run again. You don't actually need a do while though, you want to run until one of the conditions breaks you out of the while loop.
else {
point = score;
System.out.println ("\n Point = " + point);
while (true) {
scorenew = roll (dice1, dice2);
System.out.println ("\n Score new = " + scorenew);
if (scorenew == point) {
System.out.println ("\n you win");
wins = wins + 1;
break;
}
if (scorenew == 7) {
System.out.println ("\n you lose");
loss = loss + 1;
break;
}
}
}
Like #Lee Daniel Crocker said you don't need to pass in dice1 and dice2 to the roll function.
public static int roll() {
Random randomGenerator = new Random();
int dice1 = randomGenerator.nextInt(6) + 1;
int dice2 = randomGenerator.nextInt(6) + 1;
System.out.println("\n dice1 = " + dice1 + " dice2 = " + dice2);
return dice1 + dice2;
}
Another thing that might help is not declaring all the variables at the top of your method. You don't need scorenew or point outside of the third condition, in fact you don't need scorenew at all since you have point:
public static void main(String[] args) throws java.lang.Exception {
int wins = 0;
int loss = 0;
for (int i = 0; i < 10000; i++) {
System.out.println("roll the dices");
int score = roll();
System.out.println("\n score " + score);
if (score == 7 || score == 11) {
System.out.println("\n Score = " + score);
System.out.println("you win");
wins = wins + 1;
} else if (score == 2 || score == 3 || score == 12) {
System.out.println("\n Score = " + score);
System.out.println("you lose");
loss = loss + 1;
} else {
int point = score;
System.out.println("\n Point = " + point);
while (true) {
score = roll();
System.out.println("\n Score new = " + score);
if (score == point) {
System.out.println("\n you win");
wins = wins + 1;
break;
}
if (score == 7) {
System.out.println("\n you lose");
loss = loss + 1;
break;
}
}
}
}
System.out.println("\n number of wins = " + wins
+ " and number of loss = " + loss +
" and the probability for winning a game = " + (double) wins / (wins + loss));
}
public static int roll() {
...
}