java - 井字游戏错误

标签 java oop tic-tac-toe

我设计了一个简单的井字游戏,以下是我的一些担忧:

在我看来,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;  
}   

最佳答案

从面向对象的角度处理编程解决方案的一个好方法是首先问自己“我的应用程序空间中的所有名词是什么?”

正如我们从 1970 年代的周六早间电视节目中了解到的那样,名词可以指人、地点或事物。

枚举应用程序中的所有名词应该成为您要为应用程序编写的所有类的基础。名词是对象。因此,您需要使用的每个对象都需要为它们编写一个类。

在 Tic Tac Toe 中,名词可以是:Game、Grid、Symbol、Player

枚举应用程序中的名词后,您通常可以接下来考虑与它们相关的行为。问问自己“我的名词有什么行为?”或“对我的名词执行了哪些操作?”这应该成为您编写的方法的基础。您编写的方法自然应该针对它们所作用的类来编写。

例如,网格中可以放置一个标记。还可以检查网格以查看玩家是否获胜。玩家可以采取行动。您可以通过问自己这样的问题来预测您想要为类(class)编写的方法。

另一件事要问自己是“我的名词有什么特征(或属性)?”例如,游戏可以包含已玩游戏的计数。玩家可以计算胜利和失败的次数。这些特征与它们的名词有“has a”关系,这是它们应该成为类的实例字段的重要线索。

我想说,您走在正确的轨道上,但您还没有走得足够远,无法最大化应用程序的面向对象。还是太程序化了。因此,您可以预见它会很脆弱,即如果您想在不破坏应用程序的情况下进行更改,则很难修改。将您的设计建立在坚实的对象基础上将有助于您将应用程序分解为小的、可管理的和更可靠的可维护部分。

关于java - 井字游戏错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18993059/

相关文章:

java方法检索

java - 在 Java 应用程序中打开和返回数据库连接的最佳方式?

objective-c - 不了解 Objective c 中的基本属性和方法

java - Java中如何处理大量小对象的内存效率问题

确定井字游戏结束的算法

java - 如果输入有效,如何处理读取三角形三边并计算面积的家庭作业程序?

ruby - 面向对象的方式来处理复杂的方法

JavaScript Tic tac toe,如何检查图像的位置?

javascript 井字游戏

java - 如何解决异常java.sql.SQLException : No suitable driver found?