0

So I am creating the Kevin Bacon Game for my java class.

These are the files of names I gotta use

Actors:

Leonardo Dicaprio
Susan Sarandon
Tom Hanks
Robert De Niro
Barack Obama
Helen Keller
Katharine Cornell
Helen Hayes
John Laughlin
Mark Zuckerberg
Joe Lipari
Welker White

The relationships:

Susan Sarandon | Tom Hanks : Cloud Atlas
Tom Hanks | Kevin Bacon : Apollo 13
Leonardo Dicaprio | Kevin Bacon : This Boy's Life
Robert De Niro | Kevin Bacon : This Boy's Life
Barack Obama | Tom Hanks : the Road We've Traveled
Helen Keller | Katharine Cornell : Helen Keller in Her Story
Katharine Cornell | Helen Hayes : Stage Door Canteen
Helen Hayes | John Laughlin : Murder with Mirrors
John Laughlin | Kevin Bacon : Footloose
Mark Zuckerberg | Joe Lipari : Terms and Conditions May Apply
Joe Lipari | Welker White : Eat Pray Love
Welker White | Kevin Bacon : Lemon Sky

This is the program I have now:

package Game;

import java.io.*;

import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.Pattern;


/**
 * @author 
 *
 */
public class BaconNumber
{

 /**
  * @param args
  */
 private HashMap<String,String> relationships;
 private HashMap<String,String> actors;
 
 public static void main(String[] args)
 
   throws FileNotFoundException
 {
  Scanner input = new Scanner(new File("relationships"));
  HashMap<String, String> relationships = new HashMap<String, String>();
  
  while (input.hasNextLine()) {
            String[] columns = input.nextLine().split(Pattern.quote(" | "));
            relationships.put(columns[0], columns[1]);
        }

        System.out.println(relationships);
    
  }
 
 public BaconNumber()
 {
  relationships = new HashMap<String,String>();
  actors = new HashMap<String,String>();
 }
 
 public void printActors() throws FileNotFoundException
 {
  Scanner input = new Scanner(new File("actors"));
   
   while (input.hasNextLine()) 
     {
         System.out.println(input.nextLine());
     }
 }
 
 public int getBaconNumber( String actor , int number)
 {
  
  if( actor == "Kevin Bacon")
  {
   return number;
  }
  
  else
  {
   relationships.get(actor);
   System.out.println(actor + " starred in " + relationships.value + "with" + relationships.value );
   System.out.println( " The Bacon Number for " + actor + " is " + number );
   return number; // fix this
  }
  
  relationships.containsKey("Kevin Bacon")
//  {
//   number++;
//   System.out.println(" The bacon number for" + actor + " is " + number );
//  }
  
//  else
//  {
//   
//  }
  
  
  
 }
}

I need some help with my getBaconNumber(), I need the program to look up the actor and to calculate the bacon Number for when it finally reaches Kevin Bacon.

Heres what the profesor is asking for this program: 1. Look up the actor relationship in your hashmap 2. Print the current relationship to the console 3.Call the method recursively with the second actor in the relationship(Make sure you increment your bacon number).

This method is confusing me and I'm having trouble completing it.

I need it to print something like this :

Helen Keller
Helen Keller starred in "Helen Keller in her story" with Katherine Cornell.
Katherine Cornell starred in " Stage Door Canteen " with Helen Hayes.
Helen Hays starred in "Murder with mirrors" with John Laughlin.
John Laughlin starred in " FootLoose" with Kevin Bacon.
The bacon number for Helen Keller is 4

If Anyone can help at all please do I really do need help

Jay
  • 27
  • 2
  • 6
  • I also need additional Help with the BaconGame.java. It has to create a baconNumber object and manage the inputs and outputs of the game as described with the helen keller example. Should only have the main method contained in the program – Jay Mar 01 '16 at 02:51

1 Answers1

1

Since this is an assignment, I'll provide some concepts and ideas, but you'll need to turn it into something.

-you could load everything into arrays of [starting-person, ending-person, movie] and just iterate over the array multiple times until you find the end person.

-using a hashmap like you are works EXCEPT just loading each row singularly only represents one direction (look at Susan Sarandon to Barack Obama). And you can't load each row twice because you'd have duplicate keys. You could have two arrays, but not necessarily the most efficient.

-you could read in the information and create a undirected graph and then just traverse the graph for the recommended starting and ending point (which could have dead ends that you have to account for)

The biggest decisions you have is 1) how to represent the data and 2) how to traverse the data. Any number of solutions. Could be worth it to draw it out on paper and see how you would solve it and then turn it into a solution.

Scott Sosna
  • 1,443
  • 1
  • 8
  • 8
  • at this point Im kind of desperate for someone to show me how to start it out please. He said using arrays wouldnt be necessary same with the graph? is their another way to figure it out? Im about ready to pull my hair from this frustration – Jay Mar 01 '16 at 03:44
  • Have you read through the ideas I provided, tried to design a solution, written code to try anything out? Since you responded so soon after I did, I doubt it. End game is that writing computer programs is not easy, learning to write even harder. If I was your instructor - which essentially the community here is - I would expect to see some work, but I'm definitely not doing your homework for you. There are dozens of possible solutions, pick one and try and take it to a conclusion. – Scott Sosna Mar 01 '16 at 13:39