I wrote a simple program for tictactoe and have everything working except a method to check and see if a player has won the game diagonally. I am not sure how to write the for loop to check the board.
Code:
public class Board extends ReactiveDrawingPanel implements GameConstants {
private BoardState _state;
private Game _game;
private java.util.HashMap<Position, Mark> _boardMap;
public Board(Container container, Game game) {
super(container);
_game = game;
this.setColor(java.awt.Color.BLUE);
this.setDimension(new java.awt.Dimension(NUM_COLS * SQUARE_SIZE, NUM_ROWS * SQUARE_SIZE));
_state = new NullBoardState();
_boardMap = new java.util.HashMap<Position, Mark>();
}
public void start() {
this.removeAllGraphics();
this.drawGrid();
_state = new PlayBoardState();
_boardMap.clear();
}
public void react(java.awt.Point p) {
_state.reactToClick(this, p);
}
public void placeMark(java.awt.Point p) {
//place the mark of the appropriate player
Player currentPlayer = _game.getCurrentPlayer();
//which row/column did the user click on?
Position pos = new Position(p);
if(_boardMap.containsKey(pos)) {
//User tried to put X/O in a spot where there was an X/O
_game.userError();
}
else {
//User tried to put X/O in free spot.
Mark m = new Mark(this, currentPlayer.getImageName(), currentPlayer);
m.setCenterLocation(pos.getCenterLocation());
_boardMap.put(pos, m);
_game.playerMoved();
}
}
public void pause() {
System.out.println("board's pause");
_state = new NullBoardState();
for(Mark mark: _boardMap.values()) {
mark.hide();
}
}
public void unpause() {
System.out.println("board's unpause");
_state = new PlayBoardState();
for(Mark mark: _boardMap.values()) {
mark.show();
}
}
public void changeToNullState() {
_state = new NullBoardState();
}
public boolean playerWin(Player currentPlayer){
//check to see if player won in rows
for(int row = 0; row < NUM_ROWS; row++) {
if (this.rowWin(row, currentPlayer)) {
return true;
}
}
//check to see if player won in columns
for(int column = 0; column < NUM_COLS; column++) {
if (this.columnWin(column, currentPlayer)) {
return true;
}
}
[B]//check to see if player won in diagonals
if (this.diagonalWin(currentPlayer)) {
return true;
}
[/B]
return false;
}
private boolean rowWin(int row, Player currentPlayer) {
for(int column = 0; column < NUM_COLS; column++) {
if(!_boardMap.containsKey(new Position(row, column))){
return false;
}
if ( currentPlayer.getName() !=
_boardMap.get(new Position(row, column)).getPlayer().getName()) {
return false;
}
}
return true;
}
private boolean columnWin(int column, Player currentPlayer) {
for(int row = 0; row < NUM_ROWS; row++) {
if(!_boardMap.containsKey(new Position(row, column))){
return false;
}
if ( currentPlayer.getName() !=
_boardMap.get(new Position(row, column)).getPlayer().getName()) {
return false;
}
}
return true;
}
[B]private boolean diagonalWin(Player currentPlayer) {
return false;
}[/B]
private void drawGrid() {
NGP.Graphics.Line vline, hline;
for ( int i = 1; i < 3; i++ ) {
vline = new NGP.Graphics.Line(this);
vline.setColor(java.awt.Color.WHITE);
vline.setPoints(new java.awt.Point(i * SQUARE_SIZE, 0),
new java.awt.Point(i * SQUARE_SIZE,
3 * SQUARE_SIZE));
hline = new NGP.Graphics.Line(this);
hline.setColor(java.awt.Color.WHITE);
hline.setPoints(new java.awt.Point(0, i * SQUARE_SIZE),
new java.awt.Point(3 * SQUARE_SIZE,
i * SQUARE_SIZE));
} // end of for ()
}
public int getBoardSize() {
return _boardMap.size();
}
}
Can someone please help.