Source of Board.java


  1: 
  2: package tictactoe; 
  3: 
  4: import java.awt.*;
  5: import java.awt.event.*;
  6: 
  7: public class Board extends Canvas {
  8: 
  9:   public Board(Game game, int row, int column) {
 10:     this.game = game; 
 11:     this.row = row; 
 12:     this.column = column; 
 13:     maxMoves = row * column; 
 14:     moves = 0; 
 15:     board = new int[row][column]; 
 16:     addMouseListener(new MouseAdapter() {
 17:         public void mouseClicked(MouseEvent event) {
 18:           Point p = event.getPoint();  
 19:           Board.this.game.getPlayer().selectCell(p.x / columnWidth, p.y / rowHeight); 
 20:         }
 21:       });
 22:   }
 23: 
 24:   public void reset() { 
 25:     for (int i = 0; i < row; i++) {
 26:       for (int j = 0; j < column; j++) { 
 27:         board[i][j] = 0;
 28:       }
 29:     }
 30:     isover = false;
 31:     winner = -1; 
 32:     moves = 0; 
 33:     repaint();
 34:   }
 35: 
 36:   public void paint(Graphics g) {
 37:     Dimension d = getSize(); 
 38:     columnWidth = d.width / column;
 39:     rowHeight = d.height / row; 
 40: 
 41:     int fontSize = Math.min(rowHeight, columnWidth) - 8;
 42:     Font font = new Font("Sans serif", Font.BOLD, fontSize);
 43: 
 44:     g.setColor(Color.white); 
 45:     g.fillRect(0, 0, d.width, d.height);
 46:     g.setColor(Color.black);
 47:     int i, j; 
 48:     for (i = 0; i <= row; i++) {
 49:       g.drawLine(0, i * rowHeight, d.width, i * rowHeight); 
 50:     }
 51:     for (j = 0; j <= column; j++) {
 52:       g.drawLine(j * columnWidth, 0, j * columnWidth, d.height); 
 53:     }
 54:     
 55:     g.setFont(font); 
 56:     FontMetrics fm = g.getFontMetrics();
 57:     int adjX = (columnWidth - fm.stringWidth("X")) / 2; 
 58:     int adjO = (columnWidth - fm.stringWidth("O")) / 2; 
 59:     int adjy = (rowHeight - fm.getHeight()) / 2 + fm.getAscent(); 
 60:     
 61:     for (i = 0; i < row; i++) {
 62:       for (j = 0; j < column; j++) { 
 63:         if (board[i][j] != 0) {
 64:           switch (board[i][j]) {
 65:           case 1:
 66:             g.setColor(Color.red); 
 67:             g.drawString("X", i * columnWidth + adjX, j * rowHeight + adjy); 
 68:             break; 
 69:           case 2:
 70:             g.setColor(Color.blue); 
 71:             g.drawString("O", i * columnWidth + adjO, j * rowHeight + adjy); 
 72:             break; 
 73:           }
 74:         }
 75:       }
 76:     }
 77:   }
 78: 
 79:   public int getRow() {
 80:     return row; 
 81:   }
 82: 
 83:   public int getColumn() {
 84:     return column; 
 85:   }
 86: 
 87:   public boolean isOver() {
 88:     return isover; 
 89:   }
 90: 
 91:   public int getWinner() {
 92:     return winner; 
 93:   }
 94: 
 95:   public boolean makeMove(Move move, int playerId) {
 96:     if (isLegalMove(move)) {
 97:       moves++; 
 98:       board[move.row][move.column] = playerId; 
 99:       repaint(); 
100:       return true; 
101:     } else {
102:       return false; 
103:     }
104:   }
105: 
106:   public boolean isLegalMove(Move move) {
107:     return (board[move.row][move.column] == 0); 
108:   }
109: 
110:   protected void checkGame(int playerId) {
111:     if (moves > maxMoves) {
112:       isover = true;
113:       return; 
114:     }
115: 
116:     boolean win = false; 
117:     for (int i = 0; i < row; i++)
118:       if (checkRow(playerId, i)) {
119:         win = true; 
120:         break; 
121:       }
122:     if (!win) 
123:       for (int i = 0; i < column; i++)
124:         if (checkColumn(playerId, i)) {
125:           win = true; 
126:           break; 
127:         }
128:     if (!win) 
129:       win = checkDiagnal(playerId); 
130:     
131:     if (win) {
132:       winner = playerId; 
133:       isover = true; 
134:     }
135: 
136:     if (moves >= maxMoves) {
137:       isover = true; 
138:     }
139:   }
140: 
141:   protected boolean checkRow(int playerId, int row) {
142:     for (int i = 0; i < column; i++) 
143:       if (board[row][i] != playerId) 
144:         return false; 
145:     return true; 
146:   }
147: 
148:   protected boolean checkColumn(int playerId, int column) {
149:     for (int i = 0; i < row; i++) 
150:       if (board[i][column] != playerId) 
151:         return false; 
152:     return true; 
153:   }
154: 
155:   protected boolean checkDiagnal(int playerId) {
156:     boolean result = true; 
157:     for (int i = 0; i < row; i++) 
158:       if (board[i][i] != playerId) {
159:         result = false; 
160:         break; 
161:       }
162:     if (result)
163:       return true;
164:     result = true; 
165:     for (int i = 0; i < row; i++) 
166:       if (board[i][column - i - 1] != playerId) {
167:         result = false; 
168:         break; 
169:       }
170:     return result; 
171:   }
172: 
173:   protected Game game; 
174:   protected int row, column;
175:   protected int columnWidth, rowHeight;
176:   protected int board[][];
177:   protected int maxMoves; 
178:   protected int moves;   
179:   protected boolean isover = false; 
180:   protected int winner = -1; 
181: 
182: }