Russian roulette

13th October 2018

This is the Java code for a basic Russian roulette game. At the start of the game, you can decide whether you want to spin on each turn or just spin at the start. Two numbers between 1 and 6 are randomly generated. One to determine which chamber the bullet goes in, and another to determine which chamber the bullet is in line to be shot out of. The two methods called within the main method, spinOnce() and spinEachTurn(), handle the different game modes.

import java.util.Scanner;

public class RussianRoulette {

    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.println("Would you like to spin once or on each turn?");
        System.out.print("Press 1 for once, 2 for each turn: ");

        while(true) {
            String decision = sc.nextLine();
            if(decision.equals("1")) {
                spinOnce();
                break;
            } else if(decision.equals("2")) {
                spinEachTurn();
                break;
            }
        }
    }

    public static void spinOnce() {

        int chamber = (int)(Math.random() * (6 - 1 + 1)) + 1;
        int gun = (int)(Math.random() * (6 - 1 + 1)) + 1;

        while(true) {
            while(true) {
                System.out.print("Press 's' to shoot: ");
                String shoot = sc.nextLine();
                if(shoot.equals("s")) {
                    System.out.println("Shot fired");
                    break;
                }
            }

            if(chamber == gun) {
                System.out.println("You died");
                break;
            } else {
                System.out.println("You survived");
                gun = gun + 1;
                if(gun == 7) {
                    gun = 1;
                }
            }

            System.out.println("CPU just shot");
            if(chamber == gun) {
                System.out.println("CPU died, you win");
                break;
            } else {
                System.out.println("CPU survived");
                gun = gun + 1;
                if(gun == 7) {
                    gun = 1;
                }
            }
        }
    }

    public static void spinEachTurn() {

        int chamber = (int)(Math.random() * (6 - 1 + 1)) + 1;
        int gun = (int)(Math.random() * (6 - 1 + 1)) + 1;

        while(true) {
            while(true) {
                System.out.print("Press 's' to shoot: ");
                String shoot = sc.nextLine();
                if(shoot.equals("s")) {
                    System.out.println("Shot fired");
                    break;
                }
            }

            if(chamber == gun) {
                System.out.println("You died");
                break;
            } else {
                System.out.println("You survived");
                gun = (int)(Math.random() * (6 - 1 + 1)) + 1;
            }

            System.out.println("CPU just shot");
            if(chamber == gun) {
                System.out.println("CPU died, you win");
                break;
            } else {
                System.out.println("CPU survived");
                gun = (int)(Math.random() * (6 - 1 + 1)) + 1;
            }
        }
    }
}