Eclipse中的Connect4Java
我被指派在eclipse中使用java构建一个connect4游戏,我得到了构建该游戏所涉及的大多数类和方法,但我在理解静态主要部分时遇到了问题。我在理解如何初始化它时总是遇到一些问题。下面是所有涉及的课程。任何帮助都会很好Eclipse中的Connect4Java,java,eclipse,Java,Eclipse,我被指派在eclipse中使用java构建一个connect4游戏,我得到了构建该游戏所涉及的大多数类和方法,但我在理解静态主要部分时遇到了问题。我在理解如何初始化它时总是遇到一些问题。下面是所有涉及的课程。任何帮助都会很好 /** * This class represents a board for Connect 4. Please method headers as is. * * @author * */ public class Board { privat
/**
* This class represents a board for Connect 4. Please method headers as is.
*
* @author
*
*/
public class Board {
private LocationState board[][];
private int noCols, noRows;
/**
*
* This constructor creates and initialises the board.
*
* @param col the number of columns in the board
* @param row the number of rows in the board
* @see LocationState
*/
public Board(int col, int row) {
board = new LocationState[col][row];
noCols = col;
noRows = row;
clear();
}
/**
* This method clears the board by setting each element to
* LocationState.EMPTY
*
* @return Nothing
*/
public void clear() {
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[0].length; j++)
board[i][j] = LocationState.EMPTY;
}
/**
* This method gets the location state (i.e. colour) at a particular
* location
*
* @param location
* @return Location state as LocationState
* @see Location
* @see LocationState
*/
public LocationState getLocationState(Location location) {
return board[location.getX()][location.getY()];
}
/**
* This method sets the location state (i.e. colour) at a particular
* location
*
* @param location
* @return Nothing
* @see Location
* @see LocationState
*/
public boolean setLocationState(Location location, LocationState state) {
if (location.getX() < getNoCols() && location.getY() < getNoRows()) {
board[location.getX()][location.getY()] = state;
return true;
}
return false;
}
public String toString() {
String s = "";
for (int i = 0; i < noRows; i++) {
for (int j = 0; j < noCols; j++)
s += (board[j][i] + "\t");
s += "\n";
}
return s;
}
/**
* Gets the number of columns on the board.
*
* @return number of columns on board as an integer
*/
public int getNoCols() {
return noCols;
}
/**
* Gets the number of rows on the board.
*
* @return number of rows on board as an integer
*/
public int getNoRows() {
return noRows;
}
}
/**
*
* Example Computer Player.
* CREATE YOUR OWN VERSION OF THIS, REPLACING THE NUMBER IN THE CLASS NAME
* WITH YOUR STUDENT NUMBER.
* @author Frank
*
*/
public class ComputerPlayer20059226 extends IPlayer {
public ComputerPlayer20059226(LocationState playerState) {
super(playerState);
}
@Override
public int getMove(Board board) {
//TODO
return -1;
}
}
/**
*
* Class to manage the connect 5 game
*
*/
public class Connect4 {
private IPlayer human, computer;
private Board board;
private IPlayer currentPlayer;
private int numTurns = 0;
public Connect4(IPlayer human, IPlayer computer, Board board) {
super();
this.human = human;
this.computer = computer;
this.board = board;
this.currentPlayer = human;
}
/**
* Toggles current player
*/
public void nextPlayer() {
if (currentPlayer == human) {
currentPlayer = computer;
} else {
currentPlayer = human;
}
}
/**
* Checks if there's a winner
* @param board to evaluate for winner
* @return boolean to detect winner
*/
public boolean isWin(Board board) {
//TODO
return false;
}
/**
* Checks for a draw
* @return
*/
public boolean isDraw() {
if(numTurns == board.getNoCols() * board.getNoRows())
{
return true;
}
else return false;
}
/**
* Method called to get next move from player
*
* @return boolean indicating move take successfully
*/
public boolean takeTurn() {
int col = currentPlayer.getMove(board);
for (int i = 0; i < board.getNoRows(); i++) {
if (board.getLocationState(new Location(col, i)) == LocationState.EMPTY) {
board.setLocationState(new Location(col, i),
currentPlayer.getPlayerState());
numTurns++;
return true;
}
}
return false;
}
public Board getBoard() {
return board;
}
/**
* @param args
*/
public static void main(String[] args) {
Connect4 connect = new Connect4(human.YELLOW);
}
}
**
*
* Abstract class to represent a player in a Connect 4 game.
* Extend this to create your player.
* Dependent on LocationState and Board types
* @author Frank
*
*/
public abstract class IPlayer {
private LocationState playerState;
public IPlayer(LocationState playerState) {
super();
this.playerState = playerState;
}
/**
* This method should return the next move for a Connect 4 game.
* Assume columns go from 1 to 7. Move computed from board parameter
* using suitable algorithm.
* @param board - Connect 4 board as type Board
* @return column number for next turn as integer.
*/
public abstract int getMove(Board board);
/**
* This method returns the location state (i.e. colour) associated
* with the player.
* @return playerState - colour of players piece as LocationState.
*/
public LocationState getPlayerState() {
return playerState;
}
}
public class Location {
private int x;
private int y;
public Location(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
/**
* Emum class to represent possible location states in a connect 4 board
* @author Frank
*
*/
public enum LocationState {
EMPTY, RED, YELLOW;
}
/**
*此类表示Connect 4的板。请按原样选择方法标题。
*
*@作者
*
*/
公共班级委员会{
私人场所州董事会[][];
私人国际noCols,noRows;
/**
*
*此构造函数创建并初始化电路板。
*
*@param col板中的列数
*@param row板中的行数
*@见LocationState
*/
公共董事会(内部列,内部行){
board=新位置状态[列][行];
noCols=col;
noRows=行;
清除();
}
/**
*此方法通过将每个元素设置为
*LocationState.EMPTY
*
*@一无所获
*/
公共空间清除(){
对于(int i=0;ipublic static void main(String[] args) {
IPlayer human = new HumanPlayer(); // <-- you have to create this class (assignment)
IPlayer computer = new ComputerPlayer20059226();
Board board = new Board(16, 16);
Connect4 connect = new Connect4(human, computer, board);
// do something with the objects
}
public class HumanPlayer extends IPlayer {
...
}
public abstract class IPlayer { // <-- bad naming