2013-08-01 22 views
0

我是新来的Java和工作在简单的任务 - TicTacToe控制台游戏。今天,我遇到了一个问题,我不能继续执行while循环后的主要方法。该项目有两个类别 - MainField。 Field对所有游戏领域的更新都有反应。我在调用Field方法时while循环的主要方法,但是当循环结果必须完成并且主要方法必须继续时(它应该询问用户是否想再次播放)。不幸的是,程序在执行while循环后停止。循环后不能继续主要方法

这里是我的代码:

import java.io.IOException; 
import java.util.Scanner; 

public class Main { 
    private static char playerSym, compSym; 
    private static int Sym; 
    public static int playerChoice; 
    public static boolean result; 

    public static void main(String args[]) throws IOException { 
     Field field = new Field(); 

     // start of the game 
     System.out.println("Let`s play"); 
     System.out.println("Choose your symbol please"); 
     System.out.println("0='O', 1='X'"); 
     Scanner sc = new Scanner(System.in); 
     Sym = sc.nextInt(); 

     while (Sym != 0 & Sym != 1) { 
      System.out 
        .println("The symbol you entered is incorrect, please repeat"); 
      System.out.println("0='O', 1='X'"); 
      Sym = sc.nextInt(); 
     } 

     // setting player character 
     if (Sym == 1) { 
      playerSym = 'X'; 
      compSym = 'O'; 
     } else if (Sym == 0) { 
      playerSym = 'O'; 
      compSym = 'Х'; 
     } 

     System.out.println("There is a game field"); 
     System.out.println("Please choose the cell number you`d like to fill with " + playerSym); 
     field.firstShowFields(); 

     do { 
      playerChoice = (Integer) sc.nextInt(); 
      field.updateFields(playerChoice, playerSym); 
      field.showFields(field.fields); 
     } while (result==false); 

     System.out.println("Want to play once more? Y-Yes, N-No"); 
     char answer = (char) System.in.read(); 

     switch (answer) { 
      case 'Y': 
       System.out.println("Restarting the game"); 
       break; 
      case 'N': 
       System.out.println("Thank you! Bye-Bye!"); 
       break; 
      default: 
       break; 
     } 
    } 
} 

public class Field { 
    public static final int FIELD_SIZE = 3; 
    // private static final char DEFAULT_CHAR=' '; 

    public char[][] fields; 
    public boolean result = true; 
    public char playerSym; 

    public Field() { 
     fields = new char[FIELD_SIZE][FIELD_SIZE]; 
    } 

    public void firstShowFields() { 
     int cellValue = 1; 
     for (int i = 0; i < FIELD_SIZE; i++) { 
      for (int j = 0; j < FIELD_SIZE; j++) { 
       System.out.print("[" + cellValue + "]"); 
       cellValue++; 
      } 
      System.out.println(); 
     } 
    } 

    public char[][] updateFields(int choice, char sym) { 
     playerSym = sym; 
     int cellValue = 1; 
     int playerChoice = choice; 

     do { 
      for (int i = 0; i < FIELD_SIZE; i++) { 
       for (int j = 0; j < FIELD_SIZE; j++) { 

        if (playerChoice == cellValue) { 
         fields[i][j] = (char) playerSym; 
        } else if (fields[i][j] == (char) playerSym) { 
         fields[i][j] = (char) playerSym; 
        } else { 
         fields[i][j] = (char) ('0' + cellValue); 
        } 

        cellValue++; 
       } 

      } 

      this.checkWin(fields, playerSym); 
      return fields; 

     } while (this.checkWin(fields, playerSym) == false); 
    } 

    public void showFields(char[][] fields) { 
     this.fields = fields; 
     for (int i = 0; i < FIELD_SIZE; i++) { 
      for (int j = 0; j < FIELD_SIZE; j++) { 
       System.out.print("[" + fields[i][j] + "]"); 
      } 
      System.out.println(); 
     } 
    } 

    public boolean checkWin(char[][] field, char playerSym) { 
     char[][] checkField = field; 
     this.playerSym = playerSym; 

     // checkline 
     if (((checkField[0][0] == checkField[0][1]) && (checkField[0][1] == checkField[0][2])) 
       || ((checkField[1][0] == checkField[1][1]) && (checkField[1][1] == checkField[1][2])) 
       || ((checkField[2][0] == checkField[2][1]) && (checkField[2][1] == checkField[2][2]))) { 

      System.out.println("The game is over. The winner is player " + playerSym); 
      return true; 
     } 
     // checkraw 
     else if (((checkField[0][0] == checkField[1][0]) && (checkField[1][0] == checkField[2][0])) 
       || ((checkField[0][1] == checkField[1][1]) && (checkField[1][1] == checkField[2][1])) 
       || ((checkField[0][2] == checkField[1][2]) && (checkField[1][2] == checkField[2][2]))) { 
      System.out.println("The game is over. The winner is player " + playerSym); 
      return result = true; 

     } // checkdiagonal 
     else if (((checkField[0][0] == checkField[1][1]) && (checkField[1][1] == checkField[2][2])) 
       || ((checkField[0][2] == checkField[1][1]) && (checkField[1][1] == checkField[2][0]))) { 
      System.out.println("The game is over. The winner is player " + playerSym); 
      return result = true; 
     } 
      return false; 
    } 
} 
+1

有很多while循环。你是指哪一个? – MightyPork

回答

2

这是一个无限循环:

do { 
     playerChoice = (Integer) sc.nextInt(); 
     field.updateFields(playerChoice, playerSym); 
     field.showFields(field.fields); 
    } while (result==false); 

result从不更新,因此while (result==false);绝不会只要它是真实的第一次失败。您可以尝试修改它象下面这样:

do { 
     playerChoice = (Integer) sc.nextInt(); 
     field.updateFields(playerChoice, playerSym); 
     field.showFields(field.fields); 
     result = field.checkWin(field.fields, playerSym); 
    } while (result==false); 

而且,它不是通过那些已经连接到实例的实例方法领域的一个很好的做法。您可以从checkWin方法中删除参数char[][] field,并简单地在实例变量fields上运行。但这不是你的循环问题的原因。

+0

非常感谢!当然现在正在工作。我可以在这里在stackoverflow上感谢你吗? –

+0

除了接受答案之外,我不知道任何其他方式,如果你喜欢,也许会给它一个upvote。无论如何,很高兴它的工作! – user506069

0

正如user506指出它不会经过循环的唯一原因是因为布尔result从未设置为true

为什么你有2个类来分离主要方法?

+0

cw1201,我不知道,那就是我设计的方式(我写到我对Java很陌生)。什么是更好的方法? –