2017-06-21 35 views
0

你好我有一個問題,我想嘗試和做的井字遊戲。我仍然是一個初學者,所以請隨時提供有關組織和類似的提示,但我的問題是,我的方法,checkRowWin,checkColoumnWin和E.T.C似乎並沒有工作時,加在一起。它們似乎都是單獨工作的,當加在一起時,被稱爲「最後一個」的是唯一有效的工具。我似乎無法自己想出來。爪哇井字檢查贏家方法不一起工作

import java.util.*; 
public class TicTacToeATREU { 
    public static final int BOARD_SIZE = 3; 
    public static final String PLAYER_ONE = "x"; //Sets piece to corresponding player 
    public static final String PLAYER_TWO = "o"; 
    public static void main (String []args) { 
     Scanner console = new Scanner(System.in); 
     String[][] gameBoard = new String[BOARD_SIZE][BOARD_SIZE]; 
     String playerTurn = PLAYER_ONE; 
     boolean gameWon = false; 
     intro(); 
     playGame(gameBoard,console,playerTurn,gameWon); 
    } 
    public static void fillBoard(String[][] gameBoard) { //Fills board with all underscores 
     for(int i = 0; i < gameBoard.length; i++) { 
     for(int j = 0; j < gameBoard[i].length; j++) { 
      gameBoard[i][j] = "_"; 
     } 
     } 
    } 
    public static void printBoard(String[][] gameBoard) { //Prints gameBoard array so that it looks like a tic tac toe board 
     for(int i = 0; i < gameBoard.length; i++) { 
     String[] tempArray = gameBoard[i]; 
     for(int j = 0; j < tempArray.length; j++) { 
      System.out.print(tempArray[j] + " "); 
     } 
     } 
    } 
    public static void intro(){ //Intro Message to let brief players on rules 
     System.out.println("This program allows you to play a game of Tic-Tac-Toe."); 
     System.out.println("Each player will be prompted for the location"); 
     System.out.println("to place their piece. When one player has filled"); 
     System.out.println("an entire row, column, or diagonal the game is won."); 
     System.out.println(); 
     System.out.println("Player 1, you will be x"); 
     System.out.println("Player 2, you will be o"); 
     System.out.println(); 
    } 
    public static void takeTurn(String[][] gameBoard,Scanner console, String playerTurn) { //Method for placing piece each turn into board and then reprinting board 
     if(playerTurn == PLAYER_ONE) { // Tests to see which player is making move 
     System.out.println(); 
     System.out.println("Make your move player 1:"); 
     } 
     else { 
     System.out.println(); 
     System.out.println("Make your move player 2:"); 
     } 
     System.out.print("What row? "); 
     int rowMove = console.nextInt(); 
     System.out.print("What column? "); 
     int columnMove = console.nextInt(); 
     if(playerTurn == PLAYER_ONE) { //test to see which player is making move and places corresponding piece 
     gameBoard[rowMove-1][columnMove-1] = PLAYER_ONE; 
     } 
     else { 
     gameBoard[rowMove-1][columnMove-1] = PLAYER_TWO; 
     } 
     System.out.println(); 
     printBoard(gameBoard); 
    } 
    public static String playGame(String[][] gameBoard, Scanner console, String playerTurn, boolean gameWon) { //Method for playing one game of tic tac toe 
     //boolean gameWon = false; 
     int currentPlayer = 1; //this currentPlayer variable is just to offset the game reporting the congratulations method for the losing player 
     fillBoard(gameBoard); 
     printBoard(gameBoard); 
     while(gameWon == false) { //while this loop is false, the game will play 
     takeTurn(gameBoard,console,playerTurn); 
     if(checkRowWin(gameBoard,playerTurn) = true) { 
      gameWon = true; 
     } 
     gameWon = checkRowWin(gameBoard,playerTurn); 
     gameWon = checkColoumnWin(gameBoard,playerTurn); 
     gameWon = checkDiagonalWin(gameBoard,playerTurn); 
     gameWon = checkTie(gameBoard,playerTurn); 
     if(playerTurn == PLAYER_ONE) { //for switching players after every call of take turn method 
      playerTurn = PLAYER_TWO; 
      currentPlayer = 1; 
     } 
     else { 
      playerTurn = PLAYER_ONE; 
      currentPlayer = 2; 
     } 
     } 
     System.out.println(); 
     System.out.println("Congratulations Player " + currentPlayer + "! You win!"); 
     return playerTurn; 
    } 
    public static boolean checkRowWin(String[][] gameBoard, String playerTurn) { //checks for three pieces in a row to decide if the game has been won 
     boolean rowWin = true; 
     for(int i = 0; i < gameBoard.length; i++) { 
     String[] tempArray = gameBoard[i]; 
     for(int j = 0; j < tempArray.length; j++) { 
      if(!tempArray[j].equals(playerTurn)) { 
       rowWin = false;    
      } 
     } 
     if(rowWin == true) { 
      return true; 
     } 
     else { 
      rowWin = true; 
     } 
     } 
     return false; 
    } 
    public static boolean checkColoumnWin(String[][] gameBoard, String playerTurn) { //checks for three pieces in a coloumn to decide if the game has been won 
     boolean coloumnWin = true; 
     for(int i = 0; i < gameBoard.length; i++) { 
     for(int j = 0; j < gameBoard.length; j++) { 
      if(!gameBoard[j][i].equals(playerTurn)) { 
       coloumnWin = false; 
      } 
     } 
     if(coloumnWin == true) { 
      return true; 
     } 
     else { 
      coloumnWin = true; 
     } 
     } 
     return false; 
    } 
    public static boolean checkDiagonalWin(String[][] gameBoard, String playerTurn) { //checks for three pieces in a diagonal of the board to decide if the game has been won 
     boolean diagonalWin = true; 
     for(int i = 0; i < gameBoard.length; i++) { 
     if(!gameBoard[i][i].equals(playerTurn)) { 
      diagonalWin = false; 
     } 

     } 
     if(diagonalWin == true) { 
     return true; 
     } 
     else { 
     diagonalWin = true; 
     }  
     for(int j = 0; j < gameBoard.length; j++) { 
     int row = j; 
     int coloumn = BOARD_SIZE-1-j; 
     if(!gameBoard[row][coloumn].equals(playerTurn)) { 
      diagonalWin = false; 
     } 
     } 

     if(diagonalWin == true) { 
     return true; 
     } 
     else { 
     diagonalWin = true; 
     } 
     return false; 
    } 
    public static boolean checkTie(String[][] gameBoard,String playerTurn) { 
     boolean test = true; 
     for(int i = 0; i < BOARD_SIZE; i++) { 
     for(int j = 0; j < BOARD_SIZE; j++) { 
      if(gameBoard[i][j].equals("_")) { 
       test = false; 
      } 

     } 
     } 
     return test; 
    } 
} 
+4

*「歡迎免費提供技巧「* - 學習不用靜態生活 – MadProgrammer

+0

並學習不要使用==來比較字符串值。在這個特定的程序中它可能工作正常,但通常不會。 –

+0

@MadProgrammer - 可能適合自己的問題,但爲什麼沒有靜態地生活? – 2017-06-21 23:13:50

回答

3

在你的代碼

gameWon = checkRowWin(gameBoard,playerTurn); 
gameWon = checkColoumnWin(gameBoard,playerTurn); 
gameWon = checkDiagonalWin(gameBoard,playerTurn); 
gameWon = checkTie(gameBoard,playerTurn); 

意味着,前三行的結果被丟棄,並且通過任何checkTie(覆蓋)返回 - 即如果checkRowWin()返回true,那麼沒有什麼是完成了它。

該代碼還有其他可能最適合https://codereview.stackexchange.com/的問題。

0

此行應該使用的比較,而不是分配(或只是刪除了比較完全,因爲它計算爲一個布爾值不管):

if(checkRowWin(gameBoard,playerTurn) = true) { 

在這裏,您需要確保不覆蓋gameWon

 gameWon = checkRowWin(gameBoard,playerTurn); 
    gameWon = checkColoumnWin(gameBoard,playerTurn); 
    gameWon = checkDiagonalWin(gameBoard,playerTurn); 
    gameWon = checkTie(gameBoard,playerTurn); 

最快的解決辦法是:

gameWon = checkRowWin(gameBoard,playerTurn)  || 
      checkColoumnWin(gameBoard,playerTurn) || 
      checkDiagonalWin(gameBoard,playerTurn) || 
      checkTie(gameBoard,playerTurn);