2

I'm trying to make a tic-tac-toe game and I'm encountering a lot of copy-paste work for inputs. I'm trying to figure out what design pattern and implementation works for prompting the user, collecting their input, comparing it and then acting by assigning a value. Right now my code looks like this.

public void promptPlayerCount(BufferedReader in) throws IOException {
    String  input;

    // initial prompt
    System.out.println("How many players?");
    input = "try again";
    while (input.equals("try again")) {
        input = in.readLine();
        // extract data and check it
        switch (Integer.parseInt(input)) {
            case 1:
                // assignment
                playerCount = 1;
                break;
            case 2:
                playerCount = 2;
                break;
            default:
                input = "try again";
                // clarified instructions
                System.out.println("please enter 1 or 2");
        }
    }
}

There's a part of me that thinks I could make a function (maybe a factory?) that allows me to generate a function by passing the constructing function the details of the initial prompt, the extraction method, the assignment action and the clarification message.

Would this be best done with lambda functions?

TheFaceOfBoe
  • 43
  • 1
  • 4
  • For what reason you want to use lambda expressions and generics? – Nikolas Charalambidis Jul 19 '18 at 18:34
  • What kind of code are you currently duplicating? Could you give an example? – Mick Mnemonic Jul 19 '18 at 18:35
  • Possibly related: [Validating input using java.util.Scanner](https://stackoverflow.com/q/3059333) – Pshemo Jul 19 '18 at 19:02
  • I'm just wondering if lambda functions would be the best way to construct a class/function to handle the assignment behavior. The code that's really being duplicated is the logic (display a message, loop until you get valid input, assign if it's valid, give clarifying message if input is incorrect) – TheFaceOfBoe Jul 19 '18 at 19:10
  • Examples of other prompts I'm doing is asking for input as to where they want to place their piece, if they want to play again and other random prompts I want to add. – TheFaceOfBoe Jul 19 '18 at 19:16

1 Answers1

1

Text input is hard, especially if you can't trust your user (like in a game). Your parseInt will throw a nasty exception right off if your value isn't an integer.

Also standard in is not friendly. I assume this is for an assignment so I won't fault you for using it, but in anything where you don't HAVE to use stdin, don't. The problem is that it's amazingly difficult to get Java to respond to anything less than an entire line with an enter at the end.

When dealing with user input I almost always trim it (Just because they love to insert random white spaces at the beginnings and end) and check to see if it's empty. This could probably be put into a function that also either shows an error or exits the program on "Empty" and otherwise returns a string.

If you often want int values, write a second function that calls the first. Have the second function return an int, but have it catch the exception if the text is invalid and prompt the user again. You could even have this function take a "Range" of integers as a parameter and provide a prompt. So what you have above could look like this:

playerCount = getUserInput("Please enter the number of users", 1, 2);

The rest is wrapped in simple non-redundant functions.

Won't write the code for you because A) it's probably a homework assignment and the fun part is actually coding it and B) someone else probably will provide a full solution with code before I'm done typing this :(

Good luck.

Bill K
  • 60,031
  • 14
  • 96
  • 147
  • What would be a good substitution for System.in for a terminal game? – TheFaceOfBoe Jul 19 '18 at 18:45
  • 1
    Why do you suggest a static method? Why don't you create a new object with a behavior? – Nikolas Charalambidis Jul 19 '18 at 18:50
  • @TheFaceOfBoe There is a reason you've most likely never seen a java console game. If you had to you could use the jline library, but it seemed beyond this problem. – Bill K Jul 19 '18 at 22:39
  • @Nikolas I suppose that I don't feel like I have enough information about his problem space to design an OO solution that wouldn't be confusing and possibly misleading... Plus it's not really necessary. It would probably be a pretty good idea to design a "Console" object with various I/o methods that wrapped all console I/O, but as I said, it sounded to me like a homework assignment and I didn't want to confuse the issue. – Bill K Jul 19 '18 at 22:43