package rebelsky.madlibs; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStreamReader; import java.io.PrintWriter; /** * A simple implementation of the Mad Libs game. This game * prompts the user for a variety of parts of speech and then * puts them in a short story. * * @author Samuel A. Rebelsky * @version 1.0 of September 2005 */ public class MadLibs { public static void main(String[] args) throws Exception { // Look! The program uses a ton of variables. String line; // One line of the program String partOfSpeech; // The part of speech for the user to enter String response; // A response from the user. String story; // The story we've created. PrintWriter screen; // For printing to the screen. PrintWriter result; // For printing from to the result file. BufferedReader template; // For reading from the template file. BufferedReader eyes; // For user input. // Prepare the main input and output. screen = new PrintWriter(System.out, true); eyes = new BufferedReader(new InputStreamReader(System.in)); // The story is initially empty. story = ""; // Determine the file names and create appropriate readers and // writers. screen.print("Please enter the name of the template: "); screen.flush(); response = eyes.readLine(); template = new BufferedReader(new FileReader(new File(response))); screen.print("Please enter the name for the result file: "); screen.flush(); response = eyes.readLine(); result = new PrintWriter(new File(response)); screen.println(); // Four times (boy, I need a loop), read a line, find the thing // to ask, ask it, and add to the story. // ONE line = template.readLine(); partOfSpeech = line.substring(line.indexOf("_")+1); partOfSpeech = partOfSpeech.substring(0, partOfSpeech.indexOf("_")); screen.print("Please enter a(n) "); screen.print(partOfSpeech); screen.print(": "); screen.flush(); response = eyes.readLine(); partOfSpeech = line = line.replace("_".concat(partOfSpeech).concat("_"), response); story = story.concat(" ").concat(line).concat("\n"); result.println(line); // TWO line = template.readLine(); partOfSpeech = line.substring(line.indexOf("_")+1); partOfSpeech = partOfSpeech.substring(0, partOfSpeech.indexOf("_")); screen.print("Please enter a(n) "); screen.print(partOfSpeech); screen.print(": "); screen.flush(); response = eyes.readLine(); partOfSpeech = line = line.replace("_".concat(partOfSpeech).concat("_"), response); story = story.concat(" ").concat(line).concat("\n"); result.println(line); // THREE line = template.readLine(); partOfSpeech = line.substring(line.indexOf("_")+1); partOfSpeech = partOfSpeech.substring(0, partOfSpeech.indexOf("_")); screen.print("Please enter a(n) "); screen.print(partOfSpeech); screen.print(": "); screen.flush(); response = eyes.readLine(); partOfSpeech = line = line.replace("_".concat(partOfSpeech).concat("_"), response); story = story.concat(" ").concat(line).concat("\n"); result.println(line); // FOUR line = template.readLine(); partOfSpeech = line.substring(line.indexOf("_")+1); partOfSpeech = partOfSpeech.substring(0, partOfSpeech.indexOf("_")); screen.print("Please enter a(n) "); screen.print(partOfSpeech); screen.print(": "); screen.flush(); response = eyes.readLine(); partOfSpeech = line = line.replace("_".concat(partOfSpeech).concat("_"), response); story = story.concat(" ").concat(line).concat("\n"); result.println(line); // Print the story. screen.println(); screen.println("Here is your story ..."); screen.println(); screen.println(story); // Clean up. screen.close(); eyes.close(); template.close(); result.close(); } // main(String[]) } // class MadLibs