I have a problem with my program, the point is that I want to define rules regarding the positioning of one of the queens of the board so that in this way it only moves to those defined spaces, in the program I define everything with statements of if
but my The problem is that when you want to make it dynamic, those statements if
stop working since they are only defined for the first spaces of 8*8
and what I want to achieve is that these rules change with the number of queens on the board.
MOVEMENT RULES
public int[] posicion(MouseEvent evt) {
int newX = 0, newY;
if ((evt.getXOnScreen() - 50) <= 25) {
newX = 25;
} else if ((evt.getXOnScreen() - 50) <= 75) {
newX = 75;
} else if ((evt.getXOnScreen() - 50) <= 125) {
newX = 125;
} else if ((evt.getXOnScreen() - 50) <= 175) {
newX = 175;
} else if ((evt.getXOnScreen() - 50) <= 225) {
newX = 225;
} else if ((evt.getXOnScreen() - 50) <= 275) {
newX = 275;
} else if ((evt.getXOnScreen() - 50) <= 325) {
newX = 325;
} else if ((evt.getXOnScreen() - 50) <= 375) {
newX = 375;
} else {
newX = 375;
}
if ((evt.getYOnScreen() - 50) <= 25) {
newY = 25;
} else if ((evt.getYOnScreen() - 50) <= 75) {
newY = 75;
} else if ((evt.getYOnScreen() - 50) <= 125) {
newY = 125;
} else if ((evt.getYOnScreen() - 50) <= 175) {
newY = 175;
} else if ((evt.getYOnScreen() - 50) <= 225) {
newY = 225;
} else if ((evt.getYOnScreen() - 50) <= 275) {
newY = 275;
} else if ((evt.getYOnScreen() - 50) <= 325) {
newY = 325;
} else if ((evt.getYOnScreen() - 50) <= 375) {
newY = 375;
} else {
newY = 375;
}
int retorno[] = {newX, newY};
return retorno;
}
TRY
int ite_Uno = 0;
int ite_Dos = 0;
for (int i = 0; i < tablero.length; i++) {
ite_Uno = 25 * (50 * i);
if (evt.getXOnScreen() > ite_Dos && evt.getXOnScreen() < ite_Uno) {
newX = ite_Uno;
}
ite_Dos = 25 * (50 * i);
}
This was what I came up with but it doesn't work and I also don't think it's the most appropriate since the piece will be moving constantly and I think that one
for
is not the most appropriate but if it worksfor
with that I'll settle for it.
FULL CODE
package tablerojava;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
public class TableroJava extends JFrame {
static int reinas = 0;
int count = 0;
int anchoAlto = 50;
int margen = 25;
int espacio = 50;
JPanel jpanel = (JPanel) this.getContentPane();
JLabel ex = new JLabel();
JLabel label[] = new JLabel[reinas];
JLabel tablero[][] = new JLabel[reinas][reinas];
Border border = BorderFactory.createLineBorder(Color.yellow, 1);
public static void main(String[] args) {
reinas = Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese la cantidad de reinas"));
TableroJava op = new TableroJava();
op.setBounds(0, 0, (60 * reinas), (60 * reinas));
op.setVisible(true);
op.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public TableroJava() {
for (int i = 0; i < label.length; i++) {
label[i] = new JLabel();
// r + r + r + r
label[i].setBounds(margen + (espacio * i), margen, anchoAlto, anchoAlto);
label[i].setText("Q" + (i + 1));
label[i].setForeground(Color.red);
label[i].setBorder(border);
label[i].setHorizontalAlignment(SwingConstants.CENTER);
label[i].addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent evt) {
arrastreReina(evt);
}
});
label[i].addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent evt) {
movimientoReina(evt);
}
});
jpanel.add(label[i]);
}
for (int i = 0; i < tablero.length; i++) {
for (int j = 0; j < tablero.length; j++) {
tablero[i][j] = new JLabel();
tablero[i][j].setBounds(margen + (espacio * i), margen + (espacio * j), anchoAlto, anchoAlto);
tablero[i][j].setBorder(border);
if ((i%2 == 0) == (j%2 == 0)) {
tablero[i][j].setBackground(Color.white);
} else {
tablero[i][j].setBackground(Color.black);
}
tablero[i][j].setOpaque(true);
tablero[i][j].setHorizontalAlignment(SwingConstants.CENTER);
jpanel.add(tablero[i][j]);
}
}
ex.setBounds(margen, margen, anchoAlto, anchoAlto);
jpanel.add(ex);
}
public void arrastreReina(MouseEvent evt) {
if(evt.getSource() instanceof JLabel) {
((JLabel) evt.getSource()).setLocation(posicion(evt)[0], posicion(evt)[1]);
}
}
public void movimientoReina(MouseEvent evt) {
if(evt.getSource() instanceof JLabel) {
count++;
System.out.println("Movimientos: " + count);
}
}
public int[] posicion(MouseEvent evt) {
int newX = 0, newY;
if ((evt.getXOnScreen() - 50) <= 25) {
newX = 25;
} else if ((evt.getXOnScreen() - 50) <= 75) {
newX = 75;
} else if ((evt.getXOnScreen() - 50) <= 125) {
newX = 125;
} else if ((evt.getXOnScreen() - 50) <= 175) {
newX = 175;
} else if ((evt.getXOnScreen() - 50) <= 225) {
newX = 225;
} else if ((evt.getXOnScreen() - 50) <= 275) {
newX = 275;
} else if ((evt.getXOnScreen() - 50) <= 325) {
newX = 325;
} else if ((evt.getXOnScreen() - 50) <= 375) {
newX = 375;
} else {
newX = 375;
}
if ((evt.getYOnScreen() - 50) <= 25) {
newY = 25;
} else if ((evt.getYOnScreen() - 50) <= 75) {
newY = 75;
} else if ((evt.getYOnScreen() - 50) <= 125) {
newY = 125;
} else if ((evt.getYOnScreen() - 50) <= 175) {
newY = 175;
} else if ((evt.getYOnScreen() - 50) <= 225) {
newY = 225;
} else if ((evt.getYOnScreen() - 50) <= 275) {
newY = 275;
} else if ((evt.getYOnScreen() - 50) <= 325) {
newY = 325;
} else if ((evt.getYOnScreen() - 50) <= 375) {
newY = 375;
} else {
newY = 375;
}
int retorno[] = {newX, newY};
return retorno;
}
}
The best way I see to simplify for this case is to create a method to which you pass the value of
X
orY
, which does the calculation and returns the position.For example: