הזזה על הלוח
שלום,
ע"מ שיהיה נוח מצורף קטע קוד יחסית גדול:
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 החיילים ש"תקועים" מסומנים בעיגול).
תודה רבה על העזרה.
שלום,
ע"מ שיהיה נוח מצורף קטע קוד יחסית גדול:
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 החיילים ש"תקועים" מסומנים בעיגול).
תודה רבה על העזרה.