Skip to content

Instantly share code, notes, and snippets.

@feehe21
Created October 11, 2018 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feehe21/ec3306d8d3b7dca7996f588111aa1570 to your computer and use it in GitHub Desktop.
Save feehe21/ec3306d8d3b7dca7996f588111aa1570 to your computer and use it in GitHub Desktop.
/**
* A program to carry on conversations with a human user.
* This is the initial version that:
* <ul><li>
* Uses indexOf to find strings
* </li><li>
* Handles responding to simple words and phrases
* </li></ul>
* This version uses a nested if to handle default responses.
* @author Laurie White
* @version April 2012
*/
public class Magpie2
{
/**
* Get a default greeting
* @return a greeting
*/
public String getGreeting()
{
return "Hello, let's talk.";
}
/**
* Gives a response to a user statement
*
* @param statement
* the user statement
* @return a response based on the rules given
*/
public String getResponse(String statement)
{
String response = "";
if (statement.indexOf("no") >= 0 && alone(statement, "no") == true)
{
response = "Why so negative?";
}else if (statement.indexOf("I want") >= 0){
response = "Whould you really be happy if you had " +
statement.substring(statement.indexOf("I want") + 7, statement.length()) + "?";
}else if(statement.indexOf("I") >= 0 &&
statement.indexOf("I") < statement.indexOf("you") &&
alone(statement, "I") == true && alone(statement, "you") == true){
response = "Why do you " +
statement.substring(statement.indexOf("I") + 2, statement.indexOf("you") - 1)
+ " me";
}
else if (statement.indexOf("mother") >= 0
|| statement.indexOf("father") >= 0
|| statement.indexOf("sister") >= 0
|| statement.indexOf("brother") >= 0)
{
if(alone(statement, "mother") == true || alone(statement, "father") == true
|| alone(statement, "sister") == true || alone(statement, "brother") == true){
response = "Tell me more about your family.";
}else{
response = getRandomResponse();
}
}else if (statement.indexOf("cat") >= 0
|| statement.indexOf("dog") >= 0){
if(alone(statement, "cat") == true || alone(statement, "dog") == true){
response = "Tell me more about your pets.";
}else{
response = getRandomResponse();
}
}else if (statement.indexOf("Mr. ") >= 0
&& alone(statement, "Mr.") == true){
response = "He sounds like a good teacher.";
}else if (statement.indexOf("Mrs. ") >= 0
&& alone(statement, "Mrs.") == true){
response = "She sounds like a good teacher.";
}else if (statement.indexOf("Ms. ") >= 0
&& alone(statement, "Ms.") == true){
response = "She sounds like a good teacher.";
}else if (statement.indexOf("Boo!") >= 0
&& alone(statement, "Boo!") == true){
response = "Ahh!";
}else if (statement.indexOf("food") >= 0
&& alone(statement, "food") == true){
response = "Can I have some?";
}else if (statement.indexOf("car") >= 0
&& alone(statement, "car") == true){
response = "Are you going out?";
}
else
{
response = getRandomResponse();
}
return response;
}
/**
* Pick a default response to use if nothing else fits.
* @return a non-committal string
*/
private String getRandomResponse()
{
final int NUMBER_OF_RESPONSES = 4;
double r = Math.random();
int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
String response = "";
if (whichResponse == 0)
{
response = "Interesting, tell me more.";
}
else if (whichResponse == 1)
{
response = "Hmmm.";
}
else if (whichResponse == 2)
{
response = "Do you really think so?";
}
else if (whichResponse == 3)
{
response = "You don't say.";
}
return response;
}
//
private boolean alone(String statement, String keyWord)
{
boolean start = false;
boolean finish = false;
if(statement.indexOf(keyWord) - 1 >= 0){
if(statement.substring(statement.indexOf(keyWord)-1, statement.indexOf(keyWord)).equals(" ")){
start = true;
}
}else{
start = true;
}
if(statement.indexOf(keyWord) + keyWord.length() < statement.length()){
if(statement.substring(statement.indexOf(keyWord) + keyWord.length(), statement.indexOf(keyWord) + keyWord.length() + 1).equals(" ")){
finish = true;
}
}else{
finish = true;
}
if(finish == true && start == true){
return true;
}else{
return false;
}
/*
if(statement.indexOf(keyWord) == 0){
if(statement.substring(keyWord.length(),keyWord.length()+1).equals(" ")){
return true;
}else{
return false;
}
}else if(statement.indexOf(keyWord) + keyWord.length() == statement.length()){
return true;
}else if (statement.substring(statement.indexOf(keyWord), statement.indexOf(keyWord)+1).equals(" ")
&& statement.substring(statement.indexOf(keyWord) + keyWord.length(), statement.indexOf(keyWord) + keyWord.length()+1).equals(" ")){
return true;
}else{
return false;}*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment