Post by Admin on Jan 8, 2016 1:15:33 GMT
Game Code! 3
ChattyKathy
Copy paste the code below into BlueJ
Go back to the previous page to get music in the background or choose another game!
In this game you can chat with an Artificial Intelligence. Although what it says is almost completely random it's still fun.
A Thanks to Youtube tutorials :3
Doors... and Angels vs Demons are the two main games while this one is more of the one on the side.
**********
TinY
Rage WaRnInG
Due to
Repetitiveness
**********
ChattyKathy
Copy paste the code below into BlueJ
Go back to the previous page to get music in the background or choose another game!
In this game you can chat with an Artificial Intelligence. Although what it says is almost completely random it's still fun.
A Thanks to Youtube tutorials :3
Doors... and Angels vs Demons are the two main games while this one is more of the one on the side.
**********
TinY
Rage WaRnInG
Due to
Repetitiveness
**********
/**
* Emma Donahue
* F Period
*
*/
* Emma Donahue
* F Period
*
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.lang.Math;
/**
* NOTE: All of this was created with the help of a youtube tutorial.
* The following code allows you to chat with an Artificial Intelligence! Typing in any word will cause the code to run and reply with a random comment below.
* Although it's not an "intelligent" conversation it's still very cool!
* There's examples of: dialog-methods-while-if-loops
*/
public class ChatBot extends JFrame implements KeyListener{
JPanel p=new JPanel();
JTextArea dialog=new JTextArea(20,50);
JTextArea input=new JTextArea(1,50);
JScrollPane scroll=new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
String[][] chatBot={
//standard greetings
{"Hi","Hello","hola","Hola","Howdy"},
{"Hi","Hello","Hey","Yo!"},
//question greetings
{"How are you?","What's good?","S'up?"},
{"I'm good, how are you?","I'm fine, you?"},
//yes
{"Yes.", "Yeah.", "Sure.", "Mhm."},
{"no","NO","NO!!!!!!!", "...", "Alright, what do you want to talk about?"},
//default
{"Hiya!", "I think colors are cool.","What's your name?" , "Cool name!" , "I'm a robot...I'm not a very smart one yet...", "I'm not a robot! ",
"Sorry if I repeat a lot, I'm forgetful...", "I'm bored.","What are your hobbies?", "That's not very nice.","I like to talk.","Nice Hobby.","zsdxftgyhjkklhgfd","Come @ me","Fite me m8.",
"No.. no no..","You're confusing.","What are you trying to say.", "Hello.","Hi.","Yo","Have you seen that video?", "Finish the lyrics Hello......", "UGLY.","I'm going to combust.","Mlgpro",
"You can save 15% or more on car insurence you know." , "My name is Kathy.","BeaUTIFUL."}
};
public static void main(String[] args){
new ChatBot();
}
public ChatBot(){
super("ChatBot");
setSize(600,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
input.addKeyListener(this);
p.add(scroll);
p.add(input);
p.setBackground(new Color(255,200,0));
add(p);
setVisible(true);
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(false);
//-----grab quote-----------
String quote=input.getText();
input.setText("");
addText("-->You:\t"+quote);
quote.trim();
while(
quote.charAt(quote.length()-1)=='!' ||
quote.charAt(quote.length()-1)=='.' ||
quote.charAt(quote.length()-1)=='?'
){
quote=quote.substring(0,quote.length()-1);
}
quote.trim();
byte response=0;
/*
0:we're searching through chatBot[][] for matches
1:we didn't find anything
2:we did find something
*/
//-----check for matches----
int j=0;//which group we're checking
while(response==0){
if(inArray(quote.toLowerCase(),chatBot[j*2])){
response=2;
int r=(int)Math.floor(Math.random()*chatBot[(j*2)+1].length);
addText("\n-->ChattyKathy\t"+chatBot[(j*2)+1][r]);
}
j++;
if(j*2==chatBot.length-1 && response==0){
response=1;
}
}
//-----default--------------
if(response==1){
int r=(int)Math.floor(Math.random()*chatBot[chatBot.length-1].length);
addText("\n-->ChattyKathy\t"+chatBot[chatBot.length-1][r]);
}
addText("\n");
}
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(true);
}
}
public void keyTyped(KeyEvent e){}
public void addText(String str){
dialog.setText(dialog.getText()+str);
}
public boolean inArray(String in,String[] str){
boolean match=false;
for(int i=0;i<str.length;i++){
if(str.equals(in)){
match=true;
}
}
return match;
}
}
DO NOT COPY PASTE THIS PART.
There is small type down here that keeps making it's way into the code and messes it up.
DO NOT COPY PASTE THIS PART.
There is small type down here that keeps making it's way into the code and messes it up.