我一直在努力让我的win checker方法在我的Connect4代码中工作。我的“工作”我的意思是游戏不能识别win配置,只有当所有空格都填满时才会结束
我认为我的算法有问题,所以我确信如果我能找出其中一个出了什么问题,那么我应该能够修复所有的问题。
我希望这些方法返回一个布尔值,它将声明是否有人赢了。
下面是我的完整代码:
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;
}发布于 2016-09-23 05:31:29
首先,需要使用equals方法比较Strings,否则您将比较它们的引用而不是它们的内容。
例如,checkWinRows中的if语句将如下所示:
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总是有一个集成的,您可以很容易地使用。
发布于 2016-09-23 05:31:39
首先,使用==比较字符串。这在这里不起作用,因为填充的单元格不是编译时间常量,而是在运行时在以下行中连接:
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;
}https://stackoverflow.com/questions/39648674
复制相似问题