2016-09-22 46 views
0

我一直在努力让我的win checker方法适用于我的Connect4代码。我的“工作”我的意思是游戏无法识别win配置,只会在所有空格填满时才会结束赢取检查条件连接4(爪哇)

我认为我的算法出了问题,所以我确定如果我能弄清楚什么是错误的其中之一,那么我应该能够解决所有这些问题。

我想要这些方法返回一个布尔值,它将声明某人是否赢了。

这里是我的全码:

public class ConnectFour 
{ 
    private static int rowMax = 7; 
    private static int colMax = 8; 
private static String boardFill = "| |"; 

public static void main(String[] args) 
{ 
    String playerX = choosePlayerName("X").toUpperCase(); 
    String playerO = choosePlayerName("O").toUpperCase(); 
    String[][] players = {{playerX, playerO},{"X","O"}}; 

    String endChoice; 
    do 
    { 
     int playerTurn = pickFirstPlayer(players); 

     String board[][] = createBoard(rowMax, colMax, boardFill); 
     printBoard(board); 

     int turnCount = 0; 
     boolean gameOver = false; 
     while (gameOver == false) 
     { 
      showPlayerTurn(players, playerTurn); 

      int rowChosen = -1; 
      int colChosen = -1; 
      do 
      { 
       colChosen = chooseCol(colMax); 
       rowChosen = findRowIndex(board, colChosen); 
      } 
      while (isValidMove(board, rowChosen) == false); 

      board[rowChosen][colChosen] = ("|"+players[1][playerTurn]+"|"); 
      printBoard(board); 

      playerTurn = changePlayerTurn(playerTurn); 
      turnCount++; 

      gameOver = checkWinRows(board, rowMax, colMax); 
      gameOver = checkWinVertical(board, rowMax, colMax); 
      gameOver = checkWinFSlash(board, rowMax, colMax); 
      gameOver = checkWinBSlash(board, rowMax, colMax); 
      gameOver = checkMaxTurnCount(turnCount, rowMax, colMax); 

     } 

     if (checkMaxTurnCount(turnCount, rowMax, colMax)) 
     { 
      System.out.println("\n\nMaximum number of moves reached, it's a draw."); 
     } 
     else 
     { 
      System.out.println("\nPlayer"+players[0][playerTurn]+" Wins!"); 
     } 
     endChoice = checkQuitOrRestart().toUpperCase(); 
     endQuit(endChoice); 
    } 
    while (endChoice.equals("R")); 
} 

// Player Instantiate Methods 
public static String choosePlayerName(String playerSymbol) 
{ 

    @SuppressWarnings("resource") 
    Scanner nameInput = new Scanner(System.in); 
    System.out.println("\nYour Symbol is "+playerSymbol+". Please Enter Player's Name: "); 
    String playerName = nameInput.nextLine(); 

    return playerName; 
} 
public static int pickFirstPlayer(String[][] players) 
{ 
    return (int) Math.round(Math.random()); 

} 
// Board Create/Print Methods 
public static String[][] createBoard(int rowMax, int colMax, String boardFill) 
{ 
    { 
     String board[][] = new String[rowMax][colMax]; 

     for (int row = 0; row < rowMax; row++) 
     { 
      for (int col = 0; col < colMax; col++) 
      { 
       board[row][col] = boardFill; 
      } 
     } 
     return board; 
    } 
} 
public static void printBoard(String[][] board) 
{ 
    System.out.print("\n 1 2 3 4 5 6 7 8"); 
    for (int row = 0; row < rowMax; row++) 
    { 
     System.out.print("\n"); 
     for (int col = 0; col < colMax; col++) 
     { 
      System.out.print(board[row][col]);   
     } 
    } 
} 
// Player Turn Methods 
public static void showPlayerTurn(String players[][], int playerTurn) 
{ 
    System.out.println("\nPlayer's Turn: "+players[0][playerTurn]+" ["+players[1][playerTurn]+"]"); 
} 
public static int chooseCol(int colMax) 
{ 
    boolean isColValid; 
    int colChosen = -1; 

    do 
    { 
     isColValid = true; 
     @SuppressWarnings("resource") 
     Scanner colInput = new Scanner(System.in); 
     System.out.println("Choose a column to place your token [1-8]: "); 
     try 
     { 
      colChosen = colInput.nextInt(); 
      if (colChosen < 1 || colChosen > colMax) 
      { 
       isColValid = false; 
       System.out.println("Column out of bounds."); 
      } 
     } 
     catch (NumberFormatException e) 
     { 
      isColValid = false; 
      System.out.println("Enter valid number"); 
     } 
     catch (InputMismatchException e) 
     { 
      isColValid = false; 
      System.out.println("Enter column number as integer."); 
     } 
    } 
    while (!isColValid); 

    return (colChosen - 1); 
} 
public static int findRowIndex(String[][] board, int colChosen) 
{ 
    int rowChosen = -1; 

    for (int rowIndex = 0; rowIndex < board.length; rowIndex++) 
    { 
     if (board[rowIndex][colChosen] == boardFill) 
     { 
      rowChosen = rowIndex; 
     } 
    } 

    return rowChosen; 
} 
public static boolean isValidMove(String[][] board, int rowChosen) 
{ 
    boolean validMove; 

    if (rowChosen == -1) 
    { 
     System.out.print("Column full, please select valid column."); 
     validMove = false; 
    } 
    else 
    { 
     validMove = true; 
    } 

    return validMove; 
} 
public static int changePlayerTurn(int playerTurn) 
{ 
    if (playerTurn == 0) 
    { 
     playerTurn = 1; 
    } 
    else if (playerTurn ==1) 
    { 
     playerTurn = 0; 
    } 

    return playerTurn; 
} 
// Win/End Condition Check Methods 
// Win Check Methods  
public static boolean checkWinRows(String[][] board, int rowMax, int colMax) 
{ 
    boolean winRowCheck = false; 

    for (int row = 0; row < rowMax; row++) 
    { 
     for (int col = 0; col < (colMax - 3); col++) 
     { 
      if (board[row][col] == board[row][col + 1] && 
        board[row][col] == board[row][col + 2] && 
        board[row][col] == board[row][col + 3] && 
        board[row][col] != "| |") 
      { 
       winRowCheck = true; 
      } 
     } 
    } 

    return winRowCheck; 
} 
public static boolean checkWinVertical(String[][] board, int rowMax, int colMax) 
{ 
    boolean winVerticalCheck = false; 

    for (int row = 0; row < (rowMax - 3); row++) 
    { 
     for (int col = 0; col < (colMax - 3); col++) 
     { 
      if (board[row][col] == board[row + 1][col] && 
        board[row][col] == board[row + 2][col] && 
        board[row][col] == board[row + 3][col] && 
        board[row][col] != "| |") 
      { 
       winVerticalCheck = true; 
      } 
     } 
    } 

    return winVerticalCheck; 
} 
public static boolean checkWinFSlash(String[][] board, int rowMax, int colMax) 
{ 
    Boolean winFSlashCheck = false; 

    for (int row = 3; row < rowMax; row++) 
    { 
     for (int col = 0; col < (colMax - 3); col++) 
     { 
      if (board[row][col] == board[row - 1][col + 1] && 
        board[row][col] == board[row - 2][col + 2] && 
        board[row][col] == board[row - 3][col + 3] && 
        board[row][col] != "| |") 
      { 
       winFSlashCheck = true; 
      } 
     } 
    } 

    return winFSlashCheck; 
} 
public static boolean checkWinBSlash(String[][] board, int rowMax, int colMax) 
{ 
    boolean winBSlash = false; 

    for (int row = 4; row < rowMax; row++) 
    { 
     for (int col = 3; col < (colMax - 3); col++) 
     { 
      if (board[row][col] == board[row - 1][col - 1] && 
        board[row][col] == board[row - 1][col - 2] && 
        board[row][col] == board[row - 1][col - 3] && 
        board[row][col] != "| |") 
      { 
       winBSlash = true; 
      } 
     } 
    } 

    return winBSlash; 
} 
public static boolean checkMaxTurnCount(int turnCount, int rowMax, int colMax) 
{ 
    boolean maxTurnCountReached = false; 

    if (turnCount >= rowMax*colMax) 
    { 
     maxTurnCountReached = true; 
    } 

    return maxTurnCountReached; 
} 
// End Prompt Methods 
// End Game Methods 
public static String checkQuitOrRestart() 
{ 
    @SuppressWarnings("resource") 
    Scanner endChoiceImport = new Scanner(System.in); 
    System.out.println("\nPlease Select: Restart [R] or Quit [Q]"); 
    String endChoice = endChoiceImport.next(); 

    return endChoice; 
} 
public static void endQuit(String endChoice) 
{ 
    if (endChoice.equals("Q") || endChoice.equals("q")) 
    { 
     System.out.println("\nQuitting Program."); 
     System.exit(0); 
    } 
    else if (endChoice.equals("R")) 
    { 
     System.out.println("\nRestarting Program."); 
    } 
} 

这里的方法之一(checkWinRows):

public static boolean checkWinRows(String[][] board, int rowMax, int colMax) 
{ 
    boolean winRowCheck = false; 

    for (int row = 0; row < rowMax; row++) 
    { 
     for (int col = 0; col < (colMax - 3); col++) 
     { 
      if (board[row][col] == board[row][col + 1] && 
        board[row][col] == board[row][col + 2] && 
        board[row][col] == board[row][col + 3] && 
        board[row][col] != "| |") 
      { 
       winRowCheck = true; 
      } 
     } 
    } 

    return winRowCheck; 
} 

回答

0

首先,Strings需要使用equals方法进行比较,否则就会比较他们的引用而不是他们的内容。

例如,在checkWinRowsif声明是这样的:

if ((board[row][col].equals(board[row + 1][col])) 
    && (board[row][col].equals(board[row + 2][col])) 
    && (board[row][col].equals(board[row + 3][col])) 
    && (!board[row][col].equals("| |"))) { 
    winVerticalCheck = true; 
} 

然后,当你正在检查的gameOver,该变量由后续方法覆盖。你可以这样避免使用简单的检查,像这样:

gameOver = checkWinRows(board, rowMax, colMax); 
if(!gameOver) { 
    gameOver = checkWinVertical(board, rowMax, colMax); 
} 
if(!gameOver) { 
    gameOver = checkWinFSlash(board, rowMax, colMax); 
} 
... 

我没有看全源,从而有可能是其他错误或信息,您可以优化/改善(例如,你可以立即return true当你发现一场胜利,而不是总是完成整个周期),但这应该足以让你独立完成。

还记得在程序不像您期望的那样运行时总是使用调试器,它实际上是一个基本工具。像Eclipse这样的IDE总是有一个可以轻松使用的集成。

+0

太感谢你了,那解决的问题! –

0

首先,您使用==比较字符串。这不下面一行在这里工作,因为填充细胞没有编译时间常数,但串接在运行时:

board[rowChosen][colChosen] = ("|"+players[1][playerTurn]+"|"); 

而且您覆盖的gameOver的值,而不管之前的检查。

后者可以通过使用分离来固定,前者使用equals来比较值。

gameOver = checkWinRows(board, rowMax, colMax) 
      || checkWinVertical(board, rowMax, colMax) 
      || checkWinFSlash(board, rowMax, colMax) 
      || checkWinBSlash(board, rowMax, colMax) 
      || checkMaxTurnCount(turnCount, rowMax, colMax); 
public static boolean checkWinRows(String[][] board, int rowMax, int colMax) 
{ 
    for (int row = 0; row < rowMax; row++) 
    { 
     for (int col = 0; col < (colMax - 3); col++) 
     { 
      if (board[row][col] != boardFill && // this references the same object, if it's there, therefore != can be used here 
       board[row][col].equals(board[row][col + 1]) && 
       board[row][col].equals(board[row][col + 2]) && 
       board[row][col].equals(board[row][col + 3])) 
      { 
       return true; 
      } 
     } 
    } 

    return false; 
}