• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with loop in java  RSS feed

 
carlos novoa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Good morning dear forum members,

I am making a programm with java with backtracking algorithm. I have created
a method to represent the movements of the horse on a chessboard.

I am able to print the whole board after the algorithm, but what I want is to print every
new movement in a new board so that I can observe the sequence.

Can anybody tell what I have to do in the loop? If anybody could tell how I can print
the board as the algorithm moves on, that would be great.

Thanks in advance.

The loop I created produces the array "tabla" ( board in spanish).

This the code.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you can print out the whole board after the loop, why can't you print it out in the loop? Wouldn't the code be the same?

Hopefully, you created a printBoard() method (although I'd guess you named it in Spanish ). So, just call that method inside the loop, after you've made the move.
 
carlos novoa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there, it is not as esay as it looks. I tried to do it, but I print the movements not as I wanted.
There is some coding that does not work.

Regards.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Carlos,

some things that I see are:

in line 29 I see: tabla[fil] [col]= j;
but nowhere do I see any updating of this 'fil' and 'col'
This also affects lines 27 and 28.

in line 15: if ( tabla[i][j]== tabla[0][0]){
shouldn't that be: if (i == 0 && j == 0) {...
(I'm not sure with what these tabla-elements are filled;
this also holds for line 19, where you compare two elements of tabla)

Maybe you can check this?

Greetz,
Piet
 
carlos novoa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Piet,

I include the code for the two methods, one does the knight backtracking and the other
does the printing. I I follow your suggestion but nothing changes.

I dont know where to change the coding in the procesar method ( it does the backtracking) or escribir ( does the printing)
I am really stuck here.

public void procesar (int x, int y) {
int i = 0;
int j = 0;
int cuentasaltos = 0;
int fil = 0;
int col = 0;
int k = 0;
int cuento = 0;
int dondevoy = 0;
int nuevafil = 0;
int nuevacol = 0;

tabla[x][y] = 1;
fil = x;
col = y;

for (j = 2; j <= (FILAS*COLUMNAS); j++) {
cuentasaltos = 9;
for (i = 0; i < FILAS; i++) {
nuevafil = fil + desplaz_fil[i];
nuevacol = col + desplaz_col[i];
if (nuevafil >= 0 && nuevafil <= 7 && nuevacol >= 0 && nuevacol <= 7 && tabla[nuevafil][nuevacol] == 0){
cuento = 0;
for (k = 0; k < 8; k++) {
if ((nuevafil + desplaz_fil[k] >= 0 && nuevafil + desplaz_fil[k] <= 7)
&& (nuevacol + desplaz_col[k] >= 0 && nuevacol + desplaz_col[k] <= 7)
&& (tabla[nuevafil+desplaz_fil[k]][nuevacol+desplaz_col[k]] == 0)) {
cuento++;
}
}

if (cuento < cuentasaltos) {
cuentasaltos = cuento;
dondevoy = i;
}
}
}

fil = fil + desplaz_fil[dondevoy];
col = col + desplaz_col[dondevoy];
tabla[fil][col] = j;
}
}


public void escribir () {
int i = 0;
int j = 0;
for (i = 0; i < FILAS; i++) {
System.out.println(" -----------------------------------------");
for (j = 0; j < COLUMNAS; j++) {
if (tabla[i][j] > 9) {
System.out.print(" | " + tabla[i][j]);
}
else {
System.out.print(" | " + tabla[i][j] + " ");
}
}
System.out.println(" |");
}
System.out.println(" -----------------------------------------");
}
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Carlos,

I've tried to follow what is happening in your method "processar", but I don't understand what is going on here.
You seem to start from square [x,y], you mark tabla[x][y] with 1 (unfortunately, you did not put your code
between the code tags, so it makes following what happens extra difficult), you set the variables 'fil' and 'col' to x and y,
and then I think you are trying to locate the maximum 8 fields that can be reached by a knight, from this location [x,y].

So far I can follow. But then I see a j-loop, within it an i-loop and within that a k-loop, and here is where I can't follow
what's happening.

Before going to the printing problem, can you explain what the goal of the method "processar" is?
And then how you try to achieve that goal, with all these loops? I'm especially curious to know
what this loop involves:

 
carlos novoa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Piet,

very simple. The method "Procesar" in spanish " to process" does backtracking algorithm. I have not included the
whole program, but previously I set the movements of the knight. To make it clear, despla_fil and despla_col are
2 arrays which add a new row or column, every time the knight moves. Tabla is a bi-dimensional array which stores
the position of the knight through the algorithm. FILAS and COLUMNAS are again 2 arrays for the rows and the columns.

The print method "escribir" prints out the board and perfectly. Now coming back to my enquiry, I dont know exactly where
to place the condition to print the knight every step with a new board.

I have considered these options:

A) I can add another method which complements the "Procesar" method for the step by step movement.
I tried this but in Elipse I get an error that variables despla_fil and despla_col are not defined, though
they are in the main costructor.

B) Modify "Procesar" but I dont know exactly where to set the condition to stop in every cycle of the loop and print.


C) Create another class and inherit "Procesar" and here add a method which prints step by step. Again here, Elipse
does not recognize the constructor of the main class.

Any help will be appreciated!

Cheers

Carlos
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Carlos,

well, I must say I still don't understand what it is that you are trying to do. You speak of a backtracking
method, but in that case I would have expected to see some recursion taking place, and I see none
of this in the method "processar". So, obviously, you are doing this somewhere else.

But you want to see every move that the knight is taking. The place where that happens is, I think, where you
set a value in your tabla array.I see two places in the "processar" method where this filling takes place:


and

Since you are only interested in printing one move at a time, I would suggest to simply print some thing like

or


But I don't know if these lines do in fact represent a new move.

So, I think what I write is possibility B.

Hope this helps.

Greetings,
Piet
 
carlos novoa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi Piet.

Thanks for your efforts. Nearly there. I am going to explain to you graphically what I intend to do.

The method "procesa" does the bt, the method escribir "prints the results".

What I am trying to do it:

1. Move of knight

----------------------------------
1







----------------------------------

2. Move of knight

----------------------------------
1
2





-----------------------------------



3. Move of knight

-----------------------------------
1
2


3




-----------------------------------

4. Move of knight

-----------------------------------

1
2
3


4


------------------------------------


You got me?

Of course your contribution is a good idea. But I cannot call a method before method. And this
is the whole point, I cannot use "escribir" inside "procesar because they are different.

If you have any idea...

Carlos










 
carlos novoa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I just viewed the board and is not entirely correct but you get the idea, the movement should be the movement
of the knight on the board in a sequence.
Cheers
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carlos, I have been trying to follow this conversation for a while, but like Piet, I can't follow what you want or what the problems you are having. I think you need to do a couple things:
1) Read this: UseCodeTags. It makes the code easier to read. Easier to read means better help. Not using code tags makes it harder to help and more likely that fora goers with limited time skip your posts or don't try too hard to decipher what you have. So it behooves you to always use code tags

2) Show complete code. It is really hard to see what you want. It is really hard to infer what is actually happening when you don't give runnable code so we can compile and run and see for ourselves. If the program is large, then cut out the important parts and create an SSCCE (<- link) that is small and easy to follow.

3) Be complete about your problem. What do you want? What do you get? How are they different? What error messages? etc..

4) At this point, you seem to be casting about in code trying to guess on what to do next. This is a bad idea. It gets harder to program with each bad guess, and harder to just scratch what you have and start over. Instead start with a pencil and paper. Write down in your native language what you want to do, step by step. Then take those steps and make them simpler. Simplify them line by line to get a sequence of commands with very simple sentence structure (something you could give to a 10 year old, and they would understand). Then follow through the sequence and make sure it produces the result you want (still on paper). When you get it right, then turn it to Java.
 
carlos novoa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Good evening.

Ok sorry guys. I did not put the whole code because I did not want to abuse this forum.

I will make it clear.

The purpose of the program is to move a knight thoughout the chessboard and cover all the
squares with its movement. Only the knight moves on the board.
The program must use the knight and with the bt algorithm find the best choice, in the end
the chessboard should be covered with the sequence of the movement of the knight. That
is the objective.

My program does that and I get a board with the result of all the movements. What I want
to program is the sequence on the board of the knight until completing the whole board.

And here is where I am stuck.

I can print a sequence with java System.out.println( " The movement added is" + x + y ) like you suggested before, but what I want
is to use a method to print this sequence. And here is the question, where to do it or how to do it. My idea is to use the array tabla[x][y]
and print the results as the loop sequence advances. But if I do I get an error "OutOfBoundsException", the variable "SaltoDelCaballo" is not
defined for the "procesar", etc...


I add here the code.




 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
carlos novoa wrote:But if I do I get an error "OutOfBoundsException", the variable "SaltoDelCaballo" is not
defined for the "procesar", etc...


What is the exact error message(s)? Copy and past them, and their stack trace. The more details you provide the better the help you will get.

p.s. Did you use pencil and paper to write out your plan like I suggested?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Carlos,

I am not surprised to read that you are working on the classic problem of all these knight moves.
visiting all squares of the board once.

The problem is: the algorithm that you use, well, I haven't been able to figure out how it works!
But your printing problem has to do with how the algorithm finds its way in the tree.

Right now I am thinking of an ArrayList<Point> or LinkedList<Point>, and each time you discover
a new square that the knight can visit, add that square to the list, and print the list.
But that must only be done if you are going in a "forward" direction, and removing the square
from the List if you are moving backward.

If the algorithm is using some form of "breadh first" pattern, then it will be somewhat more difficult,
but then you must also define in what direction you are travelling.

Can you explain the algorithm? What values do you put into your tabla array, for instance?

Greetings,
Piet
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!