📄 board.java
字号:
package edu.rit.cs.mlr5773.connectfour;import java.util.*;import java.awt.*;/** * Represents a ConnectFour Board * * @author Mark Roth */public class Board extends Observable { /** The grid of pieces */ private int[][] grid; /** The current maximum height of any stack (used for optimization) */ private int maxHeight; /** The current move number */ private int moveNumber; /** * Initializes a new Board */ public Board(){ grid = new int[7][6]; maxHeight = -1; moveNumber = 0; } /** * Restores a board from its encoded state (unflattens it) */ public Board( String encoded ) { this(); StringTokenizer str = new StringTokenizer( encoded ); int x, y; for( y = 0; y < 6; y++ ) { for( x = 0; x < 7; x++ ) { grid[x][y] = new Integer( str.nextToken() ).intValue(); } } } /** * Helper function to see if there are any winners. * Checks the / diagonal. */ private boolean checkDiag1( int x, int y ) { int i; if( (x > 3) || (y > 2) ) return false; int player = grid[x][y]; int count = 1; for( i = 1; i < 4; i++ ) { if( grid[x+i][y+i] == player ) count++; else break; } return (count==4); } /** * Helper function to see if there are any winners. * Checks the \ diagonal. */ private boolean checkDiag2( int x, int y ) { int i; if( (x < 3) || (y > 2) ) return false; int player = grid[x][y]; int count = 1; for( i = 1; i < 4; i++ ) { if( grid[x-i][y+i] == player ) count++; else break; } return (count==4); } /** * Helper function to see if there are any winners. * Checks horizontally. */ private boolean checkHorizontal( int x, int y ) { int i; if( x > 3 ) return false; int player = grid[x][y]; int count = 1; for( i = (x+1); i < (x+4); i++ ) { if( grid[i][y] == player ) count++; else break; } return (count==4); } /** * Helper function to see if there are any winners. * Checks vertically. */ private boolean checkVertical( int x, int y ) { int i; if( y > 2 ) return false; int player = grid[x][y]; int count = 1; for( i = (y+1); i < (y+4); i++ ) { if( grid[x][i] == player ) count++; else break; } return (count==4); } /** * Creates an exact copy of this board. */ public Board copy() { Board nBoard = new Board(); int x, y; for( y = 0; y < 6; y++ ) { for( x = 0; x < 7; x++ ) { nBoard.grid[x][y] = grid[x][y]; } } nBoard.maxHeight = maxHeight; nBoard.moveNumber = moveNumber; return nBoard; } /** * Creates a flattened representation of this Board so that it can be * transported over the network. */ public String encode() { int x, y; String result = ""; for( y = 0; y < 6; y++ ) { for( x = 0; x < 7; x++ ) { result += grid[x][y] + " "; } } return result; } /** * Returns the piece at the given location, or -1 if an invalid location. * @return int * @param x int * @param y int */ public int getAt( int x, int y ) { if( (x<0) || (y<0) || (x>6) || (y>5) ) return -1; return grid[x][y]; } /** * Returns the maximum height in any given column. */ public int getMaxHeight() { if( maxHeight == -1 ) { int x, y, result = 5; boolean found; for (y = 0; y < 6; y++) { found = true; for (x = 0; x < 7; x++) { if (grid[x][y] != 0) { found = false; break; } } if (found) { result = y; break; } } maxHeight = result; } return maxHeight; } /** * Returns the current move number for this board. * (0 = Nobody has moved, 1 = Red has moved once, 2 = Red and black have * both moved, 3 = red just did second move, etc. ). */ public int getMoveNumber() { return moveNumber; } /** * Determines if anyone has won the game in the Board's current state. */ public int getWinner() { int x, y; int winner = 0; for( y = 0; y < 6; y++ ) { for( x = 0; x < 7; x++ ) { if( grid[x][y] != 0 ) { if( checkHorizontal( x, y ) || checkVertical( x, y ) || checkDiag1( x, y ) || checkDiag2( x, y ) ) { winner = grid[x][y]; break; } } } if( winner != 0 ) break; } if( winner == 0 ) { for( x = 0; x < 7; x++ ) { if( grid[x][5] == 0 ) { winner = -1; break; } } } return winner; } /** * Determines if it is valid to place a piece in the given column. */ public boolean isValidMove( int place ) { return (place >= 0) && (place <= 6) && (grid[place][5] == 0); } /** * Drops the given piece (player) in the given place (column). */ public void move( int place, int player ) { int y, found = -1; moveNumber++; for( y = 0; y < 6; y++ ) { if( grid[place][y] == 0 ) { found = y; break; } } if( found != -1 ) { grid[place][found] = player; } maxHeight = -1; setChanged(); notifyObservers( new Point( place, found ) ); } /** * Returns a string representation of this board that looks like the board. * 1 = Red, 2 = Black, 0 = nobody. */ public String toString() { String result = ""; int x, y; for( y = 5; y >= 0; y-- ) { for( x = 0; x < 7; x++ ) { result += grid[x][y] + " "; } result += "\n"; } return result; } /** * Undoes a move, given the column of the last move. */ public void undo( int move ) { int y; int found = -1; moveNumber--; maxHeight = -1; for( y = 5; y >= 0; y-- ) { if( grid[move][y] != 0 ) { grid[move][y] = 0; break; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -