gpt4 book ai didi

java - 如何实现 JOptionpane 列表选项?

转载 作者:行者123 更新时间:2023-12-01 22:38:57 26 4
gpt4 key购买 nike

Github 完整下载链接

https://github.com/Jamiex304/Chess-Game

我目前正在开发一款国际象棋游戏,遇到了一个问题

目前,当棋子到达棋盘另一侧的末端时,默认情况下它会变成白皇后

我想让用户决定它会变成什么(因此是国际象棋的规则)我正在摆弄 JOption Pane ,但我很难让它工作我可以运行它并显示我们的错误,但它确实如此对于我正在寻找实现方面的帮助的部分而言,我没有任何帮助

女王代码片段(此处未包含完整代码,因为如果您想自己运行该文件,在 github 链接上找到的所有文件都会很长)

if(!validMove){
int location=0;
if(startY ==0){
location = startX;
}
else{
location = (startY*8)+startX;
}
String pieceLocation = pieceName+".png";
pieces = new JLabel( new ImageIcon(pieceLocation) );
panels = (JPanel)chessBoard.getComponent(location);
panels.add(pieces);
}
else{
if(success){
int location = 56 + (e.getX()/75);
if (c instanceof JLabel){
Container parent = c.getParent();
parent.remove(0);
pieces = new JLabel( new ImageIcon("WhiteQueen.png") );
parent = (JPanel)chessBoard.getComponent(location);
parent.add(pieces);
}
else{
Container parent = (Container)c;
pieces = new JLabel( new ImageIcon("WhiteQueen.png") );
parent = (JPanel)chessBoard.getComponent(location);
parent.add(pieces);
}
}
else{
if (c instanceof JLabel){
Container parent = c.getParent();
parent.remove(0);
parent.add( chessPiece );
}
else {
Container parent = (Container)c;
parent.add( chessPiece );
}
chessPiece.setVisible(true);
}
}

如果您想了解我的意思,请务必下载并运行 java 文件本身,您可以看到它仅更改为白皇后的方式

最佳答案

我真的不知道该怎么处理这个。这是一个“糟糕的问题”吗?我不知道。

一般性评论:该实现在各个方面都糟糕。你如何搞乱 GUI 组件并试图在单个类中实现游戏逻辑的方式是可怕的。有太多的个人缺陷,无法通过合理的努力在这里列出。错误太多,无法给出合理的改进提示。基于此代码编写人工智能基本上是不可能的,因此任何建议“解决您的问题”的答案从长远来看都不会帮助您。

一般来说,特别是针对您编写 AI 的意图:

  • 您应该有一个名为 Board 的类来表示棋盘。这应该是一个 GUI 组件,而只是一个非常简单的类,可能由用于字段/片段的 8x8 数组组成,以及一些在各个字段设置片段的方法
  • 您可能应该有一个名为 Piece 的类,或者一个名为 Field 的类,其中包含一个片段的表示。再次强调,这不应该是一个 GUI 组件,而只是一个简单的类,可能只包含一些基本信息(棋子类型、位置、颜色...)
  • 您应该有一个名为 Move 的类。这其实是最重要的一点。如果你想实现人工智能,你可能会从 Minimax 开始算法,然后用 Alpha-Beta-Pruning 扩展它。对于这两种算法,您只需要一组非常基本操作:

    • 您必须能够采取行动
    • 您必须能够评估董事会
    • 您必须能够撤消该移动

    因此,Move 类必须包含执行和撤消移动所需的所有信息。

  • (指向 Chess Programming Wiki 的链接可能无论如何都不会帮助您,但我想在这里提及它)

关于实际问题:这是我认为实现当前目标所必需的最小修改(使用 UniversE 的代码片段,+1)。但是您不应该尝试基于此类编写 AI,因为您不会成功。

    //-------------------------------------Changes to Queen Piece and Validates Move----------------------------------------------

if(!validMove){
int location=0;
if(startY ==0){
location = startX;
}
else{
location = (startY*8)+startX;
}
String pieceLocation = pieceName+".png";
pieces = new JLabel( new ImageIcon(pieceLocation) );
panels = (JPanel)chessBoard.getComponent(location);
panels.add(pieces);
}
else{
if(success){

if (c instanceof JLabel){
Container parent = c.getParent();
parent.remove(0);

String promoteTo;
do {
promoteTo = (String) JOptionPane.showInputDialog(null,
"Promote to:", "Promotion",
JOptionPane.QUESTION_MESSAGE, null,
new String[]{"Queen", "Bishup", "Knight", "Rook"}, "Queen");
} while (promoteTo == null);
String newPiece = null;
int location = 0;
if (pieceName.contains("White"))
{
location = 56 + (e.getX()/75);
newPiece = "White"+promoteTo;
}
else
{
location = (e.getX()/75);
newPiece = "Black"+promoteTo;
}

pieces = new JLabel( new ImageIcon(newPiece+".png") );
parent = (JPanel)chessBoard.getComponent(location);
parent.add(pieces);
validate();
repaint();
}
}
else{
if (c instanceof JLabel){
Container parent = c.getParent();
parent.remove(0);
parent.add( chessPiece );
}
else {
Container parent = (Container)c;
parent.add( chessPiece );
}
chessPiece.setVisible(true);
}
}

关于java - 如何实现 JOptionpane 列表选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26476669/

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