Java(已编辑) - 为什么我的TicTacToe游戏的获胜方法不被承认?

时间:2015-10-02 01:59:46

标签: java arrays

我完全改变了我的代码,用于检查行,列和两个对角线中的获胜但是当我运行程序时,获胜者不会被确认。

这是我检查获胜者,显示棋盘,添加移动,在玩家之间切换以及重新启动的方法。我遇到了一个问题,因为当我运行程序时,它不会在玩家之间正常切换。

import java.util.*;
public class TicTacToe {
   private char[][] gameBoard = new char[3][3]; //stores the game as a 2D array in which each element is a character
   private char currentMark;
   private Scanner input;
   public TicTacToe() { //initializes the board
      currentMark = 'X';
      for (int i=0; i<3; i++) { //for every row
         for (int j=0; j<3; j++) { //for every column in that row
            gameBoard[i][j] = ' '; //set the value of each element to an empty string
         }

      }
      input = new Scanner(System.in);
   }


   public void whoseTurn() {
      if (currentMark == 'X') {
         currentMark = 'O';
         System.out.println("It is your turn, player O");
      }
      else {
         currentMark = 'X';
         System.out.println("It is your turn, player X");
      }    
   }

   public boolean checkRows() {
      boolean rowWin=false;
      for (int i=0;i<3;i++){ 
         if ((gameBoard[i][0] == gameBoard[i][1]) && (gameBoard[i][1] == gameBoard[i][2]) && (gameBoard[i][0] == currentMark)) { //check every row to find a match
            rowWin= true;
         }
      }
      return rowWin;
   }

   public boolean checkColumns() {
      boolean columnWin=false;
      for (int i=0;i<3;i++) {
         if ((gameBoard[0][i] == gameBoard[1][i]) && (gameBoard[1][i] == gameBoard[2][i]) && (gameBoard[0][i] == currentMark)) { //checks every column to find a match
            columnWin= true;
         }
      }
      return columnWin;
   }

   public boolean checkDiag1() {
     boolean diag1win=false;
     for (int i=0;i<3;i++) {
         if ((gameBoard[0][0] == gameBoard[1][1]) && (gameBoard[1][1] == gameBoard[2][2]) && (gameBoard[0][0] == currentMark)) { //checks first diagonal
            diag1win= true;
         }
      }
      return diag1win;
   }

   public boolean checkDiag2() {
      boolean diag2win=false;
      for (int i=0;i<3;i++) {
         if ((gameBoard[0][2] == gameBoard[1][1]) && (gameBoard[1][1] == gameBoard[2][0]) && (gameBoard[0][2] == currentMark)) { //checks second diagonal
            diag2win= true;   
         }
      }
      return diag2win;
    }

   public boolean checkWinner() {
     boolean yesWinner = false;
     if (checkRows() || checkColumns() || checkDiag1() || checkDiag2()) {
         yesWinner = true;
     }
     return yesWinner;
   }

   public boolean addMove() { 
      boolean nonacceptable = true;
      int row;
      int column;
      while (nonacceptable) {
         System.out.println("Which row and column would you like to enter your mark? Enter the row and column between 0 and 2 separated by a space.");
         row = input.nextInt();
         column = input.nextInt();
         if ((row >= 0 && row <=2) && (column >= 0 && column <=2)) { //make sure user entered a number between 0 and 2
            if (gameBoard[row][column] != ' ') {
               System.out.println("Sorry, this position is not open!");
            }
            else {
               gameBoard[row][column] = currentMark;
               nonacceptable = false;
            }
         }   
         else 
            System.out.println("That position is not between 0 and 2!");
         }
         return nonacceptable;     
   }
   public void restart() {
      for (int i = 0; i<3; i++) {
         for (int j=0; j<3; j++) {
            gameBoard[i][j] = ' ';
         }
      }
   }
   public boolean boardFull() {
      boolean notFull = true;
      for (int i=0; i<3;i++) {
         for (int j=0; j<3; j++) {
            if (gameBoard[i][j] != ' ') 
               notFull = false;
         } 
      }
      return notFull;
   }

   public void letsPlay() {
      while (true) {
         displayBoard();
         gameOptions();
         int choice = input.nextInt();
         if (choice == 1) {
            if (!addMove()) {
               if (checkWinner()) 
                  System.out.println(currentMark + "wins!");
               else continue;
               displayBoard();               
               whoseTurn();
               //System.exit(0);
             }


          else if (boardFull()) {
               displayBoard();
               System.out.println("Board full!");
               System.exit(0);
            } 
           else {
               whoseTurn();
            } 
         }
         else if (choice == 2) 
            restart();
         else if (choice == 3) 
            System.exit(0);

        /* else 
            System.out.println("Try again!"); */
      }
   }

   public static void main(String[] args) {
      TicTacToe game = new TicTacToe();
      game.letsPlay();
   }

}

2 个答案:

答案 0 :(得分:0)

我认为addMove中存在问题。在while循环中,此代码表示接受移动,然后继续。

else {
           gameBoard[row][column] = currentMark;
           nonacceptable = false;
        }

然后你返回nonacceptable,这是假的。但是,你的游戏方法是:

if (addMove()) { 
           if (checkWinner()) 
              System.out.println(currentMark + "wins!");
           displayBoard();               
           whoseTurn();
           System.exit(0);
        }  

addMove返回false时,它将无法进入if。更好的改变:

if (!addMove()) { 
               if (checkWinner()) 
                  System.out.println(currentMark + "wins!");
               displayBoard();               
               whoseTurn();
               System.exit(0);
            } 

还有一个,在checkWin中,当您比较单元格时,请注意这里,因为空行将返回true。您应该将每个单元格与currentMark

进行比较

好的,先说一下,你的功能检查了什么。我们调查checkColumns,它将返回true。因为gameBoard[0][0] = gameBoard[1][0] = gameBoard[2][0] =&#34; &#34 ;.这就是为什么你应该与你当前的标记进行比较的原因。您有2个选项,将当前标记传递给函数并进行检查,或使变量currentMark全局

该计划不会停止,因为当有胜利者时,你没有做任何事情来阻止它。这应该做:

if (!addMove()) { 
           if (checkWinner()) {
              System.out.println(currentMark + "wins!");
              //put the code to stop the program here
              //like return or exit
           }
           displayBoard();               
           whoseTurn();
           System.exit(0);
        }  

看看你的输出,在我看来你的whoseTurn从未被执行过。因为我无法分辨的原因。你能提供更多的代码吗,特别是你的游戏循环开始的地方

你的代码有很多小问题。在checkDiagonal中,我认为这里的for循环是不必要的,因为只有1个第一对角线和1个第二对角线。

问题是您的方法whoseTurn没有按预期行事。所以我需要看看它是如何在你的代码中调用的。有了这个,我无法看到问题所在。您可以尝试调试,在whoseTurn或一些输出中放置一个断点,以查看是否调用了此函数。

所以我是怀疑,这是你的游戏循环,方法letsPlay这是错误的。在if语句中,当检查是否有胜利者时,在其他地方,continue将使代码立即返回while,忽略其余代码。这就是whoseTurn被忽略的原因。这应该做:

public void letsPlay() {
        displayBoard();

        while (true) {
            gameOptions();
            int choice = input.nextInt();
            if (choice == 1) {
                if (!addMove()) {
                    displayBoard();

                    if (checkWinner()){
                        System.out.println(currentMark + "wins!");
                        return;
                    }
                    whoseTurn();
                    // System.exit(0);
                }

                else if (boardFull()) {
                    displayBoard();
                    System.out.println("Board full!");
                    System.exit(0);
                } else {
                    displayBoard();

                    whoseTurn();
                }
            } else if (choice == 2)
                restart();
            else if (choice == 3)
                System.exit(0);

            /*
             * else System.out.println("Try again!");
             */
        }
    }

我检查并发挥好。

答案 1 :(得分:0)

您应该考虑删除boolean方法上的addMove()返回值,移除addMove()中的return语句,将返回值更改为void,然后更新播放方法代码:

addMove(); 
if (checkWinner()) {
    System.out.println(currentMark + "wins!");
}
displayBoard();               
whoseTurn();
System.exit(0);

boolean中的addMove()作为控制while循环内迭代的守卫,我认为你会非常接近你想要的。