?? board.java
字號:
/** * Represents a OXO board . * Each square can either be blank or hold a value * * @author Ian Bradley * @version 1.0 March 2008 */public class Board{ private final String BLANK = ""; private String [][] board; /** * Constructor for objects of class Board */ public Board() { board = new String[3][3]; for ( int r = 0 ; r < 3 ; r++ ) for ( int c = 0 ; c < 3 ; c++ ) board[r][c] = BLANK; } /** * Sets a cell of the board to a specific character. * * @param coord the Coord of the cellrow * @param ch the value for the cell */ public void setCell( Coord coord, String ch) { board[coord.getRow()][coord.getColumn()] = ch; } /** * Gets the value held in the given cell. * * @param coord row column pair for cell * @return the value for the cell */ public String getCellValue( Coord coord) { return board[coord.getRow()][coord.getColumn()] ; } /** * tests to see if there is still space on the board. * * @return true if spaces otherwise false */ public boolean stillSpace() { for ( int r = 0 ; r < board.length ; r++ ) for ( int c = 0 ; c < board[r].length ; c++ ) if (board[r][c].equals(BLANK)) return true; return false; } /** * checks for a win. * * @return true if a winning line otherwise false */ public boolean checkWin() { return checkRows() || checkColumns() || checkDiagonals(); } private boolean checkRows() { for (int r = 0 ; r < board.length ; r++ ) if ( !( board[r][0].equals(BLANK) )) if ( ( board[r][0] == board[r][1] ) && ( board[r][1] == board[r][2] )) return true; return false; } private boolean checkColumns() { for (int c = 0 ; c < board.length ; c++ ) if ( !( board[0][c].equals(BLANK))) if ( ( board[0][c] == board[1][c] ) && ( board[1][c] == board[2][c] )) return true; return false; } private boolean checkDiagonals() { if ( !( board[0][0].equals(BLANK) )) if ( (board[0][0] == board[1][1] ) && ( board[1][1] == board[2][2]) ) return true; if ( !(board[0][2].equals(BLANK) )) if ( (board[0][2] == board[1][1] ) && ( board[1][1] == board[2][0]) ) return true; return false; } public Coord getFirstEmptyCell() { for ( int r = 0 ; r < board.length ; r++ ) for ( int c = 0 ; c < board[r].length ; c++ ) if ((board[r][c].equals(BLANK) )) return new Coord(r,c); return null; } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -