有人可以告诉我为什么我的胜利条件不起作用?

时间:2014-01-20 20:39:17

标签: java

我正在让游戏连接四个但是每当我运行程序时,它会丢弃圆圈,除了获胜条件之外的一切都不起作用。我不太确定我是不是正确地打电话给他们,或者是不是在正确的地方打电话给他们。请指教。我感谢您即将推出的答案。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ConnectFourJan {

  static boolean winCheck; 
  static drawBoard drawConnectFourBoard = new drawBoard();
  static int [][] spotOnBoard = new int [6][7];
  static int maxRow=6;
  static int maxCol=7;
  static boolean endOfGame = false;
  static boolean gameStart;
  static JPanel boardPanel;

  static JButton firstOption = new JButton ("Drop");
  static JButton secondOption = new JButton ("Drop");
  static JButton thirdOption = new JButton ("Drop");
  static JButton fourthOption = new JButton ("Drop");
  static JButton fifthOption = new JButton ("Drop");
  static JButton sixthOption = new JButton ("Drop");
  static JButton seventhOption = new JButton ("Drop");

  static JButton playAgain = new JButton ("New");
  static JButton reset = new JButton ("Reset");
  static JButton exit = new JButton ("Exit");

  static int blank =0;
  static int red=1;
  static int yellow=2;
  static int firstColour= red;

  public static void board() {
    spotOnBoard = new int [maxRow][maxCol];
    for (int row=0; row < maxRow; row++){
      for (int col=0; col< maxCol; col++){
        spotOnBoard[row][col]=blank;
      }
    }
  }  

  public static class drawBoard extends JPanel {
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      drawConnectFourBoard(g); 
    }//end of paintComponent.

    void drawConnectFourBoard(Graphics g){          
      g.setColor(Color.BLUE);
      g.fillRect(0, 0, 100+100*maxCol, 100+100*maxRow);
      for (int row=0; row<maxRow; row++)
        for (int col=0; col<maxCol; col++) {
        if (spotOnBoard[row][col]==blank) g.setColor(Color.white);
        if (spotOnBoard[row][col]==red) g.setColor(Color.red);
        if (spotOnBoard[row][col]==yellow) g.setColor(Color.yellow);
        g.fillOval(100*col, 100*row, 100, 100);
      }
    }
  }

  public static boolean winCheck(int colour) { 
   int box;
    if (colour==red)
      box = red;
    else
      box = yellow;
    for(int j = 0; j < 6; j++) { 
      for (int k = 0; k < 7; k++) { 
        if(spotOnBoard[j][k]==(box)) {           
          if(((j -3) >= 0) && spotOnBoard[j -1][k]==(box) && spotOnBoard[j -2][k]==(box) && spotOnBoard[j -3][k]==(box)) {  
            return true;
          }          
          else if(((k -3) >= 0) && spotOnBoard[j][k -1]==(box) && spotOnBoard[j][k -2]==(box) && spotOnBoard[j][k -3]==(box)) {
            return true; 
          }          
          else if(((j+3)<= 5) && spotOnBoard[j+1][k]==(box) && spotOnBoard[j+2][k]==(box) && spotOnBoard[j+3][k]==(box)){ 
            return true;
          }          
          else if(((k +3) <= 6) && spotOnBoard[j][k +1]==(box) && spotOnBoard[j][k +2]==(box) && spotOnBoard[j][k +3]==(box)) {          
            return true; 
          }          
          else if(((j -3)>= 0) && ((k +3)<=6) && spotOnBoard[j-1][k+1]==(box) && spotOnBoard[j-2][k+2]==(box) && spotOnBoard[j-3][k+3]==(box)){ 
            return true; 
          }          
          else if(((j +3) <=5) && ((k -3) >=0) && spotOnBoard[j +1][k -1]==(box) && spotOnBoard[j +2][k -2]==(box) && spotOnBoard[j +3][k -3]==(box)){ 
            return true;
          }          
          else if(((j -3)>=0) && ((k -3)>= 0) && spotOnBoard[j -1][k -1]==(box) && spotOnBoard[j -2][k -2]==(box) && spotOnBoard[j -3][k -3]==(box)){ 
            return true;
          }          
          else if(((j +3) <=5) && ((k +3) <=6) && spotOnBoard[j +1][k +1]==(box) && spotOnBoard[j +2][k +2]==(box) && spotOnBoard[j +3][k +3]==(box)){
            return true; 
          }

        }
      } 
    }
    return false; 
  }

  public static void displayWinner(int n) {
    if (n==red) {
      JOptionPane.showMessageDialog(null, "red wins! Congratulations!", "Winner!", JOptionPane.INFORMATION_MESSAGE );
    }
    else
   JOptionPane.showMessageDialog(null, "Yellow wins! Congratulations!", "Winner!", JOptionPane.INFORMATION_MESSAGE );
  }

  public static void main (String [] args) {
    board();
    ButtonHandler listen = new ButtonHandler();

    firstOption.addActionListener(listen);
    secondOption.addActionListener(listen);
    thirdOption.addActionListener(listen);
    fourthOption.addActionListener(listen);
    fifthOption.addActionListener(listen);
    sixthOption.addActionListener(listen);
    seventhOption.addActionListener(listen);
    playAgain.addActionListener(listen);
    reset.addActionListener(listen);
    exit.addActionListener(listen);

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new GridLayout(1,7));   
    topPanel.setBackground(new Color(0,0,0));
    topPanel.add(firstOption);
    topPanel.add(secondOption);
    topPanel.add(thirdOption);
    topPanel.add(fourthOption);
    topPanel.add(fifthOption);
    topPanel.add(sixthOption);
    topPanel.add(seventhOption);    

    JPanel bottomPanel = new JPanel();
    bottomPanel.setLayout(new GridLayout(1,4));
    bottomPanel.add(playAgain);
    bottomPanel.add(reset);
    bottomPanel.add(exit);    

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    mainPanel.setBackground(new Color(0,214,154));
    mainPanel.add(topPanel, BorderLayout.NORTH);
    mainPanel.add(drawConnectFourBoard);
    board();
    mainPanel.add(bottomPanel, BorderLayout.SOUTH);

    JFrame window = new JFrame ("Connect4");
    window.setContentPane(mainPanel);
    window.setSize(720,700);
    window.setLocation(500,100);
    window.setResizable(true);
    window.setVisible(true);
  }

   private static class ButtonHandler implements ActionListener { 
    public void actionPerformed(ActionEvent a) {
      if (a.getSource()== firstOption)
        dropCircle(0);      
      else if (a.getSource()== secondOption)
        dropCircle(1);
      else if (a.getSource()== thirdOption)
        dropCircle(2);
      else if (a.getSource()== fourthOption)
        dropCircle(3);
      else if (a.getSource()== fifthOption)
        dropCircle(4);
      else if (a.getSource()== sixthOption)
        dropCircle(5);
      else if (a.getSource()==seventhOption)
        dropCircle(6);

    if (a.getSource() == playAgain) {
      gameStart=true;
    }
    if (a.getSource() == exit) {
      System.exit(0);
    }
    if (a.getSource() == reset) {
      for (int row=0; row < maxRow; row++){
        for (int col=0; col< maxCol; col++){
          spotOnBoard[row][col]=blank;
        }
      }
      drawConnectFourBoard.repaint();
    }
    }

    public void dropCircle(int n) {
      if (endOfGame) return;
      gameStart=true;
      int row;
      for (row=0; row<maxRow; row++)
       if (spotOnBoard[row][n]>0) break;
      if (row>0) {
        if(winCheck(firstColour)){
          displayWinner(firstColour);
        }
        spotOnBoard[--row][n]=firstColour;
        if (firstColour==red)
          firstColour=yellow;
        else
          firstColour=red;        
      }
      drawConnectFourBoard.repaint();
    }

   }
}

3 个答案:

答案 0 :(得分:2)

您的问题是在dropCircle,您正在填充boardArray,但在winCheck,当您正在检查获胜时,您正在查看spotOnBoard },这是一个完全不同的数组。也就是说,你在一个数组中设置值,但在不同的地方寻找它们。这永远不会起作用。

答案 1 :(得分:0)

除了别人说的话,你的for循环似乎还有一个错误:

for(int j = 0; j < 6; j++) {
    for(int k = 0; k < 7; k++) {
        if(/* condition */) {
            return true;
        } else if(/* condition */) {
            return true;
        } else if(/* condition */) {
            return true;

        } else {            // bug
            return false;   // here
        }
    }
}

return false;

这只会检查第一次迭代,因此您需要删除else。

您似乎也没有检查winCheck的结果:

winCheck(firstColour);
displayWinner(firstColour);

我认为你的意思是这样的:

if(winCheck(firstColour)) {
    displayWinner(firstColour);
}

答案 2 :(得分:0)

当您对程序进行修订时,答案可能会发生变化,但您似乎错过了如何使用您为游戏定义的阵列的核心概念。

字符串,整数,布尔值,双打等等,无论您决定使用哪些对象/原语来模拟游戏,您都应该在整个程序中使用。 (即:字符串应使用.equals()进行比较,如前所述),您提供的数据结构也应该有用(即:不要在一个地方分配新的数据结构)如果您可以合法地在两个地方共享相同的数据,则在另一个地方指定另一个类似目的。)

截至目前,您的dropCircle方法看起来有点问题。

public void dropCircle(int n) {
  if (endOfGame) return;
  gameStart=true;
  int row;
  for (row=0; row<maxRow; row++)
    if (spotOnBoard[row][n]>0) break;  
  if (row>0) {
    if(winCheck(firstColour)){            // checking for a win here will result in
                                          // false because the winning position has
                                          // yet to be assigned.
      displayWinner(firstColour);
    }
    spotOnBoard[--row][n] = firstcolour;  // move this line above the if statement above.
    if (firstColour==red)
      firstColour=yellow;
    else
      firstColour=red;        
  }
  drawConnectFourBoard.repaint();
}

需要注意的另一点是您一直使用字符串和整数来表示董事会状态。你可以想象使用相同的数据类型实现整个游戏,它可以消除大量的逻辑错误,这很可能是由于数据类型处理不当造成的。

我可能会建议您重命名并整合一些数据结构。 spotOnBoard意味着电路板上有一个空间,实际上,这个数组你的电路板。板上的点是[row,col]

的元素

其次,我会查看Object equals的javadoc。特别是String等于。 http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object)

祝你好运

修改:修改以反映OP更新和建议采取的措施。