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.

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.

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

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

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?

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.

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.

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

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.

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

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