Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 4 of 9 FirstFirst ... 23456 ... LastLast
Results 76 to 100 of 219

Thread: Chess Program

  1. #76
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    I don't know what an array would be used for. The String class has methods for getting at each of the characters in the String.

    how do i set the 8 slashes / so they never change? they are always there
    I don't know what you are asking. I have been talking about the code for a method that gets the FEN-notation String and retrieves the pieces and their locations from it.

    Its best to work on one thing at a time. When the method to parse the String is working, then move on to the next problem.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #77
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    Can you please explain how i am going to build this, how do i place pieces in the gridlayout since there are 64 square objects jlabels in the gridlayout

    how do i couple the first character of the FEN notation to the first square in the gridlayout(upper left) and how do to show character 8 or for example 2 P 5 when a pawn is placed.

    Can you please help me which code i need i really dont know how to build this
    System.out.println(" dream in code ");

  3. #78
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    how do i place pieces in the gridlayout since there are 64 square objects jlabels in the gridlayout
    Earlier I explained how you can convert row/column coordinates to a single index:
    index = row * nbrColInRow + col;
    The coordinates for the squares on the board: upper left = 0,0 and the lower right = 7,7

    You need to use variables to keep track of the row and the column of each character as it is taken from the FEN-notation String. The first char would be at row=0 and col=0.
    Advance the col while scanning the characters.
    Advance the row and reset the col to 0 when a / is found.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #79
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    can you make an example? sorry for this but i really dont know how to build it

    index = row * nbrColInRow + col;
    row = integer?
    nbrColInRow = integer?
    col = integer? how does the method or class knows this is about the ArrayList<Square> squares because the gridlayout is only a GUI, the ArrayList contains the squares from 0 to 64

    The coordinates for the squares on the board: upper left = 0,0 and the lower right = 7,7
    are these the gridlayout coordinates
    System.out.println(" dream in code ");

  5. #80
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    are these the gridlayout coordinates
    Where are the Squares located when they are added to the gridlayout? Does the first one go in the upper left and the last one go in the lower right?

    In my example row and col are the integer index values for a square on the board. 0,0 could be for the upper left and 7,7 for the lower right. The formula I gave is used to convert the row and col values to the index into the arraylist that has 64 elements.

    To see, take a piece of paper, draw an 8x8 grid and write the numbers 0 to 63 in the squares of the grid. one number in each square starting at the top left and going row by row:
    0 1 2 3 4 5 6 7
    8 9 10 11 12 13 14 15
    ...
    ... 62 63
    Then using the formula I gave, see how given a row and col value it will return the value written in that square of the grid. rows and cols are 0 based.
    row=1, col=2 gives 1*8+2 = 10
    If you don't understand my answer, don't ignore it, ask a question.

  6. #81
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    for (int row = 0; row < 8; row++) {
                for (int col = 0; col < 8; col++){
     
                }
    }

    something like this?

    Where are the Squares located when they are added to the gridlayout?
    in a class ChessBoard

    package model;
     
    import java.util.Set;
    import java.util.ArrayList;
     
    import javax.swing.ImageIcon;
     
    public class ChessBoard {
     
        private static ArrayList<ChessPiece> pieces;
        private ArrayList<Square> squares;
        private static final String[] LOCATIONS = {
        "A8","B8","C8","D8","E8","F8","G8","H8",
        "A7","B7","C7","D7","E7","F7","G7","H7",
        "A6","B6","C6","D6","E6","F6","G6","H6",
        "A5","B5","C5","D5","E5","F5","G5","H5",
        "A4","B4","C4","D4","E4","F4","G4","H4",
        "A3","B3","C3","D3","E3","F3","G3","H3",
        "A2","B2","C2","D2","E2","F2","G2","H2",
        "A1","B1","C1","D1","E1","F1","G1","H1",
        };
     
        public ChessBoard(){ 
            squares = new ArrayList<Square>();
            pieces = new ArrayList<ChessPiece>();
            addLocations();
     
            setSquareColors();
            makeChessPieces();
        }
     
        private void addLocations(){
            for(int i = 0; i < LOCATIONS.length; i++){
            	squares.add(new Square(LOCATIONS[i]));    
            }       
        }
     
        private void setSquareColors(){
        	for(Square square: squares){
        		if(square.toString().matches("A1|C1|E1|G1|B2|D2|F2|"
        				+ "H2|A3|C3|E3|G3|B4|D4|F4|H4|A5|C5|E5|G5|"
        				+ "B6|D6|F6|H6|A7|C7|E7|G7|B8|D8|F8|H8")){
        			square.setColor("BROWN");
        		}
        		else if(square.toString().matches("A2|C2|E2|G2|B1|D1|F1|"
            			+ "H1|A4|C4|E4|G4|B3|D3|F3|H3|A6|C6|E6|G6|"
            			+ "B5|D5|F5|H5|A8|C8|E8|G8|B7|D7|F7|H7")){
            		square.setColor("WHITE");
        		}
        	}
        }
     
        public Square getSquare(int i){
     
        	return squares.get(i);
        }
     
        public static ChessPiece getPiece(int i){
        	return pieces.get(i);
        }
     
        public String getSquareColor(int i){
            return squares.get(i).getColor();
        }
     
        public ArrayList<Square> getAllSquares(){
        	return squares;
        }
     
        public static ArrayList<ChessPiece> getAllPieces(){
        	return pieces;
        }
     
        public void setSquareImage(int i, ImageIcon image){
        	getSquare(i).setImage(image);
        }
     
        public static void setPieceImage(int i, ImageIcon image){
        	getPiece(i).setImage(image);
        }
     
        public String toString(){
        	return squares.toString();
        }
     
        private void makeChessPieces(){
        	String letterpb = "p";
        	String namepb = "pawn";
        	String colorpb = "BLACK";
        	pieces.add(new ChessPiece(namepb, letterpb, colorpb));
     
        	String letterrb = "r";
        	String namerb = "rook";
        	String colorrb = "BLACK";
        	pieces.add(new ChessPiece(namerb, letterrb, colorrb));
     
        	String letternb = "n";
        	String namenb = "knight";
        	String colornb = "BLACK";
        	pieces.add(new ChessPiece(namenb, letternb, colornb));
     
        	String letterbb1 = "b1";
        	String namebb1 = "bishop1";
        	String colorbb1 = "BLACK";
        	pieces.add(new ChessPiece(namebb1, letterbb1, colorbb1));
     
        	String letterbb2 = "b2";
        	String namebb2 = "bishop2";
        	String colorbb2 = "BLACK";
        	pieces.add(new ChessPiece(namebb2, letterbb2, colorbb2));
     
        	String letterqb = "q";
        	String nameqb = "queen";
        	String colorqb = "BLACK";
        	pieces.add(new ChessPiece(nameqb, letterqb, colorqb));
     
        	String letterkb = "k";
        	String namekb = "king";
        	String colorkb = "BLACK";
        	pieces.add(new ChessPiece(namekb, letterkb, colorkb));
        }
    }
    System.out.println(" dream in code ");

  7. #82
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    Make a list of the steps the code needs to do BEFORE writing any code. Work out the logic first.

    The changing of the row and col values depends on the characters in the FEN-notation string.
    You can't write nested loops to change the values of row and col. Their values depend on the data in the string.
    The loop would be over the characters in the String.

    Where are the Squares located when they are added to the gridlayout?
    I was asking about where the Square objects display in the GUI when they are added to the gridlayout.
    For example:
    Where is the Square with location="A1"? Where is the Square with location="C2"? etc
    At what row and col are those Squares shown?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #83
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    Screen Shot 2013-08-13 at 6.31.51 PM.jpg

    Location of the squares

    can you see them? they are a bit small but A8 upper left and H1 lower right
    System.out.println(" dream in code ");

  9. #84
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    Are the squares displayed where you expect them to be shown?
    If they are, then move on to the logic needed to parse the FEN-notation string.

    Do you have a list of the steps the program needs to take to parse the FEN-notation string?
    It should get the piece and its location on the board as the string is scanned.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #85
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    what can i use to check the characters in a string
    String FenParser = "rnbqkbnr/ppppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
    one by one so first r next n next b next q....next / next p etc
    System.out.println(" dream in code ");

  11. #86
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    what can i use to check the characters in a string
    See the API doc for the String class. It has methods for getting the characters in a String.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #87
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    this is so *king difficult! i really cant get it to work
    System.out.println(" dream in code ");

  13. #88
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    i really cant get it to work
    You need to define what this part of the program is supposed to do before writing any code.
    Post the logic and the steps that the code must take to do the job.
    Then work on one simple step at a time.
    For example the code needs to look at the characters in the string one at a time.
    To do that, write a loop, get the characters one at a time and print them out.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #89
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    i know but i still have to place the pieces on the board

    really need help with code what to do! Advice is also helpful, but i need help with code its so difficult!

    i have to count the squares as well, like this /2p5/8/8/8/PPPPPPPP etc
    2 empty squares, one pawn, 5 empty squares
    System.out.println(" dream in code ");

  15. #90
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    As I said before several times: make a design BEFORE writing any code.
    what is your design for this part of the program?
    You can not write code without a design.

    have to place the pieces on the board
    Describe your problem. The parsing of the FEN-notation string gives the row, column and piece.
    Given those three items, you could call a method in the chessboard class to put the piece on that row and column.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #91
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    *up the program all the squares are white instead of white and brown

    goddamn

    Okay i fixed the squares

    --- Update ---

    - It has to read the FEN-notation
    - Check the FEN-notation (is it a correct FEN notation Length and legal characters otherwise exception e, if a "row" is empty it shows 8) but thats is diffibult i dont work with rows right only the location shows the row number
    - press enter
    - The piece objects need to be placed on the right location.

    --- Update ---

    Maybe i sound really stupid and i know what you mean by make a design before writing code! thats why they learn students to use unified modeling language (UML) but i dont have time for that at the moment i need that program really bad so i want it to work, maybe i am a bit to stubborn.

    and i dont know what you mean by counting row and collum since i have an ArrayList with 64 squares
    Last edited by Wolverine89; August 13th, 2013 at 03:38 PM. Reason: Fixed
    System.out.println(" dream in code ");

  17. #92
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    All those steps are simple except this one:
    Check the FEN-notation (is it a correct FEN notation Length and legal characters otherwise exception e, if a "row" is empty it shows 8) but thats is diffibult i dont work with rows right only the location shows the row number
    What are the steps needed to do that one?
    A suggestion: don't bother checking the FEN string before trying to scan/parse it. Detect any errors as it is parsed. What would be the problem with scanning the string and using its values directly instead of first doing a scan for correctness?

    i dont work with rows right
    What dpes that mean? What problems are you having with the 8 rows in a FEN string?

    --- Update ---

    counting row and collum
    The FEN string gives the row and column location for all the pieces.

    Did you do what I suggested in post#80
    To see, take a piece of paper, draw an 8x8 grid and write the numbers 0 to 63 in the squares of the grid. Then using the formula I gave, see how given a row and col value it will return the value written in that square of the grid.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #93
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    What dpes that mean? What problems are you having with the 8 rows in a FEN string?
    how to access the arraylist<squares> for example the third row how to access those squares and put a JLabel on it!

    is this formula for the fen notation? index = row * nbrColInRow + col;

    i really dont get it can you give an example so i see how it works index = row * nbrColInRow + col;
    System.out.println(" dream in code ");

  19. #94
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    how to access the arraylist<squares>
    Use that equation to convert the row and column to the arraylist index.

    Here's a simple start that may give you some ideas.
    Write a program that scans the FEN string and prints out the piece, row and column. The output should look like this:
    r@r=0,c=0
    n@r=0,c=1
    b@r=0,c=2
    ...
    r@r=0,c=7
    p@r=1,c=0
    p@r=1,c=1
    ...
    p@r=1,c=7
    p@r=6,c=0
    p@r=6,c=1
    ...
    r@r=7,c=0
    ...
    r@r=7,c=7

    I'm assuming the top left corner is 0,0 and the bottom right is 7,7

    --- Update ---

    see how it works index = row * nbrColInRow + col;
    Did you Draw the diagram as I suggested in post#80?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #95
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    Yes, made a grid 8,8 like a chess board and started in the upper left corner with 0 and ended lower right with 63

    index = row * nbrColInRow + col; for instance rook = int index = 0 * 0 + 0;
    index = row * nbrColInRow + col; for instance knight = int index = 0 * 1 + 1;
    index = row * nbrColInRow + col; for instance pawn = int index = 1 * 0 + 0;

    I really appreciate all of your help so far! Next week is the deadline then i have to show my Chess Fen Program
    System.out.println(" dream in code ");

  21. #96
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    What about the simple program that scans a FEN string and prints out the piece, row and column that I suggested in post#94?

    When that works, its an easy step to write a method in the Chessboard class that takes that info and puts the piece on the square.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #97
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    public class Fen {
     
    	private final int NUM_OF_COLS = 8, NUM_OF_ROWS = 8;
    	private String FenParser;
    	private static ChessBoard chessboard;
     
    	private ChessPiece chesspiece;
     
    	public Fen(){
    		defaultFen();
    		coorToSqi(0,0);
    	}
     
        public Square coorToSqi(int col, int row)
    	{
    	        int index = row * NUM_OF_COLS + col;
    	        System.out.println(chessboard.getAllSquares().get(index));
    	        return chessboard.getAllSquares().get(index);
     
    	}

    getting a nullpointer
    System.out.println(" dream in code ");

  23. #98
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    Please copy the full text of the error message paste it here if you need help with it.

    The message gives the line number where there is a variable with a null value.

    Instead of trying to write code, you need to design what the code is supposed to do. What is the posted code supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  24. #99
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    Exception in thread "main" java.lang.NullPointerException
    at model.Fen.coorToSqi(Fen.java:21)
    at model.Fen.<init>(Fen.java:15)
    at view.ChessBoardView.<init>(ChessBoardView.java:38)
    at controller.Controller.makeFrame(Controller.java:45 )
    at controller.Controller.<init>(Controller.java:24)
    at controller.Main.main(Main.java:9)

    the nullpointer starts in the fen class line 21 where the system.out.println is
    System.out.println(" dream in code ");

  25. #100
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    What variable at line 21 has the null value? Find that variable and then backtrack in the code to see what that variable does not have a valid non-null value.

    Have you written the method I suggested in post#94 that takes a FEN string and prints out the piece, row and column for all the pieces in the string?

    Why is the coorToSqi() method in the Fen class?
    Shouldn't it be in the Chessboard class? It does the same thing the getSquare() method does.
    If you don't understand my answer, don't ignore it, ask a question.

Page 4 of 9 FirstFirst ... 23456 ... LastLast

Similar Threads

  1. Simple chess program (no AI) - Need help with structuring my code
    By MineeMo in forum Object Oriented Programming
    Replies: 6
    Last Post: June 18th, 2012, 10:12 AM
  2. Chess game help
    By that_guy in forum Java Theory & Questions
    Replies: 3
    Last Post: December 4th, 2011, 08:42 PM
  3. checking for draw by repetition in chess app
    By aisthesis in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 16th, 2011, 02:40 AM
  4. Chess Problem
    By pmg in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 10th, 2011, 12:07 PM
  5. Simple Chess program
    By x3rubiachica3x in forum What's Wrong With My Code?
    Replies: 23
    Last Post: September 22nd, 2010, 11:12 AM