最近,我一直在开发我的 Noughts and Crosses 游戏,但我偶然发现了一个我似乎无法解决的问题。
每当我第一次输入一些东西时,它就会工作并打印出电路板,但是,从那时起,它就停止检查和打印电路板。
while(inf){
String temp = input.next();
if(playerTurn == 1){
if(amountP1 <= 3){
if(temp.equalsIgnoreCase("00")){
if(board[0][0] == 0){
board[0][0] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("01")){
if(board[0][1] == 0){
board[0][1] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("02")){
if(board[0][2] == 0){
board[0][2] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("10")){
if(board[1][0] == 0){
board[1][0] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("11")){
if(board[1][1] == 0){
board[1][1] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("12")){
if(board[1][2] == 0){
board[1][2] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("20")){
if(board[2][0] == 0){
board[2][0] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("21")){
if(board[2][1] == 0){
board[2][1] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("22")){
if(board[2][2] == 0){
board[2][2] = 1;
showBoard(board);
playerTurn = 2;
amountP1++;
}
else{
System.out.println("This space is already occupied!");
}
}
}
else if(playerTurn == 2){
if(amountP2 <= 3){
if(temp.equalsIgnoreCase("00")){
if(board[0][0] == 0){
board[0][0] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("01")){
if(board[0][1] == 0){
board[0][1] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("02")){
if(board[0][2] == 0){
board[0][2] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("10")){
if(board[1][0] == 0){
board[1][0] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("11")){
if(board[1][1] == 0){
board[1][1] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("12")){
if(board[1][2] == 0){
board[1][2] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("20")){
if(board[2][0] == 0){
board[2][0] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("21")){
if(board[2][1] == 0){
board[2][1] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
else if(temp.equalsIgnoreCase("22")){
if(board[2][2] == 0){
board[2][2] = 2;
showBoard(board);
playerTurn = 1;
amountP2++;
}
else{
System.out.println("This space is already occupied!");
}
}
}
}
}
}
这是打印板的方法:
public static void showBoard(int x[][]){
int rowNumber = 1;
for(int row = 0 ; row < x.length ; row++){
for(int column = 0 ; column < x[row].length ; column++){
if(x[row][column] == 0){
if(rowNumber <= 2){
System.out.print(" E");
rowNumber++;
}
else if(rowNumber == 3){
System.out.println(" E");
rowNumber = 1;
}
}
else if(x[row][column] == 1){
if(rowNumber <= 2){
System.out.print(" X");
rowNumber++;
}
else if(rowNumber == 3){
System.out.println(" X");
rowNumber = 1;
}
}
else if(x[row][column] == 2){
if(rowNumber <= 2){
System.out.print(" O");
rowNumber++;
}
else if(rowNumber == 3){
System.out.println(" O");
rowNumber = 1;
}
}
}
}
}
另外,我知道这段代码是一团糟,可能还有很多其他更有效的方法可以做到这一点,但是由于我是一个相当新的程序员,所以我选择使用这个功能而不是效率。