gpt4 book ai didi

java - 适合初学者的简单 8 车游戏

转载 作者:太空宇宙 更新时间:2023-11-04 08:20:35 24 4
gpt4 key购买 nike

嗨,我是 java 新手,我想尝试制作一个游戏,让用户实际尝试自己解决 8 个皇后问题。然而,从 8 个车开始,到 14 个象,然后是 8 个后,难度会增加。

我已经成功创建了棋盘。我的鼠标监听器有问题...棋盘上的每个方 block 都是一个按钮,当单击时,我的意图是该方 block 将改变颜色以指示其已被单击,然后所有无法再次单击的方 block 也将更改以指示游戏中的方 block 。

当点击该方 block 时,它似乎没有执行任何操作。抱歉,我知道这很微不足道。谢谢。

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


public class rooks extends JFrame implements MouseListener{

private final int BOARD_SIZE = 8;
private final int BOARD_SIZE_COLS = 8;
private final int BOARD_SIZE_ROWS = 8;
// private JTextField bottom = new JTextField("") ");
// private JLabel bannerl = new JLabel("The game");
// private JButton queens = new JButton(" Play Queens ");
private JButton rooks = new JButton(" Play Rooks ");
// private JButton bishops = new JButton(" Play Knights ");
private JButton[][] cboard = new JButton[BOARD_SIZE][BOARD_SIZE];
private JTextArea bottomtextarea = new JTextArea();




// constructor creating the chessboard
public rooks(){
this.setSize(500, 500);
this.setTitle("rooks");
// this.setIconImage();

// create JPanels and add JComponents
JPanel main = new JPanel(new BorderLayout());
this.setContentPane(main);

JPanel north = new JPanel();
north.setLayout(new GridLayout(1,3));
main.add(north, BorderLayout.NORTH);
// north.add(queens);
north.add(rooks);
// north.add(bishops);

JPanel south = new JPanel();
main.add(south, BorderLayout.SOUTH);
south.add(bottomtextarea);
bottomtextarea.setEditable(false);
bottomtextarea.setVisible(true);

// create grid (actual chessboard) and initialise each button with no char
JPanel chessBoard = new JPanel(new GridLayout(BOARD_SIZE, BOARD_SIZE));
main.add(chessBoard, BorderLayout.CENTER);
for (int i=0; i<BOARD_SIZE_ROWS; i++){
for(int j=0; j<BOARD_SIZE_COLS; j++){
cboard[i][j] = new JButton("");
chessBoard.add(cboard[i][j]);

// as it loops add colour to the board, if (i+j=even then white, otherwise black)
if ((i + j) % 2 == 0) {
cboard[i][j].setBackground(Color.black);
}
else {
cboard[i][j].setBackground(Color.white);
}
}
}

cboard[7][7].addMouseListener(this);


this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void mousePressed(MouseEvent e){

}
public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mouseClicked(MouseEvent e) {
System.out.print("it has been clicked");
}

void saySomething(String eventDescription, MouseEvent e) {

}





}

最佳答案

您的代码正在运行。我运行它,当我单击 7-7 方 block (即右下角的方 block )时,我收到消息:“它已被单击”。

由于您仅将鼠标监听器添加到此方 block ,因此代码的行为符合预期。

但是有些事情你应该重构:

  • 为什么要定义 BOARD_SIZE、BOARD_SIZE_COLS、BOARD_SIZE_ROWS?如果您只使用二次游戏板,则只需要 BOARD_SIZE,否则则不需要 BOARD_SIZE。
  • 按照惯例,类(class)的第一个字母要大写。所以是 Rooks 而不是 rooks
  • 您需要将监听器添加到板上的每个方格,而不是仅一个方格

这应该足以开始。

关于java - 适合初学者的简单 8 车游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9606465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com