我已经获得了在eclipse中使用java构建connect4游戏的任务,我获得了构建它所涉及的大多数类和方法,但是我在理解静态主要虚拟部分时遇到了问题。我一直在理解如何初始化其中的一些问题。以下是所涉及的所有课程。任何帮助都会很棒
/**
* 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;
}
答案 0 :(得分:0)
您的主要方法应如下所示:
public 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
}
您的作业似乎主要是创建代表您自己的IPlayer
的具体版本:
public class HumanPlayer extends IPlayer {
...
}
您的作业中的班级IPlayer
的名称实际应为AbstractPlayer
,因为IPlayer
表示它是一个界面,而不是一个抽象类。
public abstract class IPlayer { // <-- bad naming
此外,还要求您在班级public boolean isWin(Board board)
中实施方法Connect4
。
即使这不应该是困难的,我认为该方法具有类型Board
的参数并且同时不静态的事实真的是误导,因为实例Connect4
已经有一个Board
成员的实例。