Tuesday, 7 November 2017

My Girl Friend Chat - Android Game Development

My Girl Friend Chat Game
Hello once more, welcome to the world of mobile game development. Today, we are going to create a chat application which I named “My Girl Friend Chat”.  The game is all about inputting some sort of question in the edit text. If the question in the arrays of questions matches with the question inputted by you, then an answer which corresponds to the question will be display on the textview screen. This game is all about manipulating arrays to produce desirable result. It involves creating a database using array of strings. If the question inputted by you does not match any of the questions in the array, then the computer should be able to randomly pick out “I don’t know” answers. Clearly, the random class in Java will be use.
My girl friend chat game enables you to chat with a computer as if it was a real person. Therefore, every step is necessary. It should be able to think like a human and give answers to good questions. It should be able to answer simple questions like what is your name. Where do you live? To complicated questions like what do you believe in? And more complicated naughty question like have you ever cheated on your boy friend? Etc. To create a successful gaming experience, you will need to have close to like a thousand or more questions in your database. But first thing first, let’s check out the procedure
Procedures
1. An edit text for accepting input
2. A textview for displaying related answers.
3. A button for sending chat
4. An array of string for creating a question database
5. An array of string for creating an answer database
6. An array of string for creating “I don’t know answer” database

activity_main.xml
1. Under activity_main.xml, input the following statement 
<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="unycorperation.mygirlfriendchat.MainActivity">

<RelativeLayout

    android:layout_width="match_parent"

    android:layout_height="match_parent">

    <EditText

        android:id="@+id/editText"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:ems="10"

        android:hint="Chat with me"

        android:inputType="text"

        android:layout_below="@+id/textView"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:layout_marginLeft="12dp"

        android:layout_marginStart="12dp"

        android:layout_marginTop="70dp" />



    <Button

        android:id="@+id/button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Button"

        android:layout_below="@+id/editText"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="30dp" />



    <TextView

        android:id="@+id/textView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="TextView"

        android:textSize="45dp"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="69dp" />

</RelativeLayout>

</ScrollView>

MainActivity.java
2. In your MainActivity.java, popularise it with the following statement

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;



import java.util.Random;



public class MainActivity extends AppCompatActivity {

String questions[];

    String[] answers;

    String[] answerIdontKnow;

    Button button;

    EditText ed;

    TextView tv;

    private Random ran;

    private int myRan;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);

        ed = (EditText) findViewById(R.id.editText);

        tv = (TextView) findViewById(R.id.textView);

tv.setText("1. At the end of every questions, use a question mark" + "\n" +

"2. do not use comma(,), fullstop(.), exclamation mark(!) etc when asking a question"+ "\n"+

"3. you can choose to use either upercase or lowercase letters");

        questions = new String[]{"how are you?",

                "how was your day?",

                "where are you?",

                "hi",

                "hello",

                "what is your idea of a perfect day?",

                "do you like to sing in the shower?",

                "are you a kind of friend you will want to have as a friend?",

                "do you like singing in the public?",

                "what was your first impression of me?",

                "do you feel like you can tell me anything?",

                "what do you love about me the most?",

                "do you rather follow your heart or head?",

                "what makes you smile?",

                "who is your favorite superhero?",

                "if you are having a bad day how can i cheer you up?",

                "what is the craziest thing you have done?",

                "have you ever look in the mirror and wink at yourself",

                "what is your most embarrassing moment?",

                "do you have any regrets in life?",

                "what do you believe in?",

                "have you ever cheated on your boyfriend?",

                "have you ever been in love",

                "have you ever been arrested?",

                "who influence you most in life?",

                "what is the biggest lie you ever told?",

                "how many sexual partner have you had?",

                "what is one thing you will never do again?",

                "what is one thing you will like to do again?",

                "where is our relationship going?",

                "do you want to have kids?",

                "what is your stance on divorce?",

                "how will you describe your self in 5 words?",

                "does size matter to you?",

                "tell me about your first time?",

                "why do i like you so much?",

                "on a scale from 1 t0 5 rate me im i your type?",

                "if giving the opportunity would you let me kiss you?",

                "what part of my body do you admire the most?",

                "what is your name?",

                "can you tell me your name please?",

                "please your name?",

                "no",

                "yes",

                "i like you",

                "nice speaking to you",

                "i love you",

                "whats up",

                "where do you live?",

                "what is your phone number?"

        };



        answers = new String[]{"fine",

                "greate",

                "home",

                "hello",

                "hi",

                "being around people i love",

                "no",

                "yes",

                "yes",

                "tall and intelligent",

                "yes",

                "the way your handled matters",

                "head",

                "you",

                "captain america",

                "taking me shopping",

                "sky diving",

                "yes",

                "singing in public",

                "yes",

                "God",

                "no",

                "yes",

                "yes",

                "my mom",

                "The president of USA is my dad",

                "10 i think",

                "fall for the fake guys",

                "fall in love",

                "to marriage",

                "yes",

                "not a do and die affair",

                "good, beautiful, intelligent, smart, and loving",

                "yes",

                "why do you want to know?",

                "because i'm beautiful",

                "7, definitely yes",

                "yes",

                "do you really want to know?",

                "my name is Angel",

                "my name is Angel",

                "Angel",

                "i'm i lying?",

                "that intersting",

                "i like you too",

                "same here",

                "i am your girlfriend i know",

                "you were flirting with her",

                "Callifonai, USA",

                "0123456789"



        };



        answerIdontKnow = new String[]{"i don't understnd",

        "maybe",

        "seriously",

        "can you explain",

        "oh! really",

        "is that suppose to be a good thing?",

                "come again please",

                "that is intersting",

                "you are bad",

                "you are boring",

                "please can we change the subject?"

        };



        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                String myeditText = ed.getText().toString();

                String input = myeditText.toLowerCase();

                for (int i = 0; i < questions.length; i++) {

                    if (input.indexOf(questions[i]) != -1) {

                        tv.setText(answers[i]);

                        break;

                    } else {

                        chooseAnswer();

                    }

                }



            }

        });

    }

public void chooseAnswer(){

    ran = new Random();

    myRan = ran.nextInt(11);

    tv.setText(answerIdontKnow[myRan]);

    }



        }

3. Click on shift + f10 to run the program































How it works
We have 50 questions and corresponding 50 answers in both questions and answer database. We have 11 answers in “I don’t know” database.
To create a database, we use an array of string. This automatically acts as a database because more than one variable is in the string of questions
questions = new String[]{"how are you?",
               
"how was your day?",
               
"where are you?",
               
"hi",
               
"hello",
               
"what is your idea of a perfect day?",
               
"do you like to sing in the shower?",
               
"are you a kind of friend you will want to have as a friend?",
               
"do you like singing in the public?",
               
"what was your first impression of me?",
                
                  :
                       :
                       :
"please your name?",
               
"no",
               
"yes",
               
"i like you",
               
"nice speaking to you",
               
"i love you",
               
"whats up",
               
"where do you live?",
               
"what is your phone number?"
       
};
Arrays start counting from 0 up to the last index. For instance the “how are you” have an index of 0. “How was your day” have an index of 1, “where are you” have an index of 2 and so on. For every string created, the next string is separated by a comma. Also, note that for the last array in a string there is no comma attach to it.
answers = new String[]{"fine",
               
"great",
                
"home",
               
"hello",
               
"hi",
               
"being around people i love",
               
"no",
               
"yes",
               
"yes",
               
"tall and intelligent",
               
"yes",
               
"the way your handled matters",
                              
:
                     :
                     :
    "i'm i lying?",
               
"that interesting",
               
"i like you too",
               
"same here",
               
"i am your girlfriend i know",
               
"you were flirting with her",
               
"California, USA",
               
"0123456789"

       
};
Now we know how to create a database using array. Therefore, we proceed by creating the answers to those questions. The answers are also an array of string which also has an index number starting from 0. Therefore, “fine” has an index number of 0, “great” and “home” have an index number of 1 and 2 respectively. Here is where the game comes into play. If the question[0] = answer[0] then there is a match. The million dollars question is how do we check this? By using a “for” statement.
for (int i = 0; i < questions.length; i++) {
                   
if (input.indexOf(questions[i]) != -1) {
                       
tv.setText(answers[i]);
                       
break;
                    }
else {
                        chooseAnswer();
                    }
                }

            }
        });
    }
Firstly, we let the “for” loop to run across the array of question strings for (int i = 0; i < questions.length; i++) {. This means that every string in the arrays of question is being check one after the other.
Recall that the input is what we are inserting in the edit text. The java function indexOf() checks if a particular string correspond to a set of strings.
if (input.indexOf(questions[i]) != -1) {
Take this example
String love = “the good times”;
System.out.println(indexOf(“good”));
This actually returns true because the string “good” which we are searching for is included in the main string of love.
tv.setText(answers[i]);
After which, we let the textview to display text that correspond to the index number. Therefore, question[o] = answer[0] and question[1] = answer[1] and so on. For instance we want to loop through this array of string. If the keyword we are looking for is “fine” and we have two string value in the array, the indexOf() will return false. Meaning that when using indexOf() method, it return true if the keyword is unique and return false otherwise.
answers = new String[]{"fine",
               
"great and fine"};
Take for instance the string “how was your day” and the string “how are you”. These strings contain two keyword which is “how” therefore, the indexOf() method will return false. The computer will be force to use another statement. This is where the function chooseAnswer() comes into play. Assuming the computer cannot fetch out the correct question inputted by the user, a function chooseAnswer() is called. This function actually picks answer from the array “answerIdontKnow” database.
answerIdontKnow = new String[]{"i don't understnd",
       
"maybe",
       
"seriously",
       
"can you explain",
       
"oh! really",
       
"is that suppose to be a good thing?",
               
"come again please",
               
"that is intersting",
               
"you are bad",
               
"you are boring",
               
"please can we change the subject?"
       
};
It will be very stupid of the computer will always use a single respond to question it does not understand. Therefore, we let the computer to choose random answers to question it does not understand using the random class from Java.
public void chooseAnswer(){

    ran = new Random();

    myRan = ran.nextInt(11);

    tv.setText(answerIdontKnow[myRan]);

    }



        }
It should be well noted that this random number generated by the computer is place under every button click event. Meaning that each time the button is click, a random number is generated. A user may choose to use capitalize letters when asking questions. For instance the string “Where are you?” is different from the string “WHERE ARE YOU” which is also different from the string “where are you?” in other to avoid this error, we decided to change all string to lower case letters.
String input = myeditText.toLowerCase();
Therefore, all string is treated as a lower case. The game gives you a set of tutorial in game play. For instance the game give a condition of always ending your question with a question mark and when asking question do not use comma, exclamation mark, full stop, apostrophe, quotation etc to ask your question. The game also considers different people with the different way they speak. For instance I can ask you “what is your name” and others may say “your name please”. It is for the duty of the app to respond to the two different ways. If creating a string you should consider the different ways certain people will ask their questions.
"what is your name?",
               
"can you tell me your name please?",
               
"please your name?"
You can improve this game by including close to 5000 questions and 5000 answers. For now we keep it simple.



















1 comment:

  1. I have been following this blog for some time. love everything about it and awesome tutorial

    ReplyDelete