Friday, May 25, 2012

Friendship Algorithm in Java

UPDATE: Source code fixed and updated below.


UPDATE: Currently trying to nail down a problem with the loopcounter at the end of the code. I will update the source here as soon as it is fixed.

For Geek Pride Day (May 25)/ 42 Day (25+05+12), a super double geek-out! I took the Friendship Algorithm from the second season of The Big Bang Theory and spent about an hour converting it into Java code in Eclipse. The result is an extremely convoluted but functional bag of Java loops that accepts input of answers to the original flow chart's questions and goes down the proper path for the answers. Full source code below.


Full Source:


package Algorithm;
import java.util.Scanner;
public class FriendshipAlgorithm {
public static void main (String [] args) {
int loopcounter = 0;
System.out.println("Place phone call");
System.out.println("Home? (1=Y/2=N)");
int home = 0;
Scanner in = new Scanner(System.in);
home = in.nextInt();
while (home == 2) {
System.out.println("Leave message");
System.out.println("Wait for callback");
home = 0;
}
while (home == 1) {
System.out.println("Say: 'Would you like to share a meal?'");
System.out.println("What is the response? (1=Y/2=N)");
int meal = 0;
Scanner intwo = new Scanner(System.in);
meal = in.nextInt();
while (meal == 1) {
System.out.println("Dine together");
System.out.println("BEGIN FRIENDSHIP");
meal = 0;
home = 0;
}
while (meal == 2) {
System.out.println("Say: 'Do you enjoy a hot beverage?'");
System.out.println("What is the response? (1=y/2=N)");
int hot = 0;
Scanner inthree = new Scanner(System.in);
hot = in.nextInt();
while (hot == 1) {
System.out.println("Case: Tea/Coffee/Cocoa");
System.out.println("Have Coffee/Tea/Cocoa");
System.out.println("BEGIN FRIENDSHIP");
home = 0;
meal = 0;
hot = 0;
}
while (hot == 2) {
while (loopcounter < 6) {
System.out.println("Say: 'Recreational activity? Tell me one of your interests.'");
System.out.println("Do you share this interest? (1=Y/2=N)");
int shared = 0;
Scanner infour = new Scanner(System.in);
shared = in.nextInt();
loopcounter = loopcounter+1;
while (shared == 1) {
System.out.println("Say: 'Why don't we do that together?'");
System.out.println("Partake in interest");
System.out.println("BEGIN FRIENDSHIP");
home = 0;
meal = 0;
hot = 0;
loopcounter = 7;
shared = 0;
}
}
while (loopcounter > 5) {
System.out.println("Choose least objectional interest");
System.out.println("Partake in interest");
System.out.println("BEGIN FRIENDSHIP");
home = 0;
meal = 0;
hot = 0;
loopcounter = 0;
}
}
}
}
}
}

4 comments:

grandmommy said...

WOW!! Is all I can say!! Happy Geek Day to you and Wayne!!

Unknown said...


I don't know if it's just me or if everyone else encountering issues with your website. It looks like some of the written text on your content are running off the screen. Can somebody else please provide feedback and let me know if this is happening to them too? This may be a problem with my internet browser because I've had this happen before. Cheers all of craigslist

Kathleen M. Gartner said...

thanku

WUGI said...

Online casino for everyone, come in and win now only we have the best online slots The best onl