1
\$\begingroup\$

First of all, I won't include the spin game there, cause it's very simple.

I went a bit deeper into object oriented since the last time, I learned array lists, using them with objects, maphashes, and more.

They are pretty useful I must say!

Let's begin:

Mains.java:

public class Mains {

    public static void main (String[] args) {
        //Start the game
        startGame();

    }

    private static void startGame() {

        //Declares
        GameHandler handler = new GameHandler();
        Scanner console     = new Scanner(System.in);   
        boolean game        = true;
        String input        = "";  

        //Print program welcome text
        handler.printStart();

        //While in game...
        while (game) {
            //Getting input ready for new commands from the player
            input = console.nextLine();

            //Checking if input was set.
            if (input != null) {
                //Selecting the game you want to play.
                handler.selectGame(input);

                //If game was selected.. then.. let's start playing.
                while (handler.inGame) {
                    //Use will say something.
                    input = console.nextLine();

                    //If it was "exit", it will go back and select another game.
                    if (input.equals("exit")) {
                        handler.exitGame();
                    } else {
                        //Play again.
                        handler.continueGame(input);
                    }
                }
            }
        }

    }
}

GameHandler.java:

public class GameHandler {

    private String[] games              = {"Spin", "Tof"};
    private Spin spin                   = new Spin();
    private Tof tof                     = new Tof();
    private boolean spinGame            = false;
    public static boolean tofGame       = false;
    public boolean inGame               = false;
    public int myPoints                 = 0;

    /**
    * Method printStart
    *
    * Will welcome the player to the program.
    */
    public void printStart() {

        this.print(0, "Welcome to the program!");
        this.print(0, "Please select a game: " + this.availableGames());

    }

    /**
    * Method available games
    *
    * This will print all the games that are located in the games array in one row.
    **/

    private String availableGames() {
        String names = "";
        for (int i = 0; i < games.length; i++) {
            names = (names + games[i]);
            if (i < games.length -1) {
                names = (names + ", ");
            }
        }

        return names;
    }

    /**
    * Method selectGame
    *
    * This will select the given game.
    * @param command The entered command.
    **/

    public void selectGame(String command) {
        if (this.inArray(command))
        {
            if (command.equalsIgnoreCase("spin")) {
                this.startGame("spin");
            } else if (command.equalsIgnoreCase("tof")) {
                this.startGame("tof");
            }
        } else {
            this.print(0, "Could not find game!");
        }
    }

    /**
    * Method inArray
    *
    * This will check if the entered game name is exisiting in the games array.
    * If yes, will return a boolean true, else false.
    *
    * @param value The entered game name.
    * @return boolean true/false.
    **/

    private boolean inArray(String value) {
        boolean exists = false;
        for (String s : games) {
            if (value.equalsIgnoreCase(s)) {
                exists = true;
            }
        }

        return exists;
    }

    /**
    * Method startGame
    *
    * Will start the game, and print instructions.
    * will set the game boolean to true.
    **/

    private void startGame(String game) {
        switch (game) {
            case "spin":
                this.print(0, "Welcome to spin game!");
                this.print(0, "Please click on any key to spin!");
                spinGame = true;
            break;
            case "tof":
                this.print(0, "Welcome to the ToF game!");
                this.print(0, "Please answer the questions.");
                System.out.println("");
                System.out.println("===============");
                System.out.println("QUESTION");
                System.out.println("===============");
                System.out.println("");     
                this.generateQuestion();
                tofGame = true;
            break;
        }

        inGame = true;
    }

    private void generateQuestion() {
        System.out.println(tof.selectQuestion());
    }

    /**
    * Method continueGame
    *
    * Will continue the game, either spin again, or print new question or even answer.
    * @param command The entered command.
    **/
    public void continueGame(String command) {
        while (inGame) {
            if (spinGame) {
                this.spinWheel();
                // Break out of the loop.
                break;
            }
            if (tofGame) {
                this.checkAnswer(command);
                break;
            }               
        }
    }


    public void checkAnswer(String command) {
        if (tof.checkQuestion(command)) {
            tof.myPoints++;
            System.out.println("<------------->");
            System.out.println("Nice job!");
            System.out.println("You received 1 point, you now have " + tof.myPoints + " Points");
            System.out.println("<------------->");              
            System.out.println("");
            System.out.println("===============");
            System.out.println("NEXT QUESTION");
            System.out.println("===============");
            System.out.println("");             
            this.generateQuestion();
        }
        else {
            System.out.println("");
            System.out.println("Your answer was incorrect.");
            System.out.println("");
            System.out.println("===============");
            System.out.println("NEXT QUESTION");
            System.out.println("===============");
            System.out.println("");
            this.generateQuestion();
        }
    }

    /**
    * Method exitGame
    *
    * Exit the game..
    **/

    public void exitGame() {
        System.out.println("");
        System.out.println("");
        spinGame = false;
        tofGame = false;
        inGame = false;
        this.printStart();
        tof.selectQuestion();
    }

    /**
    * Method spinWheel
    *
    * This will spin the wheel.
    **/

    private void spinWheel() {
        this.print(0, spin.spinWheel());
    }

    /**
    * Method print
    *
    * Prints text using System.out
    * @param type printing type (Println/print).
    * @param message The message
    **/

    private void print(int type, String message) {
        switch (type) {
            case 0:
                System.out.println(message);
            break;
            case 1:
                System.out.print(message);
            break;              
        }
    }
}

Tof.java:

public class Tof {

    private Random r = new Random();
    private ArrayList<QnA> questionArray = new ArrayList<QnA>();
    public int question;
    public int myPoints = 0;

    public Tof() {
        questionArray.add(new QnA("Is daniel big? ", "true"));
        questionArray.add(new QnA("Does Daniel sucking at linux?", "false"));
        questionArray.add(new QnA("Daniel is pink? ", "true"));
        questionArray.add(new QnA("Daniel is funny? ", "false"));
    }

    public String selectQuestion() {
        if (questionArray.size() > 0) {
            int index = r.nextInt(questionArray.size());
            this.question = index;
            return questionArray.get(index).getQuestion();
        }
        else {
            this.noQuestions();
            return "Please type exit and re-enter the game.";
        }

    }

    public boolean checkQuestion(String answer) {
        boolean returning = false;
        if (questionArray.get(this.question).getAnswer().equalsIgnoreCase(answer)) {
            returning = true;
        }
        questionArray.remove(this.question);
        return returning;
    }

    private void noQuestions() {
        if (GameHandler.tofGame) {
            System.out.println("");
            System.out.println("Oh boy! It looks like you've ran out of questions.");
            System.out.println("blabla");
            System.out.println("");
            System.out.println("You could gain total of " + this.myPoints + " Points.");
            this.myPoints = 0;
        }
        GameHandler.tofGame = false;
    }
} 

and last one, QnA class:

public class QnA {

    private String question     = ""; 
    private String answer   = "";

    public QnA(String q, String a) {
        this.question = q;
        this.answer = a;
    }

    public String getQuestion() {
        return question;
    }

    public String getAnswer() {
        return answer;
    }  
}

I want to focus on my Object-oriented structures, how I handle the code, etc. Please tell me what's wrong and useless code there (Besides the plant printlns hehe).

Thanks!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

If you are focussing on OO it would be good to put your games in a separate class. For OO perspective you should check which are the different entities in your application and represent them in a class, defining that each entity can do and the relations to other entity.

You might want to to have a parent game class which defines standard behaviour of a game such as starting, stopping, requested game name, loading your game dependencies, ... and an extention / implementation of that general game class to a specific game.

Make your handler / controller use the generic game interface as it only needs to access the standard behaviour of a game and not it's specific implementation. Like that create a loose coupling between your controller & your games, making it easier to extend the game with more games later on. You can also inject all your different games into the handler making it easier to unit test the functionality of the handler (with a mock set of games).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.