Search code examples
javaperformanceswingjoptionpanechess

How do I implement a JOptionpane List Option?


Github Link to full download

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

I am currently developing a chess game and have ran into a problem

Currently when a pawn reaches the end of the other side of the board it changes into a white queen by default

I want to let the user decide what it changes into (hence the rules of chess) i was toying around with JOption pane but I am having trouble getting it to work i can run it with our errors but it does nothing for me in terms of chaging the pieces i am looking for some help with the implementation,

The queen code snipet (didnt include full code here because it would be to long all files found on github link if you wish to run the file for yourselfs)

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);
                    }
                }

If you want to see what I mean by all means download and run the java file itself you can see the way it only changes to the white queen


Solution

  • I really wasn't sure what to do with this one. Is it a "bad question"? I don't know.

    A general remark: The implementation is BAD in every way. The way how you are messing around with the GUI components and trying to implement the game logic in a single class is horrible. There are way too many individual flaws to list them here with reasonable effort. There is too much wrong to give reasonable hints for improvements. It will basically be impossible to write an AI based on this code, so any answer that suggests to "solve your problem" will not help you in the long run.

    In general, and particularly aiming at your intention to write an AI:

    • You should have a class called Board that represents the chess board. And this should NOT be a GUI component, but only a very simple class, probably consisting of a 8x8 array for the fields/pieces, and some methods to set the pieces at individual fields
    • You should probably have a class called Piece, or maybe a class called Field that contains a representation of a piece. Again, this should NOT be a GUI component, but only a simple class, maybe not containing more than some basic information (piece type, position, color...)
    • You should have a class called Move. This is in fact the important one. If you want to implement an AI, you'll probably start with the Minimax algorithm, and later extend it with Alpha-Beta-Pruning. And for both algorithms, you only need a very basic set of operations:

      • You must be able to make a move
      • You must be able to evaluate the board
      • You must be able to undo the move

      So the Move class must contain all information that is necessary to do and undo the move.

    • (A link to Chess Programming Wiki probably won't help you anyhow, but I wanted to mention it here)

    Concerning the actual question: This was the minimal modification that I considered as necessary to achieve your current goal (with the code snippet from UniversE, +1 for that). But you should not try to write an AI based on this class, because you will not be successful with that.

        //-------------------------------------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);
            }
        }