/**--------------------------------------------- * Program 2 - WordSquare * This program get user input to create * a crossword puzzle of 3 letter words. * This idea is taken from: * http://www.funbrainpuzzle.com/puzzle_e00.htm * * Class: CS 107, Spring 2008 * Lab: Billie Joe Armstrong, Wed. 6:00 AM * System: BlueJ 2.1.1, jsdk 1.5, Windows XP * * @author Dale Reed * @version September 9, 2008 * * Running the program looks like: Author: Dale Reed Program: #2, WordSquare TA: Englebert Humberdink, T 4-5 Sept 9, 2008 Welcome to the 3x3 crossword puzzle. This idea is taken from: http://www.funbrainpuzzle.com/puzzle_e00.htm Each row and each column forms a 3 letter word. On each move enter either a number for a row (1,2, or 3) or a letter for a column (A,B, or C), and then the 3 letter word to be placed there. For example, entering: 2 cat would place the word "cat" across the row 2. Similarly, entering: C dog would place the word "dog" down column 3. In each case user input overwrites what is already in those spaces. At any point type in "check" (without the quotes) to check your answer so far. At any point enter "exit" (without the quotes) to exit the program Let's begin. Across Down 1. long ___ and far away A. a feeling of great respect 2. fighting between nations B. cheerful and excited 3. used for seeing C. what metal is extracted from A B C 1 . . . 2 . . . 3 . . . 1. Please enter your move: 1 ago Across Down 1. long ___ and far away A. a feeling of great respect 2. fighting between nations B. cheerful and excited 3. used for seeing C. what metal is extracted from A B C 1 a g o 2 . . . 3 . . . 2. Please enter your move: a awe Across Down 1. long ___ and far away A. a feeling of great respect 2. fighting between nations B. cheerful and excited 3. used for seeing C. what metal is extracted from A B C 1 a g o 2 w . . 3 e . . 3. Please enter your move: check There are errors at board positions shown below: . . . . 5 6 . 8 9 Please keep trying. Across Down 1. long ___ and far away A. a feeling of great respect 2. fighting between nations B. cheerful and excited 3. used for seeing C. what metal is extracted from A B C 1 a g o 2 w . . 3 e . . 4. Please enter your move: 2 war Across Down 1. long ___ and far away A. a feeling of great respect 2. fighting between nations B. cheerful and excited 3. used for seeing C. what metal is extracted from A B C 1 a g o 2 w a r 3 e . . 5. Please enter your move: 4 elf User input was invalid. Please retry your move. Across Down 1. long ___ and far away A. a feeling of great respect 2. fighting between nations B. cheerful and excited 3. used for seeing C. what metal is extracted from A B C 1 a g o 2 w a r 3 e . . 6. Please enter your move: 3 eye Across Down 1. long ___ and far away A. a feeling of great respect 2. fighting between nations B. cheerful and excited 3. used for seeing C. what metal is extracted from A B C 1 a g o 2 w a r 3 e y e 7. Please enter your move: check *** Well done! You got it right! *** Thanks for playing! Exiting Program... * ---------------------------------------------- */ // Import libraries needed by the program import java.util.Scanner; // used for console input // Declare the class public class WordSquare { // Fields that can be accessed anywhere in the class go here Scanner keyboard = new Scanner( System.in); // used to read user input // Declare the variables used to represent the board, which will be used throughout the program. // In the variables below we start numbering at 1 rather than 0, since it makes it easier to visualize char c1,c2,c3,c4,c5,c6,c7,c8,c9; //---------------------------------------------------------------------------------------- // main() - startup main loop. It is necessary to create an instance of this class // and then call a method from that instance, otherwise there are all kinds // of error messages having to do with non-static objects (e.g. keyboard) // being called from a static context (e.g. main). By creating the instance // and *then* using keyboard, it is not longer being called from a static // context. Don't worry about understanding all this right now. public static void main(String[] args) { // create an instance of this class WordSquare theInstance = new WordSquare(); // call a non-static method to do everything else theInstance.mainLoop(); } //---------------------------------------------------------------------------------------- // mainLoop() - display identifying information and run main loop. // In your program, you will only add code inside this method. You don't need to // change anything above except for the comments at the top of the program. // void mainLoop() { // Display identifying information System.out.println( "Author: Dale Reed \n" + "Program: #2, WordSquare\n" + "TA: Englebert Humberdink, T 4-5 \n"+ "Sept 9, 2008"); System.out.println(); // Display game instructions System.out.println( "Welcome to the 3x3 crossword puzzle.\n" + " This idea is taken from: http://www.funbrainpuzzle.com/puzzle_e00.htm \n" + "Each row and each column forms a 3 letter word.\n" + "On each move enter either a number for a row (1,2, or 3) or a letter \n" + "for a column (A,B, or C), and then the 3 letter word to be placed there.\n" + "For example, entering: \n" + " 2 cat\n" + "would place the word \"cat\" across the row 2. Similarly, entering: \n" + " C dog \n" + "would place the word \"dog\" down column 3. In each case user input overwrites \n" + "what is already in those spaces. At any point type in \"check\" (without the \n" + "quotes) to check your answer so far. At any point enter \"exit\" (without the \n" + "quotes) to exit the program \n" + "Let's begin. \n"); // declare variables String userInput = ""; // stores user input int inputLength = 0; // will track length of input int moveNumber = 1; // move number // initialize variables so that BlueJ doesn't complain c1 = c2 = c3 = c4 = c5 = c6 = c7 = c8 = c9 = '.'; boolean notDone = true; // boolean "flag" used to exit main loop // Main loop, which displays the board, prompts for user input, and makes the updates while ( notDone) { // Display the board displayBoard(); // Prompt for user input System.out.print(moveNumber + ". Please enter your move: "); moveNumber++; // increment move number userInput = keyboard.nextLine(); // read an entire line // Convert to upper case to simplify checking userInput = userInput.toLowerCase(); // Check for special user input word "exit" if( userInput.equals( "exit")) { // Exit program notDone = false; // forces exiting main loop break; // break out of enclosing main loop, in this case to end program // Alternatively, to end the program right here we could have used: // System.exit( 0); } // Check for special user input word "check" if( userInput.equals( "check")) { if( solutionIsCorrect() ) { System.out.println("\n*** Well done! You got it right! ***"); break; // break out of enclosing main loop } else { System.out.println("Please keep trying."); continue; // Go back up to redisplay board and reprompt for move } } // Verify user input. Input should be 5 characters long, have a 1,2,3,a,b, or c in the // first character, and should have a space in the second character. char firstChar = userInput.charAt( 0); // get 1st character, at index position 0 if( userInput.length() != 5 || (firstChar!='1' && firstChar!='2' && firstChar!='3' && firstChar!='a' && firstChar!='b' && firstChar!='c') || userInput.charAt( 1) != ' ' ){ // user input was invalid System.out.println("User input was invalid. Please retry your move."); continue; // go back up to display board and reprompt for user input }//end if( userInput... // If we got to here, then user input should be valid. Make the move, reflecting the user input. makeTheMove( userInput); }//end while (true) System.out.println("Thanks for playing!"); System.out.println("Exiting Program..."); }//end method mainLoop() //---------------------------------------------------------------------------------------------- // displayBoard() // Display the game board, which will initially look like: // // A B C // 1 . . . // 2 . . . // 3 . . . // void displayBoard() { // Display the hints System.out.println(); System.out.println(); System.out.println(" Across Down "); System.out.println("1. long ___ and far away A. a feeling of great respect "); System.out.println("2. fighting between nations B. cheerful and excited "); System.out.println("3. used for seeing C. what metal is extracted from"); // Display the board System.out.println(); System.out.println(" A B C"); System.out.println("1 " + c1 + " " + c2 + " " + c3); System.out.println("2 " + c4 + " " + c5 + " " + c6); System.out.println("3 " + c7 + " " + c8 + " " + c9); }// end method displayBoard() //---------------------------------------------------------------------------------------------- // solutionIsCorrect() // Check to see if the user has found the correct solution. Display error messages as we go // for each position (1..9) where the letters are incorrect. The correct answer is shown at // left below, and the corresponding letter positions are shown to the right of that. // // a g o 1 2 3 // w a r 4 5 6 // e y e 7 8 9 boolean solutionIsCorrect() { if( (c1=='a') && (c2=='g') && (c3=='o') && (c4=='w') && (c5=='a') && (c6=='r') && (c7=='e') && (c8=='y') && (c9=='e') ) { // All values are correct return true; } else { // All values are not correct, display errors System.out.println("There are errors at board positions shown below: "); if( c1!='a') System.out.print(" 1 "); else System.out.print(" . "); if( c2!='g') System.out.print("2 "); else System.out.print(". "); if( c3!='o') System.out.println("3 "); else System.out.println(". "); if( c4!='w') System.out.print(" 4 "); else System.out.print(" . "); if( c5!='a') System.out.print("5 "); else System.out.print(". "); if( c6!='r') System.out.println("6 "); else System.out.println(". "); if( c7!='e') System.out.print(" 7 "); else System.out.print(" . "); if( c8!='y') System.out.print("8 "); else System.out.print(". "); if( c9!='e') System.out.println("9 "); else System.out.println(". "); // There were errors, so return false return false; }// end else }// end method solutionIsCorrect() //---------------------------------------------------------------------------------------------- // makeTheMove() // appropriately update the 3 variables corresponding to the selected row (1,2, or 3) or column // (a,b, or c) and the user input 3-letter word. void makeTheMove( String userInput) { // extract the location and 3-letter word characters char location = userInput.charAt(0); // 1st character (at index position 0) has the destination char char letter1 = userInput.charAt(2); // 1st letter (of 3) at index position 2 char letter2 = userInput.charAt(3); // 1st letter (of 3) at index position 3 char letter3 = userInput.charAt(4); // 1st letter (of 3) at index position 4 // Now assign the appropriate destination characters, checking for each of possible 6 destinations if( location == '1') { // row 1 across c1 = letter1; c2 = letter2; c3 = letter3; } if( location == '2') { // row 2 across c4 = letter1; c5 = letter2; c6 = letter3; } if( location == '3') { // row 3 across c7 = letter1; c8 = letter2; c9 = letter3; } if( location == 'a') { // column a down c1 = letter1; c4 = letter2; c7 = letter3; } if( location == 'b') { // column b down c2 = letter1; c5 = letter2; c8 = letter3; } if( location == 'c') { // column c down c3 = letter1; c6 = letter2; c9 = letter3; } }// end method makeTheMove() }//end class WordSquare