Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Oh those wonderful arrays 1

Status
Not open for further replies.

jisoo22

Programmer
Apr 30, 2001
277
US
Hello all,

I'm working on yet another school project on Java. This week's subject is arrays. I understand the basic concepts of arrays and even two-dimensional arrays. It's implementing them that I have problems with (but that comes with practice I hope =) ). Here's the objective of my new program:

Build a CrosswordBoard class with a board represented as a two-dimensional array of char, and with methods for adding words written horizontally (left to right) and vertically (top to bottom) starting from any board position.

The rules for placing a word on the board are:

The valid board positions are from (0,0) to (rows-1, columns-1).
If placeWord returns false, indicating the word cannot be placed in the indicated position, the board is unchanged.
A word cannot be placed on the board unless it fits entirely within the valid board positions.
A word cannot be placed on the board if any of its letters would cover a different letter. (It is ok for a letter to cover the SAME letter, e.g., an 'a' may cover another 'a' that is already on the board).


That being said, the code I've been working on along with the code to test it with are at the bottom. I don't think I have the constructor right (I'm sure it's a bit more complex then a couple of variable declarations) and as for the two missing methods, I was thinking a couple of for loops with nested if statements would be the best way to go. Can anyone give me a starter as to how to go about with the constructor and loops?

Thanks,
Jisoo22

The Program
Code:
public class CrosswordBoard {
    char [][] places;
    int rows;
    int cols;
    private static final char EmptySpaceChar = '.';
    public static final char DOWN = 'd';
    public static final char ACROSS = 'a';

    CrosswordBoard( int _rows, int _cols ) {
        // fill in the constructor
        rows = _rows;
        cols = _cols;
    }

    /** returns "true" and places word in places if it fits,
     *  returns "false" if it conflicts with another word already
     *  on the places or if it doesn't fit entirely on the board.
     */
    public boolean placeWord(String word, int row, int col, char dir) {
        if (dir == ACROSS) {
            return placeWordAcross(word, row, col);
        } else if (dir == DOWN)  {
            return placeWordDown(word, row, col);
        } else {
            System.err.println("Bad direction");
            return false;
        }
    }

    // Add the placeWordAcross and placeWordDown methods

    /** Return a string representing the whole board,
     *  with each row on a separate line (using \n to separate lines)
     *  Use a StringBuffer object to build it.
     */
    public String toString() {
        StringBuffer buf = new StringBuffer(rows*(cols+1));
        // Do something here ...
        return buf.toString();
    }
}

The test program
Code:
class testBoard {
    static void placeWord(CrosswordBoard b, String s, int row, int col, char dir) {
        System.out.print("Placing \"" + s + "\" at (" + row + "," + col + ");");
        boolean success = b.placeWord( s, row, col, dir ) ;
        if (success) {
            System.out.println("   Succeeded");
        } else {
            System.out.println("   Failed");
        }
    }
    public static void main(String args[] ) {
        System.out.println("---- Test 1, empty 3x3 board ----");
        CrosswordBoard b = new CrosswordBoard(3,3);
        System.out.println( b.toString() );
        System.out.println("---- Test 2, filling 3x3 board ---");
        placeWord(b, "foo", 0, 0, CrosswordBoard.ACROSS); // should succeed
        placeWord(b, "bar", 0, 0, CrosswordBoard.DOWN);   // should fail
        placeWord(b, "oop", 0, 2, CrosswordBoard.DOWN);   // should succeed
        placeWord(b, "oop", 2, 0, CrosswordBoard.ACROSS); // should succeed
        // Expect:
        // foo
        // ..o
        // oop
        System.out.println(b.toString());
    }
}
 
first you need to allocate the array or your app will crash when you try to use it!

CrosswordBoard( int _rows, int _cols ) {
// fill in the constructor
rows = _rows;
cols = _cols;
places = new char[rows][cols];
}

-pete
 
Hi again,

I'm making pretty good progress with this program but I've hit a rather nasty snag. I finished the methods that put characters in the crossword array that goes across a row and down a column. The problem is outputting the results. I'm required to utilize the StringBuffer class in order to do this and I'm having a tough time doing it. The test program, when invoked will use the CrossWordBoard class to process the words "foo", "bar", "oop", "oop" in certain starting coordinates. The coordinates assume that the crossword is a 3x3 matrix, so (0,0) is the upper left hand corner. I'm trying to process the output to make it look like as follows:

foo
..o
oop

I've posted links to the new class code as well as the testdriver class code if you would like to test (it is also posted in the first message. I hope someone can help, it's driving me bananas!

Class Program

Test Program

Any help is much appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top