CS 107 (Spring '09)
[Schedule] [Programs] [Notes & Reference] [Examples][Syllabus] [Lab & TA] [Tests] [Grades]

Program #2: BoardSquare

Prof. Reed, CS 107, Fall '08
Due Monday 9/29 at 12:00 noon

Changes to the description below made after the original publishing shown in green, and additional subsequent changes shown in blue

Description

Write a program in Java to fill out a 3 x 3 crossword puzzle. This idea is taken from the online game found at http://www.funbrainpuzzle.com/puzzle_e00.htm. Running your program will look something like what is shown below, where user input is shown in bold:

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: b yay


       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 . y .
2 . a .
3 . y .
2. 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 . y .
2 w a r
3 . y .
3. 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 . y .
2 w a r
3 e y e
4. Please enter your move: check
There are errors at board positions shown below: 
  1 2 3 
  . . . 
  . . . 
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 . y .
2 w a r
3 e y e
5. Please enter your move: d oop
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 . y .
2 w a r
3 e y e
6. Please enter your move: 3 eyes
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 . y .
2 w a r
3 e y e
7. 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 w a r
3 e y e
8. Please enter your move: check

*** Well done! You got it right! ***
Thanks for playing!
Exiting Program...

You need to know the following concepts in order to write this program:

Everything from the first assignment; reading the textbook through chapter 3, with a bit of chapter 4; a simple loop; break and continue statements to selectively repeat or exit a loop; extracting characters from a String using charAt(); comparing strings using .equals(); though not strictly necessary it is very helpful to know how to make sections of code into a separate method.

Notes:

  1. Make sure you have read chapter 3 for use in this assignment. You may also need just a bit of chapter 4.
  2. Your playing board values must be stored and manipulated using 9 char variables. You may not use any other mechanism to store the board values (e.g. arrays)
  3. I recommend you write your program in the order shown below. The number of execution points (out of 55) is shown next to each item.
    1. (0 points) Have the basic structure display just the instructions. You could use the sample program from program1 as a starting point if you wish, or copy code from my solution to the first program and use that as a starting point.
    2. (5 points) Add the code to declare the 9 board variables (e.g. char p1,p2,p3,p4,p5,p6,p7,p8,p9;) initialize them all to '.', and then use them to display the board.
    3. (5 points) Add code after the board is displayed to prompt the user for input. Then add the code to read the user's input as a String.
    4. (10 points) Now put the above 2 steps (display board and get user input) into a loop. You might want to add comments around your code so that the main loop has roughly the following (with your code in part of it):
              // Main loop, which displays the board, prompts for user input, and makes the updates.
              while ( notDone) {
                  // Display the board
                  // ... your code that you already wrote should go here ...
                             
                  // Prompt for user input and get user input
                  // ... your code that you already wrote should go here ...
      
                  
      
              }//end while (notDone)
      Now compile and run your code and make sure it works. Note that your program will be in an infinite loop, so to exit you will need to close the Terminal Window, or right-click on the horizontal "barber pole" near the bottom-left of the BlueJ window to stop the program that is running
    5. (0 points) Now put the above 2 steps (display board and get user input) into a loop. You might want to add comments around your code so that the main loop has roughly the following (with your code in part of it):
      
              // Main loop, which displays the board, prompts for user input, and makes the updates.
              while ( notDone) {
                  // Display the board
                  // ... your code here ...
                             
                  // Prompt for user input and get user input
                  // ... your code here ...
      
                  
                  // Check for special user input word "exit"
                  // ... your code here ...
      
                  // Check for special user input word "check"
                  // ... your code here ...
                 
                  
                  // 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.
                  // ... your code here ...
      
                
                  // If we got to here, then user input should be valid. Make the move, reflecting the user input.
                  // ... your code here ...
      
              }//end while (notDone)
              
    6. (5 points) Go back the the prompt for user input and convert the input to all to lower case, so that you won't later have to compare (for example) to both "exit" and "EXIT". Assuming your user input has been declared as a String called userInput, you could do this using:
           userInput = userInput.toLowerCase(); // convert userInput to all lower case
      Now add code to handle the cases when the user types in "exit". To do this you will need to compare the userInput to each of these. Remember that when comparing Strings you will need to use something like:
            if( userInput.equals("exit")) ...
      If the user types in "exit" your program should exit the main loop, for instance using the "break" command.
      Also add the code to see if the user types in "check". For now don't do anything intelligent if the user types in "check", just display a message of some sort so you can verify that your program got to that point.
    7. (15 points) Now add the code to actually make a move, depending on what the user types in. Assume for now the input is perfect - you'll handle the error checking later. You can extract individual characters from the String that has the userInput using something like:
           String userInput;      // stores the user input
           ...
          userInput = keyboard.nextLine(); // get the next line of input
          // declare variables to store individual characters from user input.
          char character0, character1, character2, character3, character4;

          // extract and store the character at position 0 (the first character) in the user input
          character0 = userInput.charAt( 0);

          ...// and so on for other characters you want to extract

      Assuming you have declared the board playing pieces as: char p1,p2,p3,p4,p5,p6,p7,p8,p9;    and also assuming that you've extracted the user input individual characters into variables called character0, character1, character2, character3, character4;    you can do use if statements with these variables to actually make a move. For instance, if character0 has a 'c' stored in it, then you know that the userinput should be the word that goes down the 3rd column. To implement this you would take the characters that store the word part of the userInput and store them into the appropriate variables in the board. If the user correctly gave userInput: c ore    then you would do the assignments: p3 = character2; p6= character3; p9=character4;     Likewise if the userInput was correctly given as:   2 war then you would do the assignments: p4 = character2; p5= character3; p6=character4; 

    8. (5 points) Add the code to check user input to verify that the length is correct (5 characters long), and the first character is either 1,2,3 or A,B,C. To get the length of the userInput string you can use something like:
           int inputLength = userInput.length();
      Using the break and continue statements can help with the error checking.

    9. (10 points) Add the code to show which pieces are still in the wrong position when the user types in "check", as shown in the sample run above.

      
  4. Turnin your program electronically using the "turnin" command from your CS account. The turnin command should be used as follows:
  5. turnin -c cs107 -p program2 BoardSquare.java
    where the file containing your solution is called BoardSquare.java .  Please do not name your program anything besides BoardSquare.java

    If you want to verify that your project was turned in, look in the turnin directory for a file with your userid. For instance for this project, from your CS account you would type:
    ls -l ~i107/submit/program2

You must turnin this assignment using the turnin command. No email solutions will be accepted, unlike the first program.

[CS Dept] [UIC] [Prof. Reed]