הזזה על הלוח

ehudl1

New member
הזזה על הלוח

שלום,

ע"מ שיהיה נוח מצורף קטע קוד יחסית גדול:


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 (B.startWith(i, j+1)!=s)
{
InfoButton info= new InfoButton(i, j+1, buttons[j+1].getBackground());
buttons[j+1].setBackground(Color.red);
if (buttons[j+1].getIcon()==black || buttons[j+1].getIcon()==white)
buttons[j+1].setIcon(red);
a.add(info);
}
}
if (j+2<=7)
{
right2=B.startWith(i, j+2);
if (B.startWith(i, j+2)!=s)
{
if (right!=s && (right!=right2 || (right=='n' && right2=='n')))
{
InfoButton info= new InfoButton(i, j+2, buttons[j+2].getBackground());
buttons[j+2].setBackground(Color.red);
if (buttons[j+2].getIcon()==black || buttons[j+2].getIcon()==white)
buttons[j+2].setIcon(red);
a.add(info);
}
}
}
if (j-1>=0)
{
left=B.startWith(i, j-1);
if (B.startWith(i, j-1)!=s)
{
InfoButton info= new InfoButton(i, j-1, buttons[j-1].getBackground());
buttons[j-1].setBackground(Color.red);
if (buttons[j-1].getIcon()==black || buttons[j-1].getIcon()==white)
buttons[j-1].setIcon(red);
a.add(info);
}
}
if (j-2>=0)
{
left2=B.startWith(i, j-2);
if (B.startWith(i, j-2)!=s)
{
if (left!=s && (left!=left2 || (left=='n' && left2=='n')))
{
InfoButton info= new InfoButton(i, j-2, buttons[j-2].getBackground());
buttons[j-2].setBackground(Color.red);
if (buttons[j-2].getIcon()==black || buttons[j-2].getIcon()==white)
buttons[j-2].setIcon(red);
a.add(info);
}
}
}
if (i+1<=7)
{
down=B.startWith(i+1, j);
if (B.startWith(i+1, j)!=s)
{
InfoButton info= new InfoButton(i+1, j, buttons[i+1][j].getBackground());
buttons[i+1][j].setBackground(Color.red);
if (buttons[i+1][j].getIcon()==black || buttons[i+1][j].getIcon()==white)
buttons[i+1][j].setIcon(red);
a.add(info);
}
}
if (i+2<=7)
{
down2=B.startWith(i+2, j);
if (B.startWith(i+2, j)!=s)
{
if (B.startWith(i+1, j)!=s)
{
InfoButton info= new InfoButton(i+2, j, buttons[i+2][j].getBackground());
buttons[i+2][j].setBackground(Color.red);
if (buttons[i+2][j].getIcon()==black || buttons[i+2][j].getIcon()==white)
buttons[i+2][j].setIcon(red);
a.add(info);
}
}
}
if (i-1>=0)
{
up=B.startWith(i-1, j);
if (B.startWith(i-1, j)!=s)
{
InfoButton info= new InfoButton(i-1, j, buttons[i-1][j].getBackground());
buttons[i-1][j].setBackground(Color.red);
if (buttons[i-1][j].getIcon()==black || buttons[i-1][j].getIcon()==white)
buttons[i-1][j].setIcon(red);
a.add(info);
}
}
if (i-2>=0)
{
up2=B.startWith(i-2, j);
if (B.startWith(i-2, j)!=s)
{
if (B.startWith(i-1, j)=='n')
{
InfoButton info= new InfoButton(i-2, j, buttons[i-2][j].getBackground());
buttons[i-2][j].setBackground(Color.red);
if (buttons[i-2][j].getIcon()==black || buttons[i-2][j].getIcon()==white)
buttons[i-2][j].setIcon(red);
a.add(info);
}
}
}
} // soldier

כמו שאפשר לראות מדובר פה על חייל במשחק, כל שצריך זה להזיז אותו קדימה/אחורה וכו'.
מסיבה לא ברורה בחלק מהמשבצות לאחר שאני מזיז פעם אחת, בפעם השנייה זה לא מזהה את הכלי כלל ככלי, אלא חושב שהוא מקום ריק.


public void Switch(ActionEvent e)
{
char c_x=e.getActionCommand().charAt(2);
char c_y=e.getActionCommand().charAt(6);

int i_x=Character.getNumericValue(c_x);
int i_y=Character.getNumericValue(c_y);

System.out.println(i_x);
System.out.println(i_y);

// if (x!=i_x && y!=i_y)
{
for (int i=0; i < a.size(); i++)
{
InfoButton info= (InfoButton) a.get(i);
if (i_x==info.getX() && i_y==info.getY())
{
B.Switch(info.getX(), info.getY(), x, y);
buttons[i_x][i_y].setIcon(buttons[x][y].getIcon());
buttons[i_x][i_y].setBackground(info.getColor());
}
else
{
if (buttons[info.getX()][info.getY()].getIcon()==red)
if (info.getColor()==Color.white)
buttons[info.getX()][info.getY()].setIcon(white);
else buttons[info.getX()][info.getY()].setIcon(black);
buttons[info.getX()][info.getY()].setBackground(info.getColor());
}
}
for (int i=0; i < a.size(); i++)
{
a.remove(i);
}

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


מצורף תמונה להמחשה. (2 החיילים ש"תקועים" מסומנים בעיגול).

תודה רבה על העזרה.
 

BravoMan

Active member
צר לי, אבל אי אפשר להבין כלום ממה שצרפת.

מהם i ו-j עליהם עושים את הבדיקות?
מה עושה פונקציה startWith?
מה זה המשתנה s?

האם אתה בטוח שהכלי לא מזוהה, כלומר התוכנה לא נכנסת ל-if העליון, או שאולי פשוט אף אחד מהתנאים בפנים לא מתקיים?

אני ממליץ שתתחיל להכניס הדפסות בתוך כל תנאי כדי לראות איזה נתיב התוכנה לוקחת, או אפילו תלמד איך משתמשים ב-debugger.

בהצלחה!
 

ehudl1

New member
כמו שאמרתי:

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

אולי זה יותר ברור ככה.


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 void searchTool(String str)
{
int i=x, j=y;

char s=str.charAt(0);

String tool=str.substring(6);

if (tool.equals("soldier"))
soldier (i,j,s);

if (tool.equals("horse"))
horse (i,j,s);

if (tool.equals("rook"))
rook (i,j);

if (tool.equals("run"))
run (i,j,s);

} // searchTool



public void soldier (int i, int j, char s)
{
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 (B.startWith(i, j+1)!=s)
{
InfoButton info= new InfoButton(i, j+1, buttons[j+1].getBackground());
buttons[j+1].setBackground(Color.red);
if (buttons[j+1].getIcon()==black || buttons[j+1].getIcon()==white)
buttons[j+1].setIcon(red);
a.add(info);
}
}
if (j+2<=7)
{
right2=B.startWith(i, j+2);
if (B.startWith(i, j+2)!=s)
{
if (right!=s && (right!=right2 || (right=='n' && right2=='n')))
{
InfoButton info= new InfoButton(i, j+2, buttons[j+2].getBackground());
buttons[j+2].setBackground(Color.red);
if (buttons[j+2].getIcon()==black || buttons[j+2].getIcon()==white)
buttons[j+2].setIcon(red);
a.add(info);
}
}
}
if (j-1>=0)
{
left=B.startWith(i, j-1);
if (B.startWith(i, j-1)!=s)
{
InfoButton info= new InfoButton(i, j-1, buttons[j-1].getBackground());
buttons[j-1].setBackground(Color.red);
if (buttons[j-1].getIcon()==black || buttons[j-1].getIcon()==white)
buttons[j-1].setIcon(red);
a.add(info);
}
}
if (j-2>=0)
{
left2=B.startWith(i, j-2);
if (B.startWith(i, j-2)!=s)
{
if (left!=s && (left!=left2 || (left=='n' && left2=='n')))
{
InfoButton info= new InfoButton(i, j-2, buttons[j-2].getBackground());
buttons[j-2].setBackground(Color.red);
if (buttons[j-2].getIcon()==black || buttons[j-2].getIcon()==white)
buttons[j-2].setIcon(red);
a.add(info);
}
}
}
if (i+1<=7)
{
down=B.startWith(i+1, j);
if (B.startWith(i+1, j)!=s)
{
InfoButton info= new InfoButton(i+1, j, buttons[i+1][j].getBackground());
buttons[i+1][j].setBackground(Color.red);
if (buttons[i+1][j].getIcon()==black || buttons[i+1][j].getIcon()==white)
buttons[i+1][j].setIcon(red);
a.add(info);
}
}
if (i+2<=7)
{
down2=B.startWith(i+2, j);
if (B.startWith(i+2, j)!=s)
{
if (B.startWith(i+1, j)!=s)
{
InfoButton info= new InfoButton(i+2, j, buttons[i+2][j].getBackground());
buttons[i+2][j].setBackground(Color.red);
if (buttons[i+2][j].getIcon()==black || buttons[i+2][j].getIcon()==white)
buttons[i+2][j].setIcon(red);
a.add(info);
}
}
}
if (i-1>=0)
{
up=B.startWith(i-1, j);
if (B.startWith(i-1, j)!=s)
{
InfoButton info= new InfoButton(i-1, j, buttons[i-1][j].getBackground());
buttons[i-1][j].setBackground(Color.red);
if (buttons[i-1][j].getIcon()==black || buttons[i-1][j].getIcon()==white)
buttons[i-1][j].setIcon(red);
a.add(info);
}
}
if (i-2>=0)
{
up2=B.startWith(i-2, j);
if (B.startWith(i-2, j)!=s)
{
if (B.startWith(i-1, j)=='n')
{
InfoButton info= new InfoButton(i-2, j, buttons[i-2][j].getBackground());
buttons[i-2][j].setBackground(Color.red);
if (buttons[i-2][j].getIcon()==black || buttons[i-2][j].getIcon()==white)
buttons[i-2][j].setIcon(red);
a.add(info);
}
}
}
} // soldier



public void Switch(ActionEvent e)
{
char c_x=e.getActionCommand().charAt(2);
char c_y=e.getActionCommand().charAt(6);

int i_x=Character.getNumericValue(c_x);
int i_y=Character.getNumericValue(c_y);

System.out.println(i_x);
System.out.println(i_y);

for (int i=0; i < a.size(); i++)
{
InfoButton info= (InfoButton) a.get(i);
if (i_x==info.getX() && i_y==info.getY())
{
B.Switch(info.getX(), info.getY(), x, y);
// System.out.println("whois: "+B.whoIs(x, y));
buttons[i_x][i_y].setIcon(buttons[x][y].getIcon());
buttons[i_x][i_y].setBackground(info.getColor());
}
else
{
if (buttons[info.getX()][info.getY()].getIcon()==red)
if (info.getColor()==Color.white)
buttons[info.getX()][info.getY()].setIcon(white);
else buttons[info.getX()][info.getY()].setIcon(black);
buttons[info.getX()][info.getY()].setBackground(info.getColor());
}
}
for (int i=0; i < a.size(); i++)
{
a.remove(i);
}

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

mazory

New member
אז זהו, שזה לא נוח

התוכנית שלך נעשית מורכבת יותר ויותר, ורוב האנשים כאן לא יעברו על מאות שורות קוד על מנת לדבג לך את התוכנית.

זו מיומנות חשובה שאתה חייב לרכוש ולהשתלט עליה.
האם אתה יודע לעבוד עם דיבאגר?
 

ehudl1

New member
פישטתי את זה

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

mazory

New member
לא ענית על השאלה שלי..

אתה כתבת את הקוד הזה, אתה צריך להיות מסוגל לדבג אותו.
יש לך תיאור מדויק של *מה* קורה.
עם דיבאגר, תוכל לשחזר את התופעה ולהבין *למה* קורה.
 

ehudl1

New member
הבנתי בגדול את הבעיה שם לאחר בדיקה

ולכן, החלטתי לחלק את זה ולעשות הפרדה מסודרת.
 
למעלה