我对 Java 很陌生,我才研究了几个星期,这个项目让我很困惑。说明是创建一个雷区游戏,该游戏生成一个随机起点、一个随机终点和两个地雷。用户必须在不撞到地雷的情况下从头到尾导航。我的目标是测试用户是否进行了有效的移动,然后更改他单击的按钮的颜色。因此,从头到尾制作了一条路径。当他点击检查按钮时,它会告诉他他是否点击了一个隐藏的矿井,以及他是赢还是输。
这是我到目前为止所拥有的。
任何有关方法的帮助,或有关如何提高效率的建议将不胜感激。
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);
}
}