diff options
| author | Scott Gasch <[email protected]> | 2016-06-02 11:43:53 -0700 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2016-06-02 11:43:53 -0700 |
| commit | e3a192fd3a5b535e246deba540164e69cfdfa689 (patch) | |
| tree | 9d04abb1e09cc4424943a7f1331368545f024f6b /org/guru/boggle/SimpleBoard.java | |
Diffstat (limited to 'org/guru/boggle/SimpleBoard.java')
| -rw-r--r-- | org/guru/boggle/SimpleBoard.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/org/guru/boggle/SimpleBoard.java b/org/guru/boggle/SimpleBoard.java new file mode 100644 index 0000000..c291a63 --- /dev/null +++ b/org/guru/boggle/SimpleBoard.java @@ -0,0 +1,120 @@ +package org.guru.boggle; + +import java.util.List; +import java.util.Random; + +import com.google.common.collect.Lists; + +public class SimpleBoard implements Board { + + private static final int DIM = 5; + private final char[][] state = new char[DIM][DIM]; + private final boolean[][] used = new boolean[DIM][DIM]; + private final Random rand = new Random(); + + public SimpleBoard() { + reset(); + dump(); + } + + @Override + public void reset() { + for (int i = 0; i < DIM; ++i) { + for (int j = 0; j < DIM; ++j) { + state[i][j] = randomLetter(); + used[i][j] = false; + } + } + } + + public void dump() { + for (int i = 0; i < DIM; ++i) { + for (int j = 0; j < DIM; ++j) { + System.out.printf("%c ", state[i][j]); + } + System.out.printf("\n"); + } + } + + public List<BoardPosition> allPositions() { + List<BoardPosition> result = Lists.newArrayList(); + for (int i = 0; i < DIM; ++i) { + for (int j = 0; j < DIM; ++j) { + result.add(new BoardPosition(i, j)); + } + } + return result; + } + + private char randomLetter() { + return Character.valueOf((char) (rand.nextInt(26) + 65)); + } + + private void checkDimension(int i) { + if (i < 0 || i >= DIM) { + throw new IllegalArgumentException("Bad board dimension: " + i); + } + } + + private void checkOnBoard(BoardPosition p) { + checkDimension(p.x()); + checkDimension(p.y()); + } + + @Override + public char letterAt(BoardPosition p) { + checkOnBoard(p); + return state[p.x()][p.y()]; + } + + @Override + public void markUsed(BoardPosition p) { + checkOnBoard(p); + used[p.x()][p.y()] = true; + } + + @Override + public void clearUsed(BoardPosition p) { + checkOnBoard(p); + used[p.x()][p.y()] = false; + } + + @Override + public boolean isUsed(BoardPosition p) { + checkOnBoard(p); + return used[p.x()][p.y()]; + } + + @Override + public List<BoardPosition> adjacentNeighborPositions(BoardPosition p) { + List<BoardPosition> result = Lists.newArrayList(); + int x = p.x(); + int y = p.y(); + for (int i = x - 1; i <= x + 1; ++i) { + for (int j = y - 1; j <= y + 1; ++j) { + if (i == x && y == j) continue; + if (i < 0 || j < 0) continue; + if (i >= DIM || j >= DIM) continue; + result.add(new BoardPosition(i, j)); + } + } + return result; + } + + @Override + public List<BoardPosition> adjacentUnusedNeighborPosition(BoardPosition p) { + List<BoardPosition> result = Lists.newArrayList(); + int x = p.x(); + int y = p.y(); + for (int i = x - 1; i <= x + 1; ++i) { + for (int j = y - 1; j <= y + 1; ++j) { + if (i == x && y == j) continue; + if (i < 0 || j < 0) continue; + if (i >= DIM || j >= DIM) continue; + if (used[i][j] == true) continue; + result.add(new BoardPosition(i, j)); + } + } + return result; + } +} |
