-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoggleBoard.java
61 lines (54 loc) · 1.59 KB
/
BoggleBoard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.*;
/**
* Represent a Boggle Board to play a game of
* Boggle.
* @author Owen Astrachan
* @date March, 2008
*/
public class BoggleBoard {
private String[] myFaces;
private int mySize;
/**
* Create a square boggle board from an array
* of Strings/Cube faces. The array should contain
* sizexsize entries, e.g., 16 for a 4x4 Boggle game.
* @param faces is row-major order of cubes used in
* the board for Boggle
*/
public BoggleBoard(String[] faces) {
myFaces = faces;
mySize = (int) Math.sqrt(faces.length);
}
/**
* Returns dimension of a square board, e.g., 4 or 5
* for Boggle or Big Boggle, respectively.
* @return size of board
*/
public int size() {
return mySize;
}
/**
* Return the cube face at specified location, this
* will be a one-character string, presumably, or
* "Qu" for a Q-cube.
* @param row is row of cube whose face is returned
* @param col is column of cube whose face is returned
* @return String/face of specified cube
* @throws ArrayIndexOutOfBoundsException if cube location
* isn't valid
*/
public String getFace(int row, int col) {
return myFaces[row * mySize + col];
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
for(int r=0; r < mySize; r++){
for(int c=0; c < mySize; c++){
sb.append(String.format("%3s",getFace(r,c)));
}
sb.append("\n");
}
return sb.toString();
}
}