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
The test program
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());
}
}