HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Cleverbot Game in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gamejavacleverbot

Problem

This is a project I've been working on for a while. It's basically Cleverbot. What do you think?

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

public class Cleverbot {

public static void main(String[] args) {
String choice;
Scanner user_in = new Scanner(System.in);
/ add "why" to all, with answer being, "You know why" /

Random start = new Random() ;
int say = start.nextInt(5);

if (say == 1){
System.out.println("Hi. I'm Typingbot. Please don't use punctuation or capital letters, because I don't really understand them.");
}
if (say == 2){
System.out.println("Welcome to Narnia, young one. Please don't use punctuation or capital letters, because I don't really understand them.");
}
if (say == 3){
System.out.println("Let's play a game. What do you want to play? Please don't use punctuation or capital letters, because I don't really understand them.");
}
if (say == 4){
System.out.println("Let's fight! pulls out pistol. Please don't use punctuation or capital letters, because I don't really understand them.");
}
if (say == 0){
System.out.println("Well? Are you going to answer my question? Yes or No?! Please don't use punctuation or capital letters, because I don't really understand them.");
}

for (int tries = 0; tries <=999999999; tries++) {

choice = user_in.nextLine();
if (choice.equals("because you are")) {
System.out.println("That is not a good reason") ;
}
else if (choice.equals("help")) {
System.out.println("Here are some helpful tips:");
System.out.println("Most of the time, don't use 'yes' or 'no' when answering a question. Instead, say, 'no i dont have a cat' or something that contains part of the question.");
System.out.println("Remember, NEVER use punctuation or capital letters. I really don't understand how they work.");
Sys

Solution

(The problem with the "test" option is that choice is still equal to the first user's input, "test". You should read another line of input into choice to get the response:

if (choice.equals("test")) {
    System.out.println(
            "HOW'S THIS MYSTERIOUS 'TEST' GOING, HUH?! DID I GET AN A++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++?!");
    choice = user_in.nextLine(); /* read another line */
    if (choice.equals("no")) {
        System.out.println("Yes I did! It was a trick question! A test about a test!");
    }
    if (choice.equals("yes")) {
        System.out.println("I'm glad somebody is honest for once.");
    }
    if (choice.equals("what")) {
        System.out.println(
                "Oh, I'm sorry, that's too complicated. Which probably explains why you got an F- on the test!");
        choice = user_in.nextLine(); /* read another line */
        if (choice.equals("which test")) {
            System.out.println("The first one, idiot.");
        }
    }
}


)

(EDITED)

Now let's review the code. Look at the following code:

Random start = new Random();
int say = start.nextInt(5);

if (say == 1) {
    System.out.println(
            "Hi. I'm Typingbot. Please don't use punctuation or capital letters, because I don't really understand them.");
}
if (say == 2) {
    System.out.println(
            "Welcome to Narnia, young one. Please don't use punctuation or capital letters, because I don't really understand them.");
}
if (say == 3) {
    System.out.println(
            "Let's play a game. What do you want to play? Please don't use punctuation or capital letters, because I don't really understand them.");
}
if (say == 4) {
    System.out.println(
            "Let's fight! pulls out pistol. Please don't use punctuation or capital letters, because I don't really understand them.");
}
if (say == 0) {
    System.out.println(
            "Well? Are you going to answer my question? Yes or No?! Please don't use punctuation or capital letters, because I don't really understand them.");
}


You're choosing among different possible responses based on a number. This can be simplified by using an array:

Random start = new Random() ;
int say = start.nextInt(5);

String responses[] = {
    "Hi. I'm Typingbot. Please don't use punctuation or capital letters, because I don't really understand them.",
    "Welcome to Narnia, young one. Please don't use punctuation or capital letters, because I don't really understand them.",
    "Let's play a game. What do you want to play? Please don't use punctuation or capital letters, because I don't really understand them.",
    "Let's fight! pulls out pistol. Please don't use punctuation or capital letters, because I don't really understand them.",
    "Well? Are you going to answer my question? Yes or No?! Please don't use punctuation or capital letters, because I don't really understand them."
};

System.out.println(responses[say]);


If you never used arrays, see the Arrays tutorial first and try out the examples in it.

And, instead of hard-coding the 5, you can use the array's length:

String responses[] = {
    "Hi. I'm Typingbot. Please don't use punctuation or capital letters, because I don't really understand them.",
    "Welcome to Narnia, young one. Please don't use punctuation or capital letters, because I don't really understand them.",
    "Let's play a game. What do you want to play? Please don't use punctuation or capital letters, because I don't really understand them.",
    "Let's fight! pulls out pistol. Please don't use punctuation or capital letters, because I don't really understand them.",
    "Well? Are you going to answer my question? Yes or No?! Please don't use punctuation or capital letters, because I don't really understand them."
};

Random start = new Random() ;
int say = start.nextInt(responses.length);
System.out.println(responses[say]);


Now change all the places where you randomly choose a response from a set of several responses to use an array, so it looks like the code above.

After you do this, you'll notice that the following three lines are repeated several times (perhaps with different variable names):

Random start = new Random() ;
int say = start.nextInt(responses.length);
System.out.println(responses[say]);


You can avoid repeating this same logic by moving it to a method.

If you're using Eclipse it can be done automatically: select these three lines (in one of the places where they are repeated) and select Refactor > Extract Method from the menu and use printRandomResponse for the method name. Click OK.

If you're not using Eclipse, do this manually by putting the following method inside your class:

private static void printRandomResponse(String[] responses) {
    Random start = new Random() ;
    int say = start.nextInt(responses.length);
    System.out.println(responses[say]);
}


And then change the three lines to this single line which calls t

Code Snippets

if (choice.equals("test")) {
    System.out.println(
            "HOW'S THIS MYSTERIOUS 'TEST' GOING, HUH?! DID I GET AN A++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++?!");
    choice = user_in.nextLine(); /* read another line */
    if (choice.equals("no")) {
        System.out.println("Yes I did! It was a trick question! A test about a test!");
    }
    if (choice.equals("yes")) {
        System.out.println("I'm glad somebody is honest for once.");
    }
    if (choice.equals("what")) {
        System.out.println(
                "Oh, I'm sorry, that's too complicated. Which probably explains why you got an F- on the test!");
        choice = user_in.nextLine(); /* read another line */
        if (choice.equals("which test")) {
            System.out.println("The first one, idiot.");
        }
    }
}
Random start = new Random();
int say = start.nextInt(5);

if (say == 1) {
    System.out.println(
            "Hi. I'm Typingbot. Please don't use punctuation or capital letters, because I don't really understand them.");
}
if (say == 2) {
    System.out.println(
            "Welcome to Narnia, young one. Please don't use punctuation or capital letters, because I don't really understand them.");
}
if (say == 3) {
    System.out.println(
            "Let's play a game. What do you want to play? Please don't use punctuation or capital letters, because I don't really understand them.");
}
if (say == 4) {
    System.out.println(
            "Let's fight! pulls out pistol. Please don't use punctuation or capital letters, because I don't really understand them.");
}
if (say == 0) {
    System.out.println(
            "Well? Are you going to answer my question? Yes or No?! Please don't use punctuation or capital letters, because I don't really understand them.");
}
Random start = new Random() ;
int say = start.nextInt(5);

String responses[] = {
    "Hi. I'm Typingbot. Please don't use punctuation or capital letters, because I don't really understand them.",
    "Welcome to Narnia, young one. Please don't use punctuation or capital letters, because I don't really understand them.",
    "Let's play a game. What do you want to play? Please don't use punctuation or capital letters, because I don't really understand them.",
    "Let's fight! pulls out pistol. Please don't use punctuation or capital letters, because I don't really understand them.",
    "Well? Are you going to answer my question? Yes or No?! Please don't use punctuation or capital letters, because I don't really understand them."
};

System.out.println(responses[say]);
String responses[] = {
    "Hi. I'm Typingbot. Please don't use punctuation or capital letters, because I don't really understand them.",
    "Welcome to Narnia, young one. Please don't use punctuation or capital letters, because I don't really understand them.",
    "Let's play a game. What do you want to play? Please don't use punctuation or capital letters, because I don't really understand them.",
    "Let's fight! pulls out pistol. Please don't use punctuation or capital letters, because I don't really understand them.",
    "Well? Are you going to answer my question? Yes or No?! Please don't use punctuation or capital letters, because I don't really understand them."
};

Random start = new Random() ;
int say = start.nextInt(responses.length);
System.out.println(responses[say]);
Random start = new Random() ;
int say = start.nextInt(responses.length);
System.out.println(responses[say]);

Context

StackExchange Code Review Q#106648, answer score: 10

Revisions (0)

No revisions yet.