首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java:连接4中对角线

Java:连接4中对角线
EN

Stack Overflow用户
提问于 2015-06-18 01:38:04
回答 2查看 3.5K关注 0票数 3

我最近做了一个Connect4游戏,我的Connect4没有赢得比赛时,它是对角向右连接。它只适用于一些组合,当它对角连接到左边时。坐标:左上角:(0,0),左下角:(5,0),右上角:(0,6),右下角:(5,6)。Connect4板是6乘7。

问题:向左对角连接的只适用于某些组合。但是,没有一个连接是对角连接到正确的工作。

代码语言:javascript
复制
/** A method of winning diagonally towards the left side when playing connect 4 two player*/
/** Giving the new method with all the possible possibilities for a user to win diagonally-left */
public static void diagWinningLeft() {
    for (int x = 5; x > 2; x--) { // Checks to see if same colored pegs are lining diagonally to the left
        for (int y = 6; y > 3; y--) {
            if (board[x][y] == 1 && board[x - 1][y - 1] == 1 && board[x - 2][y - 2] == 1 && board[x - 3][y - 3] == 1) {
                JOptionPane.showMessageDialog(null, playerNames[0]+" has connected four diagonally-left in a row in " +(countForRed)+ " turns!");
                b.drawLine(x,y,x-3,y-3);
            }
            if (board[x - 1][y - 1] == 1 && board[x - 2][y - 2] == 1 && board[x - 3][y - 3] == 1 && board[x - 4][y - 4] == 1) {
                JOptionPane.showMessageDialog(null, playerNames[0]+" has connected four diagonally-left in a row in " +(countForRed)+ " turns!");
                b.drawLine(x,y,x-3,y-3);
            }

            if (board[x][y] == 2 && board[x - 1][y - 1] == 2 && board[x - 2][y - 2] == 2 && board[x - 3][y - 3] == 2) {
                JOptionPane.showMessageDialog(null, playerNames[1]+ " has connected four diagonally-left in a row in " +(countForYellow)+ " turns!");
                b.drawLine(x,y,x-3,y-3);
            }
        }
    }
}

/** Another method of winning diagonally towards the right side when playing connect 4 two player*/
/** Giving the new method with all the possible possibilities for a user to win diagonally-right*/
public static void diagWinningRight()  {
    for (int x = 0; x < 2; x++) { // Check to see if same colored pegs are lining diagonally to the right
        for (int y = 0; y < 3; y++) {
            if (board[x][y] == 1 && board[x + 1][y + 1] == 1 && board[x + 2][y + 2] == 1 && board[x + 3][y + 3] == 1) {
                JOptionPane.showMessageDialog(null, playerNames[0]+" has connected four diagonally-right in a row in " +(countForRed)+ " turns!");
            }
            if (board[x][y] == 2 && board[x + 1][y + 1] == 2 && board[x + 2][y + 2] == 2 && board[x + 3][y + 3] == 2) {
                JOptionPane.showMessageDialog(null, playerNames[1]+" has connected four diagonally-right in a row in " +(countForYellow)+ " turns!");
            }

        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-06-19 09:31:55

请原谅我没有直接回答这个问题,但这将帮助您解决这个问题,并最终获得更好的代码和将来编写代码的能力。

将"if“条件的逻辑提取到单独的方法中,可以更容易地独立考虑该逻辑,并允许您独立于程序的其余部分对其进行测试。

因此,与其:

代码语言:javascript
复制
  if (board[x][y] == 1 && board[x - 1][y - 1] == 1 && board[x - 2][y - 2] == 1 && board[x - 3][y - 3] == 1) {
       JOptionPane.showMessageDialog(...)
  }

..。用途:

代码语言:javascript
复制
 if(isDiagonalLeft(x,y,1) { ... }

..。还有..。

代码语言:javascript
复制
 boolean isDiagonalLeft(int x, int y, int player) {
      return board[x][y] == player && 
             board[x - 1][y - 1] == player  && 
             board[x - 2][y - 2] == player  && 
             board[x - 3][y - 3] == player 
 }

现在,您可以在()上运行单元测试,以确保其工作正常。也就是说,一个小程序设置一个板,只运行isDiagonalLeft(),以确保它在各种情况下给出了正确的答案。这感觉像是额外的工作,但是大多数尝试过它的人都知道,它可以通过早期捕获bug来节省精力。

您所做的是在某种程度上将游戏逻辑与演示代码(JOptionPane)分开,这样当您只想使用游戏逻辑时,表示代码就不会妨碍您。在以后的编程研究中,您将遇到更多的方法来分离它们,比如模型。

如果你需要问一些关于堆栈溢出的问题,像这样的逻辑也是有帮助的--通过把游戏逻辑和Swing分开,你可以向那些对Swing一无所知的潜在回答者打开这个问题。

而且,您可以对每个玩家重复使用此方法一次,而不是像您所拥有的那样将逻辑复制到两个位置。

如果它不起作用,请使用IDE中的调试器逐步完成它。

现在,您已经这样做了,您可以改进该方法,以便计算机执行递减,而不是程序员.

代码语言:javascript
复制
  boolean isDiagonalLeft(int x, int y, int player) {
       for(int i = 0; i<4; i++) {
           if(board[x-i][y-i] != player) {
               return false;
           }
       }
       return true;
  }

...and,您可以将其概括为涵盖对角线的两个方向:

代码语言:javascript
复制
  boolean isDiagonal(int x, int y, int player, boolean direction) {
       int dirUnit = direction ? -1 : 1;
       for(int i = 0; i<4; i++) {
           if(board[x-i][y + dirUnit] != player) {
               return false;
           }
       }
       return true;
  }

..。所以现在你可以在四个地方重复使用这个方法:每个玩家和每个方向。

当您遇到在GUI中不工作的情况时,进行单元测试,按照GUI中的方式设置板,并在其上运行isDiagonal()。如果测试通过了,你就知道问题出在别的地方。如果测试失败,您可以使用调试器和方法的代码,以使其通过。

票数 3
EN

Stack Overflow用户

发布于 2015-06-19 05:41:07

以下代码应该有效:

代码语言:javascript
复制
//Winning diagonally towards left
public static void diagWinningLeft() {
    for (int x = 5; x > 2; x--) { // Checks to see if same colored pegs are lining diagonally to the left
        for (int y = 6; y > 2; y--) {
            if (board[x][y] == 1 && board[x - 1][y - 1] == 1 && board[x - 2][y - 2] == 1 && board[x - 3][y - 3] == 1) {
                JOptionPane.showMessageDialog(null, playerNames[0]+" has connected four diagonally-left in a row in " +(countForRed)+ " turns!");
                b.drawLine(x,y,x-3,y-3);
            }
            if (board[x][y] == 2 && board[x - 1][y - 1] == 2 && board[x - 2][y - 2] == 2 && board[x - 3][y - 3] == 2) {
                JOptionPane.showMessageDialog(null, playerNames[1]+ " has connected four diagonally-left in a row in " +(countForYellow)+ " turns!");
                b.drawLine(x,y,x-3,y-3);
            }
        }
    }
}

//Winning diagonally towards right
public static void diagWinningRight()  {
    for (int x = 5; x > 2; x--) { // Check to see if same colored pegs are lining diagonally to the right
        for (int y = 0; y < 4; y++) {
            if (board[x][y] == 1 && board[x - 1][y + 1] == 1 && board[x - 2][y + 2] == 1 && board[x - 3][y + 3] == 1) {
                JOptionPane.showMessageDialog(null, playerNames[0]+" has connected four diagonally-right in a row in " +(countForRed)+ " turns!");
            }
            if (board[x][y] == 2 && board[x - 1][y + 1] == 2 && board[x - 2][y + 2] == 2 && board[x - 3][y + 3] == 2) {
                JOptionPane.showMessageDialog(null, playerNames[1]+" has connected four diagonally-right in a row in " +(countForYellow)+ " turns!");
            }
        }
    }
}

代码的问题是,在向右检查时,您的坐标(索引)继续检查左方向(因为ax中有4条对角线)。ax+1在NW-SE方向(或左对角线)构成对角线,ax+1在NE-SW方向(或右对角线)构成对角线.

此外,您的内部for循环没有运行到limits.Hence,diagWinningLeft()有时不工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30905138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档