טעות לא ברורה

ehudl1

New member
טעות לא ברורה

שלום,
הכל נראה על פניו הגיוני, אבל ישנה טעות בסופו של דבר במקום כלשהו.
אני מזיז "כלי" על המסך, בפעם הראשונה הכל בסדר, בפעם השנייה כבר קיימת בעיה.
מסיבה לא ברורה איפה שסימנתי (X) לא משתנה הצבע של הרקע למרות שהגדרתי שהם יהיו null ועל פי הקוד אם יש null זה משתנה.

public void searchTool(String str, int i, int j)
{
char s=str.charAt(0);

String tool=str.substring(6);

if (tool.equals("soldier"))
{
char right='n';
char right2='n';
char left='n';
char left2='n';
char up='n';
char up2='n';
char down='n';
char down2='n';

if (j+1<=7)
{
right=B.startWith(i, j+1);
if (right!=s)
{
InfoButton info= new InfoButton(i, j+1, buttons[j+1].getBackground());
buttons[j+1].setBackground(Color.red);
a.add(info);
}
}
if (j+2<=7)
{
right2=B.startWith(i, j+2);
if (right2!=s)
{
InfoButton info= new InfoButton(i, j+2, buttons[j+2].getBackground());
buttons[j+2].setBackground(Color.red);
a.add(info);
}
}
if (j-1>=0)
{
left=B.startWith(i, j-1);
if (left!=s)
{
InfoButton info= new InfoButton(i, j-1, buttons[j-1].getBackground());
buttons[j-1].setBackground(Color.red);
a.add(info);
}
}
if (j-2>=0)
{
left2=B.startWith(i, j-2);
if (left2!=s)
{
InfoButton info= new InfoButton(i, j-2, buttons[j-2].getBackground());
buttons[j-2].setBackground(Color.red);
a.add(info);
}
}
if (i+1<=7)
{
down=B.startWith(i+1, j);
if (down!=s)
{
InfoButton info= new InfoButton(i+1, j, buttons[i+1][j].getBackground());
buttons[i+1][j].setBackground(Color.red);
a.add(info);
}
}
if (i+2<=7)
{
down2=B.startWith(i+2, j);
if (down2!=s)
{
InfoButton info= new InfoButton(i+2, j, buttons[i+2][j].getBackground());
buttons[i+2][j].setBackground(Color.red);
a.add(info);
}
}
if (i-1>=0)
{
up=B.startWith(i-1, j);
if (up!=s)
{
InfoButton info= new InfoButton(i-1, j, buttons[i-1][j].getBackground());
buttons[i-1][j].setBackground(Color.red);
a.add(info);
}
}
if (i-2>=0)
{
up2=B.startWith(i-2, j);
if (up2!=s)
{
InfoButton info= new InfoButton(i-2, j, buttons[i-2][j].getBackground());
buttons[i-2][j].setBackground(Color.red);
a.add(info);
}
}
} // soldier
} // searchTool


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

public void Switch (int x1, int y1, int x2, int y2)
{
if(board[x1][y1]==null)
{
board[x1][y1]=board[x2][y2];
board[x2][y2]=null;
}
}

public String getUniqName (String str)
{
char x=str.charAt(2);
char y=str.charAt(6);

int i_x=Character.getNumericValue(x);
int i_y=Character.getNumericValue(y);

if (board[i_x][i_y]!=null)
return board[i_x][i_y];

return null;

}

public char startWith (int x, int y)
{
if (board[x][y]!=null)
{
char c=board[x][y].charAt(0);
return c;
}
else return 'n';
}

public String whoIs (int x, int y)
{
return board[x][y];
}


תודה רבה.
 

ehudl1

New member
או לזה:

public char startWith (int x, int y)
{
System.out.println("board[x][y](startwith): "+"x="+x+"y="+y+board[x][y]);

if (board[x][y]!=null)
{
char c=board[x][y].charAt(0);
return c;
}
else return 'n';
}

הוספתי שורות הדפסה לבדיקה ומשום מה גם כאשר שמתי במקום null הוא עדיין מחזיר לי תו שהוא שונה מ- n.
 

nocgod

New member
הייתי מנסה לעזור

אבל קשה להכנס ולהבין מקור בעיות בגזירי קוד בלי רפרנס במה מדובר ומה המערכת עושה, אולי זה כי אני לא מנוסה מי יודע מה...
 

ehudl1

New member
אני אסביר

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

תודה.
 
למעלה