帮助基本的tic tac toe程序

我的生活从未像现在这样沮丧过。 甚至不能在这里做基础…只需要做一个简单的tic tac toe程序。 我现在在这个世界上感到很孤单..我得到了基本的想法,但不能逻辑地把它放在一起。

类实例变量:

  • private char [] [] board; 私人信件
  • 玩家; //’X’或’O’

方法:

  • 公共TicTacToe()
  • public void print()
  • public boolean play(String s)
  • public boolean won()
  • public boolean stalemate()

这是我的代码:

import java.util.Scanner; public class Six1 { public static void main(String[] args) { TicTacToe ttt = new TicTacToe(); ttt.TicTacToe(); ttt.print(); } static class TicTacToe { private char player; // 'X' or 'O' private char[][] board; // make board public TicTacToe() { // construct board board = new char[3][3]; // initialize elements for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = ' ' ; } } } // print board public void print() { for ( int i = 0; i < 3; i++) { System.out.println(" "); for( int j = 0; j < 3; j++) { System.out.print(board[i][j] + " "); } System.out.println("\n------"); } } } } 

你还没有完成很多工作,但你所拥有的东西似乎最合理。 使用内部类可能会使事情变得过于复杂。 如果你想要发生什么,你需要把东西放在main东西。

你不能一次编写整个程序。 您既可以从顶部开始,也可以查看详细信息,或者处理细节,然后将它们组合成一个整体。

自上而下工作

如果您不确定从哪里开始,这可能是让事情发生变化的好方法。 使用您希望存在的任何函数编写代码的主体。 也许你会开始

 public static void main(String[] args) { printBoard(); while (!isWinner()) { readMove(); // get move from stdin and mark on board printBoard(); // redraw board } printWinner(); // say who won } 

这些function尚不存在。 获得主要级别后,开始实现这些组成的function,必要时使用更多的组合function。 重复,直到您了解到您知道如何实现的简单function。

如果要在不实现每个方法的情况下编译代码,可以使用throw new UnsupportedOperationException("not implemented"); 作为需要返回值的任何方法的主体。

自下而上工作

如果你知道你需要某些部件,但不确定它们如何组合在一起,那就从这个方法开始吧。

你知道你需要某种方式来询问用户他们想要做出什么样的举动。 因此,创建一个能够执行此操作并在其上自行测试的函数。 你知道你需要一种方法来检查是否有赢家。 将一些值硬编码到board[]并测试你的isWinner()函数。 一旦你有了一些工件,你可以将它们组装成越来越大的块,直到你有一个正常运行的程序。

你说你不知道主要做什么:

 public static void main(String[] args) { // what the hell do i need here?!?! // TicTacToe() <--??? // print() <-???? } 

简单地调用这些方法不会有两个原因:

  1. main是一个静态方法,它在没有对象实例的情况下运行。 您要调用的方法是实例方法,即您需要一个对象来调用它们。

  2. 您已经定义了一个内部类。 这些方法是内部类的一部分。

当然,你可以解决这两个问题,尽管正如bemace所说,2。使你的程序比必要的更复杂。 你可以放弃“class TicTacToe”定义。 如果你想保留它,你可以创建一个它的实例,如下所示:

 TicTacToe ttt = new TicTacToe(); 

编辑:请注意,正如NamshubWriter所评论的那样,除非您将内部类声明为static否则这将无效:

static class TicTacToe

然后你可以调用它上面的方法:

 ttt.print(); 

你试图调用的TicTacToe()实际上是一个构造函数。 当您执行new TicTacToe()以创建新对象时,会自动调用它。

如果将上面两行放在main方法中,则应该更进一步。

令人困惑的一件事是内部/嵌套类。 我假设作业要求你有一个名为Six1的类。 我会让TicTacToe成为顶级课程。 所以你在同一目录中有两个源文件:

开始Six1.java

 public class Six1 { public static void main(String[] args) TicTacToe ttt = new TicTacToe(); ttt.play(); } } 

结束Six1.java

开始TicTacToe.java

 public class TicTacToe { private char[][] board; private char player; // 'X' or 'O' /** * Constructs a new Tic Tac Toe object */ public TicTacToe() { // initialize board } /** * Plays a game */ public void play() { // play the game print(); } /** * Prints the board */ public void print() { } } 

结束TicTacToe.java

打破这个问题,尽量不要整体解决它。

现在专注于测试胜利的方法。 你希望函数在每个实例中返回什么(Draw,未完成的游戏,玩家1赢或玩家2赢了)?

你将如何测试这场胜利?

一旦你有了这种方法,其余的应该更容易适应。

这就是我想出的。 如果有人仍然感兴趣,希望这会有所帮助!

 public class TicTacToe { static int SIZE = 3; static int MAX_MOVES = SIZE * SIZE; static char[][] matrix = new char[SIZE][SIZE]; static int[][] moveMatrix = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; public static void printState() { System.out.println("Current State:"); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) if ((matrix[i][j] == 'x') || (matrix[i][j] == 'o')) System.out.print(" " + matrix[i][j]); else System.out.print(" -"); System.out.println(); } } public static int enterMove() { System.out.println("Enter your move as follows:"); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) if ((matrix[i][j] == 'x') || (matrix[i][j] == 'o')) System.out.print(" -"); else System.out.print(" " + moveMatrix[i][j]); System.out.println(); } System.out.println("Enter your move :"); int move = 0; move = Integer.parseInt(System.console().readLine()); return move; } public static boolean updateMatrix(int move, char ch) { int i = 0; int j = 0; boolean status; switch (move) { case 1: break; case 2: i = 0; j = 1; break; case 3: i = 0; j = 2; break; case 4: i = 1; j = 0; break; case 5: i = 1; j = 1; break; case 6: i = 1; j = 2; break; case 7: i = 2; j = 0; break; case 8: i = 2; j = 1; break; case 9: i = 2; j = 2; break; default: System.out.println("Invalid entry"); status = false; break; } if (moveMatrix[i][j] == -1) { System.out .println("Entry already marked,please re-enter your move."); status = false; } else { matrix[i][j] = ch; moveMatrix[i][j] = -1; status = true; } return status; } public static boolean isGameOver() { boolean status = false; if ((matrix[0][0] == matrix[1][1]) && (matrix[1][1] == matrix[2][2]) && ((matrix[1][1] == 'x') || (matrix[1][1]) == 'o')) status = true; else if ((matrix[2][0] == matrix[1][1]) && (matrix[1][1] == matrix[0][2]) && ((matrix[1][1] == 'x') || (matrix[1][1]) == 'o')) status = true; for (int i = 0; i < SIZE; i++) if ((matrix[i][0] == matrix[i][1]) && (matrix[i][1] == matrix[i][2]) && ((matrix[i][1] == 'x') || (matrix[i][1]) == 'o')) status = true; else if ((matrix[0][i] == matrix[1][i]) && (matrix[1][i] == matrix[2][i]) && ((matrix[1][i] == 'x') || (matrix[1][i]) == 'o')) status = true; return status; } public static void main(String[] args) { char ch; int k = 0; int player; do { player = k % 2 + 1; k++; if (player == 1) ch = 'x'; else ch = 'o'; printState(); System.out.println("Player" + player + " [" + ch + "]:"); if (!updateMatrix(enterMove(), ch)) k--; } while ((!isGameOver()) && (k < MAX_MOVES)); printState(); if (isGameOver()) System.out.println("Game Over. Player" + player + " [" + ch + "] wins!"); else System.out.println("Game Tied. No winner!"); } }