这是我的代码。我正在努力检查赢家。我只是一个初学者,所以请放轻松。我希望董事会改变尺寸。所以,我希望获胜者的支票可以使用到大小,它不会只检查9块。TicTacToe Java - 检查赢家
import java.util.*;
public class TicTacToe {
private String[][] board;
private Scanner console;
public TicTacToe(String[][] table, Scanner console) {
this.board = table;
this.console = console;
}
public void makeTable() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = "_";
}
}
}
public void printTable() {
System.out.print(" ");
for (int i = 0; i < board.length; i++) {
System.out.print(" " + i);
}
System.out.println();
for (int i = 0; i < board.length; i++) {
System.out.print(i + "│");
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j] + "│");
}
System.out.println();
}
}
public void play(Scanner console) {
int turn = 0;
String player = "_";
makeTable();
printTable();
while (turn != 9) {
int x = console.nextInt();
int y = console.nextInt();
while (x >= board.length || y >= board[1].length) {
System.out.println("Out of bounce, try again!!!");
x = console.nextInt();
y = console.nextInt();
}
while (board[y][x] != "_") {
System.out.println("Occupied, try again!!!");
x = console.nextInt();
y = console.nextInt();
}
if (turn % 2 == 0) {
player = "X";
} else {
player = "O";
}
board[y][x] = player;
turn++;
printTable();
}
}
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
String[][] board = new String[3][3];
TicTacToe ttt = new TicTacToe(board, console);
ttt.play(console);
}
}
对于初学者,您有一个很有希望的编码风格。尼斯。关于检查,你有没有试图做到这一点,如果是的话,你可以包括你的尝试代码? –
[SO不适用于代码转储](http://meta.stackexchange.com/questions/88842/discourage-code-dumps)。请描述你的问题,你采取的措施来解决它,等等。 –
@DaveNewton我给了他一个非常非常普遍的解决方案。没有代码,只是算法。 –