עזרה JAVA

ehudl1

New member
עזרה JAVA

שלום לכולם,

אודה לעזתכם הטובה כתמיד.

לאחר התקדמות קטנה (סוף סוף :) )
נתקעתי בעוד נקודה שלא מסתדרת לי מסיבה לא ברורה (לפחות לעין).
אעתיק לכאן חלקים מהקוד עם הסבר על השלבים כיצד זה מתגלגל ואראה תמונה עם השגיאה.


arr=soldier.setOptions(x,y);
arr=tool.checkLigal(arr, x, y);

הנה המתודות:

public ArrayList setOptions(int i, int j)
{
InfoPlace info1= new InfoPlace();
info1.setX(i);
info1.setY(j+1);
if (j<8)
arr.add(info1);

InfoPlace info2= new InfoPlace();
info2.setX(i+1);
info2.setY(j);
if (i<8)
arr.add(info2);

InfoPlace info3= new InfoPlace();
info3.setX(i-1);
info3.setY(j);
if (i>-1)
arr.add(info3);

InfoPlace info4= new InfoPlace();
info4.setX(i);
info4.setY(j-1);
if (j>-1)
arr.add(info4);



public ArrayList checkLigal(ArrayList arr, int _x, int _y)
{
ArrayList newArr = new ArrayList();
System.out.println("_x: "+_x+" _y: "+_y);
System.out.println("arrSize checkLigal: "+arr.size());

for (int k=0; k < arr.size(); k++)
{
InfoPlace info=(InfoPlace) arr.get(k);
int x=info.getX();
int y=info.getY();

if (startWith(x,y)!=startWith(_x,_y))
newArr.add(info);
}
System.out.println("newArrSize checkLigal: "+newArr.size());
return newArr;
}

עד כאן כביכול כל מה שעשיתי הוא לבדוק האם הנקודות "חוקיות" לתזוזה על הלוח.
אם הן חוקיות אני נותן אופציית תזוזה (במקרה הזה מראה את האופציות).
משום מה בפעם השנייה המערך שלי מכיל 8 איברים ולא רק 4 ואז מסנן אותם.
כלומר: כל פעם כרגע יש לי 4 אופציות ומתוכן אני מקבל את מה שאפשר "לעבוד" איתו, בפעם השנייה הוא מכיל 8 ואז מסנן ל- 4, ובכלל הן לא נכונות.


tool.paintOptions(buttons, arr);



public void switcButtonshOnBoard(int x, int y, int i_x, int i_y, JButton[][] buttons, ArrayList arr)
{
InfoButton infoButton= new InfoButton();

for (int k=0; k < arr.size(); k++)
{
InfoPlace info= (InfoPlace) arr.get(k);

if (i_x==info.getX() && i_y==info.getY())
{
buttons[i_x][i_y].setIcon(buttons[x][y].getIcon());
boolean color=infoButton.whatColor(info.getX(), info.getY());
if (color==true)
buttons[i_x][i_y].setBackground(Color.black);
else buttons[i_x][i_y].setBackground(Color.white);
arr.remove(k);

if (buttons[x][y].getBackground()==Color.black)
{
buttons[x][y].setIcon(black);
}
else
{
buttons[x][y].setIcon(white);
}
}
}

for (int k=0; k < arr.size(); k++)
{
InfoPlace info= (InfoPlace) arr.get(k);
boolean color=infoButton.whatColor(info.getX(), info.getY());
if (color==true)
{
buttons[i_x][i_y].setIcon(black);
buttons[i_x][i_y].setBackground(Color.black);
}
else
{
buttons[i_x][i_y].setIcon(white);
buttons[i_x][i_y].setBackground(Color.white);
}
arr.remove(k);
}

כפי שאפשר לראות אני כן מסיר את האיברים מהמערך, כך שלא אמור להישאר לי כלום בסופו של דבר.
מאיפה אני מקבל את ה- 8 איברים אני לא מבין?

תודה.
 
למעלה