- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从头开始创建一个 Connect Four 程序以进行良好的实践,但我在使用 checkAlignment() 方法时遇到了问题,或者获胜条件是什么。它适用于某些行,但不是所有行,并且不适用于任何其他方向(垂直、对角向前、对角向后)。
public char checkAlignment(int row, int column) {
char color = board[row][column];
char[][] current = getBoard();
// Horizontal Left-to-Right Check - - - - - - - - - -
if (column + 4 <= columns) {
for (int i = 1; i < 4; i++) {
if (current[row][column + i] != color) {
return NONE;
}
}
return color;
}
// Horizontal Right-To-Left Check - - - - - - - -
if (column - 4 > -1) {
for (int i = 1; i < 4; i++) {
if (current[row][column - i] != color) {
return NONE;
}
}
return color;
}
// Vertical Top-To-Bottom Check - - - - - - -
if (row + 4 <= rows) {
for (int i = 1; i < 4; i++) {
if (current[row + i][column] != color) {
return NONE;
}
}
return color;
}
// Vertical Bottom-To-Top Check - - - - - - - -
if (row - 4 > -1) {
for (int i = 1; i < 4; i++) {
if (current[row - i][column] != color) {
return NONE;
}
}
return color;
}
// Main Diagonal Backwards Check - - - - - - - - - -
if (column - 4 > -1 && row - 4 > -1) {
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 4; j++) {
if (current[row - i][column - j] != color) {
return NONE;
}
}
}
return color;
}
// Main Diagonal Forwards Check - - - - - - - - - -
if (column + 4 <= columns && row + 4 <= rows) {
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 4; j++) {
if (current[row + i][column + j] != color) {
return NONE;
}
}
}
return color;
}
// Secondary Diagonal Backwards Check - - - - - - - - -
if (column - 4 > -1 && row + 4 <= rows) {
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 4; j++) {
if (current[row + i][column - j] != color) {
return NONE;
}
}
}
return color;
}
// Secondary Diagonal Forwards Check - - - - - - - - - -
if (column + 4 <= columns && row - 4 > -1) {
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 4; j++) {
if (current[row - i][column + j] != color) {
return NONE;
}
}
}
return color;
}
return NONE;
}
谁能帮帮我吗?
编辑/调整:
public char checkAlignment(int row, int column) {
char color = board[row][column];
char[][] current = getBoard();
// Horizontal Left-to-Right Check
if (column + 4 <= NUM_COLS) {
for (int i = 0; i < 4; i++) {
if (current[row][column + i] != color) {
return NONE;
}
}
return color;
}
// Horizontal Right-To-Left Check
if (column - 4 > -1) {
for (int i = 0; i < 4; i++) {
if (current[row][column - i] != color) {
return NONE;
}
}
return color;
}
// Vertical Top-To-Bottom Check
if (row + 4 <= NUM_ROWS) {
for (int i = 0; i < 4; i++) {
if (current[row + i][column] != color) {
return NONE;
}
}
return color;
}
// Vertical Bottom-To-Top Check
if (row - 4 > -1) {
for (int i = 0; i < 4; i++) {
if (current[row - i][column] != color) {
return NONE;
}
}
return color;
}
// Main Diagonal Backwards Check
if (column - 4 > -1 && row - 4 > -1) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (current[row - i][column - j] != color) {
return NONE;
}
}
}
return color;
}
// Main Diagonal Forwards Check - - - - - - - - - -
if (column + 4 <= NUM_COLS && row + 4 <= NUM_ROWS) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (current[row + i][column + j] != color) {
return NONE;
}
}
}
return color;
}
// Secondary Diagonal Backwards Check - - - - - - - - -
if (column - 4 > -1 && row + 4 <= NUM_ROWS) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (current[row + i][column - j] != color) {
return NONE;
}
}
}
return color;
}
// Secondary Diagonal Forwards Check - - - - - - - - - -
if (column + 4 <= NUM_COLS && row - 4 > -1) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (current[row - i][column + j] != color) {
return NONE;
}
}
}
return color;
}
return NONE;
}
最佳答案
从外观上看,每个循环都使用 i=1
,然后最多使用 4 个循环,每个循环运行四次。但是,您的行和列始终会被检查为距当前值 +/- 4。如果我没看错的话,在某些情况下,你会比需要的多检查一项。因此,要么使 i=0 要么达到 3(因为您已经用当前检查了自己的方 block )。
编辑:我再看了一下
我上面的第一句话并不是真正的问题。我认为真正的问题是,对于每一项,您只在远离“当前”一项的单一方向上检查 4 项。这并不总是正确的,如以下示例所示,其中“y”是最后放入网格中的一 block 。
x x y x
因此,在这种情况下,您可以尝试以下代码来检查“当前”所选 token 左右两侧的总数。如果两个方向上有 4 个或更多连接,则为 connect-4:
//Horizantal check
//Right side
int connected = 0;
int i = 1;
while (column + i <= columns) {
if (current[row + i][column] != color) {
break;
}else{
connected++;
}
i++;
}
i = 1;
//left side
while (column - i <= columns) {
if (current[row - i][column] != color) {
break;
}else{
connected++;
}
i++;
}
if(connected >= 4){//total connected
return color;
}else{
return NONE;
}
经审查,您的垂直平纹支票看起来不错。回想一下,在 connect-4 中,您将 token 沿着网格放下,因此您永远无法将新 token 放入其上方有另一个 token 的插槽中(在您放下它的回合中),这意味着不需要向上进行垂直检查。
//Vertical plain check - The one dropped must always be on top
if (row - 4 > -1) {
for(int i=1; i<4;i++){
if (current[row - i][column] != color) {
return NONE;
}
}
return color;
}
希望这对正确理解这两个方向有所帮助。对于对角线,你需要像我对水平平面所做的那样。
关于java - Java 中的 ConnectFour 程序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32857403/
我是 Hill 先生编码类的九年级学生!最近,他要求我们创建一款四子棋游戏,该游戏涉及两名玩家,防止作弊(因此玩家 1 不能继续在一回合中放置棋子等),并且功能将像真正的四子棋游戏一样!我已经开始编写
我正在从头开始创建一个 Connect Four 程序以进行良好的实践,但我在使用 checkAlignment() 方法时遇到了问题,或者获胜条件是什么。它适用于某些行,但不是所有行,并且不适用于任
我是一名优秀的程序员,十分优秀!