Bob turner Bob turner - 9 months ago 21
Java Question

Choosing Between 2 games in one code

Ok I am doing a project to make a user login, then ask the user to select which game to play, then write the code for the game. I have done everything apart from being able to choose to between the 2 games. Any tips on how to do this would greatly help!

import java.io.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.io.InputStreamReader;


public class SkillsDemo31 {
private static boolean again = true;
private static int action;


public static void main(String[] args) throws IOException {

//***************************
//Login
//***************************

class User {
User (String username, String password)
{
this.username = username;
this.password = password;
}

String GetUsername() {return username;}
String GetPassword() {return password;}

private String username;
private String password;
}


String greeting = "Hello";
String username;
String password;

// Used to hold the instance of a user who successfully logged in
User loggedInUser = null;

// Create an empty list to hold users
List<User> listOfUsers = new ArrayList<>();

// Add 3 users to the list
listOfUsers.add(new User("Gerry","spintown"));
listOfUsers.add(new User("Evelyn","poker"));
listOfUsers.add(new User("Joan","bonus"));


BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));


System.out.println("*** Welcome to the program ***\n");
System.out.println(greeting);

System.out.println("Please type your username :");
username = br.readLine();
System.out.println("Please type your password :");
password = br.readLine();

for (User user : listOfUsers)
{
if (user.GetUsername().equals(username))
{
if (user.GetPassword().equals(password))
{
loggedInUser = user;
// when a user is found, "break" stops iterating through the list
break;
}
}
}

// if loggedInUser was changed from null, it was successful
if (loggedInUser != null)
{
System.out.println("User successfully logged in: "+loggedInUser.GetUsername());
}
else
{
System.out.println("Invalid username/password combination");
}

//**********************************
//Choice of Games
//**********************************
again = true;
action = 0;

while (again)
{
System.out.println("Please type 1 for Rock, Paper, Scissors or 2 for Play pick up sticks:");
action = Integer.parseInt(br.readLine());
if (action == 1)
{
System.out.println("\nYou have chosen to play Rock, Paper, Scissors");
}
else if (action == 2)
{
System.out.println("\nYou have chosen to Play pick up sticks");
again = false;
}


//******************************
//Rock,Paper,Scissors
//********************************




Random rnd = new Random();
int input;
int score = 0;
int B = 1;


System.out.println("Pick 1,2, or 3 for:");
System.out.println("Rock (1), Paper(2), or Scissors (3)");
while (B != 0) {
// 1 = rock
// 2 = paper
// 3 = scissors
// N= Integer.parseInt(br.readLine())
int Rock = 1, Paper = 2, Scissor = 3;

input = Integer.parseInt(br.readLine());
int randomNumber = rnd.nextInt(3 - 1 + 1) + 1;
if (randomNumber == Rock) {
if (input == Rock) {
System.out.println("Rock Vs. Rock: Tie");
} else if (input == Paper) {
System.out.println("Paper Vs. Rock: You Win!");
System.out.println("Congratulations!");
score++;
} else if (input == Scissor) {
System.out.println("Scissors Vs. Rock: You Lose");
}
} else if (randomNumber == Paper) {
if (input == Rock) {
System.out.println("Rock Vs. Paper: You Loose");
} else if (input == Paper) {
System.out.println("Paper Vs. Paper: Tie");
} else if (input == Scissor) {
System.out.println("Scissors Vs. Paper: You Win");
System.out.println("Congratulations!");
score++;
}

} else if (randomNumber == Scissor) {
if (input == Rock) {
System.out.println("Rock Vs. Scissors: You Win");
System.out.println("Congratulations!");
score++;
} else if (input == Paper) {
System.out.println("Paper Vs. Scissors: You Loose");
} else if (input == Scissor) {
System.out.println("Scissors Vs. Scissors: Tie");
}

}

int Y=5, N=10;
System.out.println("Your score is "+ score);
System.out.println("Do you want to play again? Press(5) For Yes/(10) For No");
input = Integer.parseInt(br.readLine());
if(input==Y){
B=1;
System.out.println("Rock, Paper,Scissors");
}
else if(input==N)
{System.exit(0);
System.out.println("Have A Good Day!");
}

//***********************
//Pick Up Sticks
//***********************



Scanner scanner = new Scanner(System.in);

while (true) {

int numSticks = 21;
System.out.println("Would You Like to go first? (Yes/No)");
Scanner input1 = new Scanner(System.in);
String goFirst = input1.nextLine();
Scanner take = new Scanner (System.in);
int numToTake = 0;
int score2 = 0;




while (numSticks > 0) {


if (goFirst.equals("Yes") || goFirst.equals("yes")) {
System.out.println("There are " + numSticks + " sticks ");
System.out.println("How many sticks do you want to take (1 or 2)");
numToTake = take.nextInt();

if (numToTake > 2) {
numToTake = 2;

}

else if (numToTake < 1) {
numToTake = 1;
}
numSticks = numSticks - numToTake;

if (numSticks <= 0) {
System.out.println("You lose");
System.out.println("Your score is " + score );
}
else {

if((numSticks - 2) % 3 == 0 || numSticks - 2 == 0) {
numToTake = 1;
}
else {
numToTake = 2;
}
System.out.println("Computer takes " + numToTake + " sticks " );
numSticks = numSticks - numToTake;

if (numSticks <= 0) {
System.out.println(" You win ");
score++;
System.out.println("Your score is " + score );
}
}

}
else {
if((numSticks - 2) % 3 == 0 || numSticks - 2 == 0) {
numToTake = 1;
}
else {
numToTake = 2;
}
System.out.println("Computer takes " + numToTake + " sticks " );
numSticks = numSticks - numToTake;

if (numSticks <= 0) {
System.out.println("You win");
score++;
System.out.println("Your score is " + score );
}
else {
System.out.println("There are " + numSticks + " sticks ");
System.out.println("How many sticks do you want to take (1 or 2)");
numToTake = take.nextInt();

if (numToTake > 2) {
numToTake = 2;
}
else if (numToTake < 1){
numToTake = 1;
}
numSticks = numSticks - numToTake;

if(numSticks <0){
System.out.println("You win");
score++;


}




}

}



}


System.out.println("Do you want to play again, type (5) for yes or (10) for no");

if (scanner.nextLine().equals("10")) {
break;
}
}
}
}
}


}

Answer Source

You should create for every game a class and create a start method there to start the game based on your user input for example:

            System.out.println("Please type 1 for game 1 or 2 for game2:");
            action = Integer.parseInt(br.readLine());
            if (action == 1)
            {
                System.out.println("\nYou have chosen to game 1");
                Game1 game1 = new Game1();
                game1.start();
            }
            else if (action == 2)
            {
                System.out.println("\nYou have chosen game 2");
                Game2 game2 = new Game2();
                game2.start();
            }

This code is based on your code.