문제

I am making a game where move across a gridlayout of jbuttons, only allowing movement to adjacent grid jbuttons. The goal is to try and make it to the topleft (From the btm right). I think this is all that is relevant:

//Here is my method to determine whether it is a valid move (i dont want you to be able to move anywhere but adjacent spots

    public boolean goodMove(int x, int y) {

    int val = Math.abs(current.x - x) + Math.abs(current.y - y);

    return val == 1;
}

//Here is the logic in the actionlister, a big if else loop that is now referencing only the jbuttons in my gridlayout (the game panel)

      public void actionPerformed(ActionEvent e) {

    JButton clicked = (JButton) e.getSource();

    if (clicked == SHOWMINES) {

        if (SHOWMINES.getText().equals("Show Mines")) {
            showMines();
            SHOWMINES.setText("Hide Mines");
        } else {
            hideMines();
        }
    } else {
        if (clicked == NEWGAME) {
            newGame();
        } else {
            if (clicked == SHOWPATH) {
                if (SHOWPATH.getText().equals("Show Path")) {
                    showPath();
                    SHOWPATH.setText("Hide Path");
                } else {
                    hidePath();
                }
            } else {
                if (clicked == OKAY) {
                    resetGridSize(Integer.parseInt(gridSizeInput.getText()));
                    newGame();
                } else {

                }
                int gs = Integer.parseInt(gridSizeInput.getText());
                for (int i = 0; i < gs - 1; i++) {
                    for (int j = 0; j < gs - 1; j++) {
                        if (clicked == grid[i][j]) {
                            if (goodMove(i, j)) {//debug tester ,this is where the problem is.
                                System.out.println("TEST");
                                current = new Point(i, j);
                                grid[i][j].setBackground(Color.green);
                                grid[i][j].setText("=");
                            }else{System.out.println("TEST2");}
                        }
                    }
                }
            }
        }
    }
}// end of action listener

Grid[][] is my array of grid buttons, gridSizeInput is the textfield that you can adjust the gridsize in.

Thanks in advance (I'm new to programming, so keep it simple if you can :)

EDIT: I forgot to mention, it is not recognizing my if(goodMove()) loop, its just printing out TWO for my else.

EDIT: My question is why is not working, no buttons will are be recognized as clicked in my grid, I guess I am asking to see if something I have written is glaringly obviously wrong. thanks.

EDIT: Okay here is my entire action listener for all my buttons. This is my first post, sorry I am not more helpful- let me know if anything else I can add.

도움이 되었습니까?

해결책

Your problem may be as simple as your not extending your for loops out far enough. Try changing this:

for (int i = 0; i < gs - 1; i++) {
    for (int j = 0; j < gs - 1; j++) {

to this:

for (int i = 0; i < gs; i++) {
    for (int j = 0; j < gs; j++) {

Edit
This was the minimal code example that I created to test the concept:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.*;

import javax.swing.*;

public class Foo extends JPanel {
   public static final int GRID_SIZE = 10;
   private Point current;
   private JButton[][] grid;

   public Foo() {
      ButtonListener listener = new ButtonListener();
      setLayout(new GridLayout(GRID_SIZE, GRID_SIZE));
      grid = new JButton[GRID_SIZE][GRID_SIZE];
      for (int i = 0; i < grid.length; i++) {
         for (int j = 0; j < grid[i].length; j++) {
            grid[i][j] = new JButton("   ");
            grid[i][j].addActionListener(listener);
            add(grid[i][j]);
         }
      }

      current = new Point(GRID_SIZE - 1, GRID_SIZE - 1);
   }

   private class ButtonListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         JButton clicked = (JButton) e.getSource();
         // !! int gs = Integer.parseInt(gridSizeInput.getText());
         int gs = GRID_SIZE;
         // !! for (int i = 0; i < gs - 1; i++) {
         // for (int j = 0; j < gs - 1; j++) {
         for (int i = 0; i < gs; i++) {
            for (int j = 0; j < gs; j++) {

               if (clicked == grid[i][j]) {
                  if (goodMove(i, j)) {
                     System.out.println("TEST");
                     current = new Point(i, j);
                     grid[i][j].setBackground(Color.green);
                     grid[i][j].setText("=");
                  } else {
                     System.out.println("TEST2");
                  }
               }
            }
         }
      }
   }

   public boolean goodMove(int x, int y) {
      int val = Math.abs(current.x - x) + Math.abs(current.y - y);
      return val == 1;
   }

   private static void createAndShowGui() {
      Foo mainPanel = new Foo();
      JFrame frame = new JFrame("Foo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top