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.

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;
}
}
 
//check to see if player won in diagonals
[B]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();
}
}

diagnol on a board like this:

00 01 02
10 11 12
20 21 22

Would be 00,11,22 or 20,11,02

The first part can easily be done with a simple loop. The second diagnol may take a little thought.

I have the positions set up as a point with a row/column pair and am not sure how to compare diagonal positions. I cannot figure out how to write the for loop to compare simultaneus positions.
Position Class:

public class Position implements GameConstants {
 /**
  * The number of the row
  */
 private int _row;
 /**
  * The number of the column
  */
 private int _col;
 /**
  * Creates a Position instance given the row and column
  * 
  * @param row the row for the Position
  * @param col the column for the Position
  */
 public Position(int row, int col) {
  _row = row;
  _col = col;
 }
 /**
  * Creates a Position instance given a point.  Converts the point to rows/columns.  This method
  * is subsuming that the cells on the board are squares.
  * @param pt the point to be converted
  */
 public Position(java.awt.Point pt) {
  _row = pt.y / SQUARE_SIZE;
  _col = pt.x / SQUARE_SIZE;
 }
 /**
  * Returns the row of the position
  * @return the row
  */
 public int getRow() {
  return _row;
 }
 /**
  * Returns the column of the position
  * @return the column
  */
 public int getCol() {
  return _col;
 }
 /**
  * Gives the center of the row/column space (assumes they are rectangular positions).
  * @return the java.awt.Point that represents the center of the Position
  */
 public Point getCenterLocation() {
  Position pos2 = new Position(_row + 1, _col + 1);
  java.awt.Point pt1 = this.toPoint();
  java.awt.Point pt2 = pos2.toPoint();
  return new Point((int)((pt1.x + pt2.x)/2), (int)((pt1.y + pt2.y)/2));
 }
 
 /* (non-Javadoc)
  * @see java.lang.Object#toString()
  */
 public String toString() {
  return "[" + _row + "," + _col + "]";
 }
 /**
  * Converts the row/column position into a java.awt.Point
  * @return the point that represents the upper left hand corner of the row/column position.
  */
 public java.awt.Point toPoint() {
  return new java.awt.Point(_col * SQUARE_SIZE, _row * SQUARE_SIZE);
 }
 /* (non-Javadoc)
  * @see java.lang.Object#hashCode()
  */
 public int hashCode() {
  return this.toPoint().hashCode();
 }
 /* (non-Javadoc)
  * @see java.lang.Object#equals(java.lang.Object)
  */
 public boolean equals(Object obj) {
  if (obj == null || this.getClass() != obj.getClass()) {
   return false;
  } // end of if ()
  Position other = (Position) obj;
  return ((_row == other._row) && (_col == other._col));
 }
}// Position

GameConstants:

public interface GameConstants {
 public static final int SQUARE_SIZE = 100;
 public static final int NUM_ROWS = 3;
 public static final int NUM_COLS = 3;
}
Member Avatar for iamthwee

>Would be 00,11,22 or 20,11,02

Only six options. Why not just hard code it? Done and dusted?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.