1

私は Java を初めて使用します。Java を勉強してまだ数週間しか経っていませんが、このプロジェクトには戸惑っています。指示は、ランダムな開始点、ランダムな終了点、および 2 つの地雷を生成する地雷原ゲームを作成することでした。ユーザーは地雷に当たらないように最初から最後までナビゲートする必要があります。私の目標は、ユーザーが有効な動きをしたかどうかをテストし、クリックしたボタンの色を変更することでした。したがって、最初から最後までパスを作成します。チェックボタンを押すと、隠れた地雷をクリックしたかどうか、勝ったか負けたかがわかります。

これが私がこれまでに持っているものです。

メソッドに関するヘルプ、またはこれをより効率的にする方法に関する提案は大歓迎です。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import java.util.Random;

import javax.swing.*;

public class MineField extends JFrame implements ActionListener {
    private JFrame main = new JFrame("MineField Game");

    private JPanel top = new JPanel();
    private JPanel bottom = new JPanel();
    private int y;
        private int x;

/**
 * @return the grid
 */
public JButton[][] getGrid() {
    return grid;
}

/**
 * @param grid the grid to set
 */
public void setGrid(JButton[][] grid) {
    this.grid = grid;
}
JButton[][] grid; //names the grid of buttons

public MineField(int width, int length){ //constructor with 2 parameters
        Container container = getContentPane();
        container.setLayout(new GridLayout(2,1));
        top.setLayout(new GridLayout(width,length)); //set layout of frame
        grid=new JButton[width][length]; //allocate the size of grid

        //Generates 4 random numbers for coordinates.
        Random generator = new Random();
        int a = generator.nextInt(width);
        int b = generator.nextInt(length);
        int c = generator.nextInt(width);
        int d = generator.nextInt(length);
        int mine1x = generator.nextInt(width);
        int mine1y = generator.nextInt(length);
        int mine2x = generator.nextInt(width);
        int mine2y = generator.nextInt(length);


        //Starts for loop to initiate the grid.
        int t = 0;
        for(y=0; y<length; y++){ 
                for(x=0; x<width; x++){

                    if(a == x && b == y){
                        grid[a][b] = new JButton("Start");
                        grid[a][b].setOpaque(true);
                        grid[a][b].setBackground(Color.green);
                        top.add(grid[a][b]);
                        grid[a][b].addActionListener(this);
                        grid[a][b].setActionCommand("start");
                    }
                    else if(c == x && d == y){
                        grid[c][d] = new JButton("Finish");
                        grid[c][d].setOpaque(true);
                        grid[c][d].setBackground(Color.green);
                        top.add(grid[c][d]);
                        grid[c][d].addActionListener(this);
                        grid[c][d].setActionCommand("finish");
                    }
                    else if(mine1x == x && mine1y == y){
                        grid[mine1x][mine1y] = new JButton();
                        top.add(grid[mine1x][mine1y]);
                        grid[mine1x][mine1y].addActionListener(this);
                        grid[mine1x][mine1y].setActionCommand("mine");

                    }
                    else if(mine2x == x && mine2y == y){
                        grid[mine2x][mine2y] = new JButton();
                        top.add(grid[mine2x][mine2y]);
                        grid[mine2x][mine2y].addActionListener(this);
                        grid[mine2x][mine2y].setActionCommand("mine");
                    }
                    else{
                        grid[x][y]=new JButton();  
                        top.add(grid[x][y]);
                        grid[x][y].addActionListener(this);
                        grid[x][y].setActionCommand("color");
                        ++t;   
                     }    
                }
        }
        //Prints coordinates of start and end points.
        System.out.println("Start: " +a+ "," +b);
        System.out.println("Finish: " +c+ "," +d);
        System.out.println("Mine 1: " +mine1x+ "," +mine1y);
        System.out.println("Mine 2: " +mine2x+ "," +mine2y);
        System.out.println(getAlignmentX());
        System.out.println(getAlignmentY());


        //Creates buttons for clear and check at the bottom of the window.
        JButton clear = new JButton("Clear");
        clear.addActionListener(this);
        clear.setActionCommand("clear");
        JButton check = new JButton("Check");
        check.addActionListener(this);
        check.setActionCommand("check");

        //Adds the buttons to the panel.
        bottom.add(clear);
        bottom.add(check);

        //Adds panels to the JFrame.
        container.add(top, BorderLayout.NORTH);
        container.add(bottom, BorderLayout.NORTH); 
        main.add(container);
        main.pack();
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setVisible(true);

}

public void actionPerformed(ActionEvent event){
    //When clear button is pressed.
    if(event.getActionCommand().equals("clear")){
        String len = JOptionPane.showInputDialog(null, "Set amount of rows.");
        int lenInt = Integer.parseInt(len);
        String wid = JOptionPane.showInputDialog(null, "Set amount of columns.");
        int widInt = Integer.parseInt(wid);
        MineField a = new MineField(widInt, widInt);
    }

    //When a button on the grid is selected.
    if(event.getActionCommand().equals("color")){
        JButton selected;
        JButton clickedOn = (JButton)event.getSource();
        clickedOn.setOpaque(true);
        clickedOn.setBackground(Color.GREEN);
        selected = clickedOn;
    }
    if(event.getActionCommand().equals("check")){

    }



}
//I don't know what is going on here.
public boolean adjacentTo(){

}




/**
 * @author Trevor W. Hutto
 * @version 1.0
 * This program is a version of minefield
 * You must navigate from start to finish without hitting a mine.
 */
public static void main(String[] args) {
    JOptionPane.showMessageDialog(null, "-This is a minefield game. \n-You can move side to side or up and down. \n-You must navigate from start to finsih without hitting a mine.");
    String len = JOptionPane.showInputDialog(null, "Set amount of rows.");
    int lenInt = Integer.parseInt(len);
    String wid = JOptionPane.showInputDialog(null, "Set amount of columns.");
    int widInt = Integer.parseInt(wid);
    MineField a = new MineField(lenInt,widInt);



}

}

4

0 に答える 0