-1

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

Jamiex304
  • 242
  • 1
  • 7
  • 26
  • Too much code. Read on [MCVE](http://stackoverflow.com/help/mcve). – user1803551 Oct 21 '14 at 00:30
  • *"i was thinking a simple JOption pane button list would be I just need some help with the implementation*" Good choice, what help do you need? What code did you write for it and where are you stuck? – user1803551 Oct 21 '14 at 00:31
  • As for the `JOptionPane`, you could read up on this: [Multiple input in JOptionPane.showInputDialog](http://stackoverflow.com/questions/6555040/multiple-input-in-joptionpane-showinputdialog). As for the pawn move, my best guess is that your program *thinks* it is white when it is actually black promoting. – Obicere Oct 21 '14 at 01:12
  • @user1803551 i have edited the java file above showing just the pawn moves and the change to the queen instead of the whole file, i have tryed to add a JOptionpane list where it is changing to the queen piece but just keep running into compiling errors in my code, can u offer some help, and if u need the source file it is all on github – Jamiex304 Oct 21 '14 at 10:16
  • 1
    You should consider using rather constants than literals and rather object orientation than writing a single monster method that handles every task. Your code could be less than half the size. – UniversE Oct 21 '14 at 10:22
  • @UniversE can u show me how i can do that the full java file can be found on github :) – Jamiex304 Oct 21 '14 at 10:28
  • *"i have tryed to add a JOptionpane list where it is changing to the queen piece but just keep running into compiling errors in my code"* Which errors? What does the compiler say? What line? – user1803551 Oct 21 '14 at 14:24
  • The code you posted does not compile. Your code snippet needs to be runnable (by us) when we copy-paste it into our IDE, dmonstrate the problem, and include only code relevant to the problem. If the problem is with a pawn move, remove all code not related to pawns and continue from there. – user1803551 Oct 21 '14 at 14:32
  • @user1803551 thank you for your valuable input so helpful – Jamiex304 Oct 21 '14 at 16:59
  • You still did not provide any information I asked for. Did you step with a debugger line by line to see where the problem is? – user1803551 Oct 23 '14 at 13:07
  • @user1803551 what is your problem first you tell me my code is to long so i only post the bits that matter then to try and add a JOption pane myself I do that and nothing happens there is no errors in my code when I compile it, when I say that you come back and tell me I should just delete all my code and start again, now your back bitching some more at me what the hell is your problem – Jamiex304 Oct 23 '14 at 13:27
  • I should note that your code is too tightly coupled on the presentation logic. A better idea would be to create a separate data model representing the chess board, the chess pieces, and their movement. As it stands now, your code checks for the position of each piece by looking up their JPanel positions (see your `piecePresent` method, for example); that approach makes it very hard to port your code to different environments, Android for example. – Peter O. Oct 23 '14 at 13:56

2 Answers2

2

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);
        }
    }
Marco13
  • 53,703
  • 9
  • 80
  • 159
  • thanks for your help and i understand the implementation is pretty bad overall, this is just the only way i can get it to work, every time I have tried to split up the code it just cause a number of error's , can you give me any advice on splitting it up such as an example, thanks gain tho – Jamiex304 Oct 25 '14 at 20:24
  • I already tried to give some hints, and some have already been given in the comments: You should try to separate the "Model" and the "View". The Model is the actual game, consisting of something like a `Board` class and a `Move` class. The View is the GUI, that only *shows* the board as it is. Detecting whether a chess move is valid is indeed a bit complicated, and you might end up with a set of non-trivial methods there. But for example: For an AI, you have to compute a *list* of all possible moves based on a given board. This could be a starting point. – Marco13 Oct 25 '14 at 23:13
1

Use this code:

String promoteTo;
do {
    promoteTo = (String) JOptionPane.showInputDialog(yourMainFrame,
        "Promote to:", "Promotion",
        JOptionPane.QUESTION_MESSAGE, null,
        new String[]{"Queen", "Bishop", "Knight", "Rook"}, "Queen");
} while (promoteTo == null);

But you should consider externalizing the string literals to constants or a resource file.

UniversE
  • 2,419
  • 17
  • 24
  • i have try using this code but it didnt help i can get it to compile without errors but nothing happens with either pawn now. maybe i am implenting it wrong can u maybe show me an implenation in my on code file above, the full fill can be found on github if u need it – Jamiex304 Oct 21 '14 at 23:51
  • Use a debugger and a create a breakpoint in your pawn code - then check it step by step. Unfortunately I don't have so much time to do this for you, but I can give you a few more tips for your code (and how you can find bugs more quickly): Use object orientation! 1. create classes Pawn, Bishop, etc. and 2. create methods like `doMove`, `checkMove` or `promote`. Without completely reading it, I barely believe, that your code is bug-free and respects all chess rules (like pinned figures etc.). You can get ideas from my project: http://develop.uap-core.de/hg/terminal-chess/file/tip/src/chess – UniversE Oct 24 '14 at 17:49