我设计了一个简单的井字游戏,以下是我的一些担忧:
在我看来,play() 方法不属于 Game()。如果在 Player 中定义它看起来会更好,但我不知道如何让它在 Player 中工作。另外,我通过向他们传递一个 id 来初始化玩家。这在我看来是错误的。如何解决这个问题?
/*Game class-establishes rules and determines winner */
package game.tictactoe;
public class Game {
String[] gameState = new String[9];
Player player1;
Player player2;
// updates the gamestate array with the latest move
public void updateStatus(int position, String symbol) {
gameState[position] = symbol;
}
public Game() {
player1 = new Player(1);
player2 = new Player(2);
for(int i = 0; i < 9; i++) {
gameState[i] = "";
}
}
// checks if game over. If game over, return winner or tie, else return "Game in prgress"
public String getGameStatus() {
if((gameState[0].equals(gameState[1]))
&& gameState[0].equals(gameState[2])
&& gameState[1].equals(gameState[2])
&& !gameState[0].equals("")){
return gameState[0];
}
else if((gameState[3].equals(gameState[4]))
&& gameState[3].equals(gameState[5])
&& gameState[4].equals(gameState[5])
&& !gameState[3].equals("")){
return gameState[3];
}
else if((gameState[6].equals(gameState[7]))
&& gameState[6].equals(gameState[8])
&& gameState[7].equals(gameState[8])
&& !gameState[6].equals("")){
return gameState[6];
}
else if((gameState[0].equals(gameState[3]))
&& gameState[0].equals(gameState[6])
&& gameState[3].equals(gameState[6])
&& !gameState[0].equals("")){
return gameState[0];
}
else if((gameState[1].equals(gameState[4]))
&& gameState[1].equals(gameState[7])
&& gameState[4].equals(gameState[7])
&& !gameState[1].equals("")){
return gameState[1];
}
else if((gameState[2].equals(gameState[5]))
&& gameState[2].equals(gameState[8])
&& gameState[5].equals(gameState[8])
&& !gameState[2].equals("")){
return gameState[2];
}
else if((gameState[0].equals(gameState[4]))
&& gameState[0].equals(gameState[8])
&& gameState[4].equals(gameState[8])
&& !gameState[0].equals("")){
return gameState[0];
}
else if((gameState[2].equals(gameState[4]))
&& gameState[2].equals(gameState[6])
&& gameState[4].equals(gameState[6])
&& !gameState[2].equals("")){
return gameState[2];
}
else {
for(int i=0; i < 9; i++) {
if(gameState[i].equals("")) {
return "Game in progress";
}
}
return "It's a tie!!";
}
}
public String play(int position) {
if(!player1.hasPlayed()) {
player1.played(true);
player2.played(false);
updateStatus(position,player1.getSymbol());
return player1.getSymbol();
}
else {
player2.played(true);
player1.played(false);
updateStatus(position,player2.getSymbol());
return player2.getSymbol();
}
}
}
/*Player class- Initializes the players that are in the game */
package game.tictactoe;
public class Player {
private boolean hasPlayed;
private String symbol;
private int id;
Player(int id) {
this.id = id;
if(id == 1) {
hasPlayed = false;
symbol = Symbols.X.toString();
}
else {
hasPlayed = true;
symbol = Symbols.O.toString();
}
}
public void played(boolean flag) {
hasPlayed = flag;
}
public boolean hasPlayed() {
return hasPlayed;
}
public String getSymbol() {
return symbol;
}
}
/* Symbol-Enum that represents the two symbols used in the game */
package game.tictactoe;
public enum Symbols {
X,O;
}