gpt4 book ai didi

控制台中的 Java Connect Four - 水平和垂直获胜条件

转载 作者:行者123 更新时间:2023-11-30 07:02:35 28 4
gpt4 key购买 nike

我正在用 Java 为控制台开发一款四子棋游戏。我对获胜条件有疑问,因为我不知道如何对其进行编程。这是我的主要代码:

public class Main {

public static char[] playerNumber = new char[]{'1', '2'};
public static char[] Badge = new char[]{'X', 'O'};

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int moves = 7 * 6;
int whichPlayer = 0;

for (int i = 0; i < 10; i++) {
System.out.println(" FOUR IN A ROW");
System.out.println("-------------------------------------------------------");
System.out.println("Welcome to the amazing game Four In A Row:");
System.out.println("Enter a number between 0 and 6 for choosing a column.");
System.out.println();

Board board = new Board();
board.fillBoard();
board.presentBoard();

do {
// 1. get a badge
char Player = playerNumber[whichPlayer];
char badge = Badge[whichPlayer];

// 2. make a turn
board.makeTurn(badge, Player);
board.presentBoard();

// 3. Tjek om der er vinder
if (board.checkWinHorizontal() || board.checkWinVertical()) {
System.out.println("Player " + Player + " has won!");
break;
}

// 4. change the player
whichPlayer = 1 - whichPlayer;

// 5. decrease moves
--moves;

if (moves == 0) {
System.out.println("Game over, nobody has won.");
System.out.println("Do you want to play again? 'Y' or 'N':");
String newGame = scanner.nextLine();
if (newGame.equals("Y") || newGame.equals("y")) {
break;
}
if (newGame.equals("N") || newGame.equals("n")) {
System.out.println("Thanks for the game!");
return;
}
}
// 6. repeat
} while (true);
}
}

这是我的 Board 类的代码:

public class Board {

char[][] board = new char[6][7];

int column;

// Fills the empty spaces
public void fillBoard() {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
board[i][j] = ' ';
}
}
}

// Prints the board
public void presentBoard() {
for (int i = 0; i < 6; i++) {
System.out.print(" | ");
for (int j = 0; j < 7; j++) {
System.out.print(board[i][j] + " | ");
}
System.out.println();
System.out.print(" -----------------------------");
System.out.println();
}
}

// Turn
public void makeTurn(char badge, char Player) {
Scanner scanner = new Scanner(System.in);
do {
// 1. Ask for a column
System.out.println("Player " + Player + " turn: ");
column = scanner.nextInt();

// 2. Check if it's between 0 and 6
if (column > 6) {
System.out.println("That is not a valid number. Please enter a number between 0 and 6: ");
continue;
}

// 3. Place a badge
for (int i = 6 - 1; i >= 0; i--) {
if (board[i][column] == ' ') {
board[i][column] = badge;
return;
}
}

// If column is full
System.out.println("Column " + column + " is full. Try another column:");

} while (true);
}

// Check for vertical win
public boolean checkWinVertical() {
return verticalWin(5, column);
}

// Check for horizontal win
public boolean checkWinHorizontal() {
return horizontalWin(5,column);
}

// Conditions for vertical win
private boolean verticalWin(int x, int y) {
char charToCheck = board[x][y];
if (board[x-1][y] == charToCheck &&
board[x-2][y] == charToCheck &&
board[x-3][y] == charToCheck) {
return true;
}

return false;
}

// Conditions for horizontal win
private boolean horizontalWin(int x, int y) {
char charToCheck = board[x][y];
if (board[x][y+1] == charToCheck &&
board[x][y+2] == charToCheck &&
board[x][y+3] == charToCheck) {
return true;
}
return false;
}

我已经成功地让游戏在数组的底行水平和垂直地识别胜利,但我不知道如何让游戏识别整个数组。我只关注水平和垂直,因为对角线对我来说太复杂了。我不知道这是否是正确的方法或者是否有更好的方法。谢谢!

最佳答案

这是另一个解决方案。其总体思路与前面提到的相同:循环遍历每一行/列,检查是否连续 4 条。也许这个实现会提供一些其他的见解。下面,我展示了一个检查水平条纹的示例方法。对于垂直方向,您可以迭代内部 for 循环中的行。

public boolean checkWin(char badge) {
return checkHorizontalStreaks(board, badge)
|| checkVerticalStreaks(board, badge);
}

private boolean checkHorizontalStreaks(char[][] board, char badge) {
for (int row = 0; row < board.length; row++) {
// loop throught each row
int currentStreak = 0;
for (int col = 0; col < board[row].length; col++) {
// loop through each column in the row
if (board[row][col] == badge) {
// keep the streak of 'badge' going
currentStreak++;
if (currentStreak == 4) {
// winner
return true;
}
} else {
// restart the streak
currentStreak = 0;
}
}
}
return false;
}

然后更新你的主类

        if (board.checkWin(badge)) {
System.out.println("Player " + Player + " has won!");
break;
}

我敢打赌有一种更有效的方法来确定获胜者(也许通过将网格视为图形并使用一些特殊的逻辑遍历它)。但是,我怀疑这可能足以满足您的需要。我不会给你输出,但它适用于一些不同的测试用例。

关于控制台中的 Java Connect Four - 水平和垂直获胜条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40638623/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com