Java Tictaoe游戏

Java Tictaoe游戏,java,Java,我正在做我的任务,那就是创建一个Tictatcoe检查器,由于某种原因,我想不出一种方法来完成创建游戏的第一步。我们从一个文本文件中读取,其中包含一行代表一个游戏;例如: x,x,o, , ,x, ,o,o 在传统的电路板上看起来是这样的: x|x|o ----- | |x ----- |o|o 以下是我目前掌握的情况: class TicTacToe{ static String[][] game; public TicTacToe(int size){

我正在做我的任务,那就是创建一个Tictatcoe检查器,由于某种原因,我想不出一种方法来完成创建游戏的第一步。我们从一个文本文件中读取,其中包含一行代表一个游戏;例如:

x,x,o, , ,x, ,o,o
在传统的电路板上看起来是这样的:

x|x|o
-----
 | |x
-----
 |o|o
以下是我目前掌握的情况:

class TicTacToe{
    static String[][] game;

    public TicTacToe(int size){
        this.game = new String[size][size];
    }

    public static TicTacToe create(String input){
        String tokens = input.split(",");
        int size = (int)Math.sqrt(tokens.length); //For setting a size
                                                  // of the board

        return null;
    }
}

我不明白的是如何返回一个新的TicTacToe对象:当我编写检查行、列等的方法时,如何让电路板检查?我这样做对吗?

你想得太多了。使用“returnnew TicTacToe(size);”而不是“returnnull;”

虽然,通常在创建静态工厂方法(如“create”方法)时,我会将构造函数设置为私有的,因此只能从静态create方法调用它

…然后,如果您想真正记住传入的x/o位置,则需要更新电路板

[W] 当我编写检查行、列等的方法时,我将如何操作 让董事会检查一下

对象的实例应包含有关该板的所有信息。您已经(大部分)完成了-
游戏
是包含有关棋盘信息的字段。唯一的问题是,每个电路板都将包含相同的电路板信息,如果它被更新,它将包含更新的信息

如果你想把这当作一个例子,那么你需要做四件事:

  • 游戏
    中删除
    静态
    修改器。这是不必要的,会导致多个对象的状态不一致
  • 让你的构造函数私有化——除非我遗漏了什么,否则没有理由在工厂之外初始化它

     private TicTacToe(int size) {
         game = new String[size][size];
     }
    
    或者,更好的是:

     private TicTacToe(String[][] hydratedBoard) {
          game = hydratedBoard;
     }
    
    我马上告诉你原因

  • 实现
    TicTacToe.create(String)
    ,并返回完全水合的对象。建议隐式创建
    String[][]
    ,创建对象并将其作为参数传递给构造函数,然后返回
    TicTacToe
    对象

    public static TicTacToe create(String input) {
        String[][] board = new String[3][3];
        int i = 0; // count the row we're on
        String[] tokens = input.split(",");
        for(int j = 0; j < board.length; j++) {
            if (j % 3 == 0) { // advanced to the end of the column set (SO)
                i++;
            }
            board[i][j] =  tokens[i*3 + j];
        }
        return new TicTacToe(board);
     }
    
    publicstatictoe创建(字符串输入){
    字符串[][]板=新字符串[3][3];
    int i=0;//计算我们所在的行
    String[]tokens=input.split(“,”);
    对于(int j=0;j
  • 为board对象提供某种类型的getter。你可能不必按照作业要求这样做,但这是一个很好的实践。它可以是
    字符串[][]
    上的直接getter,也可以是某种漂亮的打印表示;我不知道。我将把这个实现留给读者

在这里,我为这个游戏添加了一个集成测试,它可能会让您了解如何创建/思考这个游戏

    public void testWinnerPlayerIWithVerticalLine() {

    this.displayUnitTestDescription("Integration Test for having player 'X' as winner on a vertical line use case");
    final IBoardGame<TicTacToeMove, TicTacToePlayer> ticTacTocGame = new TicTacToeGame();
    final String playerIDisplayName = "X";
    final String playerIIDisplayName = "O";
    final TicTacToePlayer playerI = new TicTacToePlayer(playerIDisplayName);
    final TicTacToePlayer playerII = new TicTacToePlayer(playerIIDisplayName);
    ticTacTocGame.setGamePlayers(new TicTacToePlayer[]{playerI, playerII});
    TicTacToePlayer[][] expectedBoardInteractionSnapshot = new TicTacToePlayer[][]{
        new TicTacToePlayer[]{playerI,      null,       playerII},
        new TicTacToePlayer[]{playerI,      playerII,   null},
        new TicTacToePlayer[]{playerI,      playerII,   playerI}            
    };

    ticTacTocGame.start();
    ticTacTocGame.makeMove(new TicTacToeMove(0, 0));
    ticTacTocGame.makeMove(new TicTacToeMove(1, 1));
    ticTacTocGame.makeMove(new TicTacToeMove(2, 2));
    ticTacTocGame.makeMove(new TicTacToeMove(0, 2));
    ticTacTocGame.makeMove(new TicTacToeMove(2, 0));
    ticTacTocGame.makeMove(new TicTacToeMove(2, 1));
    ticTacTocGame.makeMove(new TicTacToeMove(1, 0));

    this.displayBoardPlayerInteractionSnapshot(ticTacTocGame);
    final TicTacToePlayer[][] actualBoardInteractionSnapshot = ticTacTocGame.getPlayerInteractionSnapshotBoard();

    Assert.assertFalse(ticTacTocGame.isGameDraw());
    Assert.assertTrue(ticTacTocGame.existWinner());
    Assert.assertEquals(ticTacTocGame.getWinner().getDisplayName(), playerIDisplayName);
    Assert.assertArrayEquals(actualBoardInteractionSnapshot, expectedBoardInteractionSnapshot);     
}
public void testWinnerPlayerWithVerticalline(){
displayUnitTestDescription(“将玩家“X”作为垂直线用例的赢家的集成测试”);
最终IBoardGame ticTacTocGame=新的ticTacTocGame();
最终字符串playerDisplayName=“X”;
最终字符串播放器IDisplayName=“O”;
最终TictoEplayer playerI=新TictoEplayer(PlayerDisplayName);
最终TictoEplayer playerII=新TictoEplayer(PlayerIdDisplayName);
setGamePlayers(新的TicTacToePlayer[]{playerI,playerI});
TicTacToePlayer[]expectedBoardInteractionSnapshot=新的TicTacToePlayer[]{
新的TictoEplayer[]{playerI,null,playerI},
新的TictoEplayer[]{playerI,playerI,null},
新TictoEplayer[]{playerI,playerI,playerI}
};
ticTacTocGame.start();
makeMove(新的TicTacToeMove(0,0));
ticTacTocGame.makeMove(新的TicTacToeMove(1,1));
ticTacTocGame.makeMove(新的TicTacToeMove(2,2));
ticTacTocGame.makeMove(新的TicTacToeMove(0,2));
TictActoGame.makeMove(新的TictActoMove(2,0));
TictActoGame.makeMove(新的TictActoMove(2,1));
ticTacTocGame.makeMove(新的TicTacToeMove(1,0));
这个.displayBoardPlayerInteractionSnapshot(TictocGame);
最终TictoEplayer[]actualBoardInteractionSnapshot=TictocGame.getPlayerInteractionSnapshotBoard();
Assert.assertFalse(ticTacTocGame.isGameDraw());
Assert.assertTrue(ticTacTocGame.existWinner());
Assert.assertEquals(ticTacTocGame.getWinner().getDisplayName(),playerDisplayName);
AssertArrayQuals(实际BoardInteractionSnapshot,expectedBoardInteractionSnapshot);
}

如果您有兴趣了解更多信息,您可以找到TictaToe游戏的整个java实现:

谢谢,我确实把自己弄糊涂了。我们的任务不需要创建私有和公共方法。感谢您的解释,我在设置board[I][j]值时出错,但我认为这是因为文本文件中输入的一些游戏不是3x3板。不过我还是让它工作了。@user1327636,如果它们不都是3x3板的话?他们至少是NxN吗?如果木板不是正方形的,那么你怎么知道一排的末尾?是否明确提供了它们的大小?