Java Craps Simulation: Logic and Implementation
Java Craps Game Implementation
This document outlines the logic for a dice-based game simulation written in Java.
Game Logic Class
public class Game {
// Function to determine if the player wins or loses
public boolean gano_la_apuesta(int a, int b) {
int val = a + b;
// Check initial roll conditions
if (val == 7 || val == 11 || val == 2 || val == 12 || val == 3) {
if (val == 7 || val == 11) {
return true;
}
if (val == 2 || val == 12 || val == 3) {
return false;
}
}
// If no initial condition is met, enter the point phase
int val2;
int validator = 0;
do {
a = new java.util.Random().nextInt(6) + 1;
b = new java.util.Random().nextInt(6) + 1;
val2 = a + b;
if (val == val2 || val2 == 7) {
validator = 1;
}
} while (validator == 0);
if (val == val2) return true; // Win condition
return false; // Loss condition
}
// Function to simulate multiple games
public int crear_juego(int c) {
int cont = 0;
for (int i = 0; i < c; i++) {
int number = new java.util.Random().nextInt(6) + 1;
int number2 = new java.util.Random().nextInt(6) + 1;
if (gano_la_apuesta(number, number2)) {
cont++;
}
}
return cont;
}
// Main execution method
public static void main(String[] args) {
int games;
System.out.println("Enter number of games to play:");
games = new java.util.Scanner(System.in).nextInt();
System.out.println("The number of games won is: " + new Game().crear_juego(games));
System.out.println("Press any key to continue");
new java.util.Scanner(System.in).next();
}
}