Hex game making with java, NEED HELP!!
807603Nov 29 2007 — edited Nov 29 2007I need to make a simple Hex game using java. The first assignment is to make a board that detects the winner. Currently, the Board.java file is coded like this :
import java.awt.*;
import javax.swing.*;
public class Board extends JPanel {
private static final long serialVersionUID = 1L;
static final int SIZE = 11;
Nut[][] board = new Nut[SIZE][SIZE];
public Board() {
setLayout(null);
fill();
setBackground(Game.BACKGROUND);
}
void fill() {
for (int y = 0; y < board.length; y++) {
for (int x = 0; x < board[y].length; x++) {
Nut nut = createSpace(x,y);
add(nut);
board[y][x] = nut;
}
}
}
protected Nut createSpace(int x, int y) {
return new BoardSpace(x,y);
}
public void reset(int x, int y) {
replace(x, y, createSpace(x,y));
add(board[y][x]);
}
public void replace(int x, int y, Nut nut) {
remove(board[y][x]);
board[y][x] = nut;
nut.setBoardCoordinates(x,y);
}
public GamePiece neighbor(Nut gp, int direction) {
int x = gp.getXCoordinate();
int y = gp.getYCoordinate();
switch (direction) {
case 0: return getPiece(x, y-1); // above
case 1: return getPiece(x+1, y-1); // upper right
case 2: return getPiece(x+1, y); // lower right
case 3: return getPiece(x, y+1); // below
case 4: return getPiece(x-1, y+1); // lower left
case 5: return getPiece(x-1, y); // upper left
default: return null;
}
}
private GamePiece getPiece(int x, int y) {
if ((x >= 0 && x < board[0].length && y >= 0 && y < board.length)
&& (board[y][x] instanceof GamePiece))
return (GamePiece) board[y][x];
else
return null;
}
public Dimension getPreferredSize() {
Rectangle corner = board[board.length-1][board[0].length-1].getBounds();
return new Dimension(corner.x+corner.width, corner.y+corner.height+5);
}
}
There are other classes in this package, but I'm supposed to make a new board that inherits most of the features
on this current board that allows a Winner-detection feature of the game.
The hint says this:
When a piece is placed on the board, the question is whether or not there is a path from that piece to both sides of the board. To discover such a path, you can work outward from the piece, recursing on all six sides, while keeping track (in a list, for example) of all of the pieces you have already visited so that you don't visit them again. Whenever you enter a recursive call, you can check if the piece is already in the list, in which case you can return immediately. Otherwise, if the piece is for the same player, you can add it to the list and continue making recursive calls.
At the end of this recursive exploration, you'll have a list of all the pieces reachable from this one. Then, it's a matter of going through the list and seeing if there's a piece on the left end of the board and a piece on the right end of the board. (You'd use the same algorithm for the other player, but check for a top and bottom piece instead.)
You might want to use a helper method that takes the list of pieces as one of its parameters. Other parameters will include the player number and the neighboring game piece to be considered by the recursive call.
To avoid six separate recursive statements, you could make the recursive calls in a loop that iterates over the six possible directions, 0 to 5.
Please help! if you need to see other classes, please let me know. Thanks!