-1

So I want to create a menu where a user can choose to play two different games. I want to create a main method and be able to make a for loop or switch statements for the different game options or for the user to quit but I am not sure how I would call the classes so that the game runs when they choose it.

Can someone explain to me how I would go about this. Thanks!

import java.util.Scanner;
import java.util.Random;

public class RpsGame {

    /* Valid user input: rock, paper, scissors */

    public static void main(String[] args) {

      System.out.print("Please Make Your Choice (Rock, Paper or Scissors): ");

            try {
                Scanner sc = 
                new Scanner(System.in);

                String userInput =   
                sc.next();                      
                if (isValid( userInput )) {
                    game( userInput );

                } else {
                   print("Invalid user input!\nWrite rock, paper or scissors!");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    public static void print(String text) {
       System.out.println( text );
    }

    public static boolean isValid(String input) {

        if (input.equalsIgnoreCase("rock")) {
           return true; 
        } 

        if (input.equalsIgnoreCase("paper")) {
           return true; 
        }

         if (input.equalsIgnoreCase("scissors")) {                             
             return true; 
         }

        return false;
    }

    public static void game(String user) {
        String computer = computerResults();
        //System.out.print("Please Make Your Choice: ");

        print( user + " vs " + computer + "\n");

        if (user.equalsIgnoreCase(computer)) {
            print("Oh, Snap! Tied - No winners.");
        } else {

            if (checkWin(user, computer)) {
               print("You won against the computer!");
            } else {
               print("You lost against the computer!");
            }
        }
    }

    public static String computerResults() {

        String types[] = 
        {"rock", "paper", "scissors"};

        Random rand = new Random(); 
        int computerChoice = rand.nextInt(3);;

        return types[computerChoice];
    }

    public static boolean checkWin(String user, String opponent) {

        if ( (!isValid( user )) && (!isValid( opponent )) ) {
            return false;
        }

        String rock = "rock", paper = "paper", scissors = "scissors";

        if ( (user.equalsIgnoreCase( rock )) && (opponent.equalsIgnoreCase( scissors )) ) {
           return true; 
        }

         if ( (user.equalsIgnoreCase( scissors)) && (opponent.equalsIgnoreCase( paper )) ) {
           return true; 
        }

         if ( (user.equalsIgnoreCase( paper )) && (opponent.equalsIgnoreCase( rock  )) ) {
           return true; 
        }

        return false; 
        //If no possible win, assume loss.
    }
}
aman_novice
  • 1,027
  • 3
  • 13
  • 31
  • 3
    What do you mean by "call the classes"? You can create an instance of a class, call methods, etc. The structure of how to do that is covered by any Java tutorial. There are examples of it in the code you already have. Can you clarify what you've tried and what isn't working? – David Nov 22 '16 at 17:48
  • Yea. So, I'm going to create new file so that it is my main method. In it, I will have the menu but I'm not sure how i would call the methods so that I can make the game run. I haven't written any code yet, I'm just trying to figure out how I would go about this. – brownwatchman Nov 22 '16 at 17:52
  • Then I guess start with an introductory Java tutorial. Creating instances of classes and calling methods is going to be covered by *any* tutorial. `MyClass x = new MyClass();` and `x.someMethod();` would be a simplified example. – David Nov 22 '16 at 17:54
  • Help me understand. So RPS is one game and there is another game that you want to run? and either of these games have to be run from a menu? – aman_novice Nov 22 '16 at 17:55
  • "I'm just trying to figure out how I would go about this." => Sorry to say this, but this is very basic stuff which - as David said - is covered by many tutorials. Have a look at the [Java Tutorials Learning Paths](http://docs.oracle.com/javase/tutorial/tutorialLearningPaths.html) and start with the "New To Java" section. Having said this, such a question is not a good fit for SO. It is simply too broad. – Seelenvirtuose Nov 22 '16 at 17:55
  • 2
    The fact that all of your methods are static implies that you aren't really comfortable and familiar with working with objects and instances. I would recommend reading up on that subject. Currently, as all your methods are static you can just call them by writing `ClassName.methodName()` for example `RpsGame.print("Hello");` would print 'Hello' – OH GOD SPIDERS Nov 22 '16 at 17:56
  • @aman_novice Yes. So I have an assignment due and the requirement is that we have to have 3 files. Two are supposed to be classes and one is where the main method will go. So, i was thinking of making a game for each class, and in the third file where the main method and menu is I would call those games from the classes – brownwatchman Nov 22 '16 at 18:35

2 Answers2

0

The easiest method that I am familiar with is using something called a Driver Class. A Driver Class is a class that is designed to run code from other classes - perfect for running two different games. Check this post if you need more info: What is a driver class? (Java)

Community
  • 1
  • 1
Time Rift
  • 17
  • 6
0

Try something like this:

public class MyGameApp {
    public static final String OPTION_1 = "1"; 
    public static final String OPTION_2 = "2";
    public static final String OPTION_EXIT = "3";

    public static void main(String... args) {
         Scanner sc = new Scanner(System.in);

         String userChoice = null;
         do {
             System.out.println("Choose an option: \n 1. Game 1\n2. Game 2\n3. Exit");

             userChoice = sc.nextLine();

             switch(userChoice) {
                 case OPTION_1:
                     /*
                       Calls a static method of a class, so there is no need of instantiate the class first. 
                      */
                     GameOne.start();
                     break;
                 case OPTION_2:
                     /*
                      In this case, create a new instance of the class GameTwo, and then call the method start().
                      */
                     GameTwo game = new GameTwo();
                     game.start();
                     break;
                 default:
                     System.out.println("Wrong option, try again.");
             }
         while(!OPTION_EXIT.equals(userChoice));

    }
}

class GameOne {
    public static void start() { ... }
}

class GameTwo {
    public void start() { ... }
}
  • Thanks! This is almost of how I was thinking of doing it...The only thing I'm stuck on, in this case, on case option 1, how would I call the game that pasted above? would it be RpsGame.game();? That's the part I am stuck on – brownwatchman Nov 22 '16 at 19:08
  • Considering that the RpsGame would be one of your games, I would suggest you to not put the main method inside of it. So later on, you can have a static method that would start the RpsGame, and when the game is finish, it could get back to the main menu. – E. Nikolas de Oliveira Nov 22 '16 at 19:13
  • Dammit, hit "enter" by mistake... So, in your case, if you just rename the main method for something like "public static void start() { Scanner sc ... }", then yes, in my case would just replace the two lines "GameOne game = new GameOne(); game.start();" with "RpsGame.start()", and this would start the rock, paper, scissors games. – E. Nikolas de Oliveira Nov 22 '16 at 19:15
  • Thank you so much. I re named my main method in the game and I got it to work! thanks! – brownwatchman Nov 22 '16 at 19:44
  • Cheers mate! Could you mark my answer as the one who solved your issue then, please? – E. Nikolas de Oliveira Nov 22 '16 at 20:12