[英]How should I implement a horizontal win algorithm for my Connect 4 program?
I am making a connect 4 board game and I implemented this horizontal win algorithm.我正在制作一个连接 4 棋盘游戏,并实现了这种水平获胜算法。 It doesn't seem to work for some reason.
由于某种原因,它似乎不起作用。 Based on the code I assumed the conditions I set in the algorithm would look over the values assigned to the array in generateBoard method but it did not.
基于代码,我假设我在算法中设置的条件会查看在 generateBoard 方法中分配给数组的值,但它没有。
What could a possible solution be?可能的解决方案是什么? Any help is appreciated.
任何帮助表示赞赏。
public static String[][] generateBoard(){
String[][] board = new String[6][7]; //[row][column] 6 by 7
for(int i = 0; i<board.length; i++) {
for(int j = 0; j<board[i].length; j++) {
if(j == 0)
board[i][j] = "| |";
else
board[i][j] = " |";
}
}
return board;
}
Horizontal Win Algorithm:水平赢算法:
//Checks to see which player's turn it is
String color = (redPlayerTurn) ? "R" : "Y";
int pattern = 0; //Used as a flag for detecting a winner
//HORIZONTAL Check
for(int i = 1; i < board.length; i++) {
String columnIndex = board[i][0];
pattern = 1;
for(int j = 1; j < board[i].length; j++) {
if(!(board[i][j].contains("| |") || board[i][j].contains(" |"))) {
if(columnIndex == board[j][i] && pattern != 0)
pattern++;
}else
columnIndex = board[i][j];
pattern = 0;
}if(pattern == 4) {
return true;
}
}
//Checks to see which player's turn it is
String color = (redPlayerTurn) ? "R" : "Y";
int pattern = 0; //Used as a flag for detecting a winner
//HORIZONTAL Check
for(int i = 1; i < board.length; i++) {
String columnIndex = board[i][0];
pattern = 1;
for(int j = 1; j < board[i].length; j++) {
if(!board[i][j].contains("|")) {
if(columnIndex == board[j][i] && pattern != 0) {
pattern++;
} else {
columnIndex = board[i][j];
pattern = 0; // <- missing {} will assign pattern to 0
}
}
}
if(pattern >= 4) {
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.