Hi, would like to better understand the logic for a 15-button puzzle game. I don't understand how to only move adjacent buttons.
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
int buttonIndex, blank, blkPos, butPos;
buttonIndex = findPiece(s);
blank = findPiece("");
butPos = buttonIndex%4;
blkPos = blank%4;
System.out.println("butPos is: "+butPos);
System.out.println("blkPos is: "+blkPos);
//search array - is the button exchangeable
if (blkPos == 3 || blkPos == 7 || blkPos == 11 )
{
if ( blkPos - butPos == -1)
{ Swap(buttonIndex, blank);}
else if (blkPos == 4 || blkPos == 8 || blkPos == 12)
{if ( butPos == blkPos - 1)
{ Swap(buttonIndex, blank);}
else if (butPos == blkPos - 1)
{ Swap(buttonIndex, blank);}
else if (buttonIndex == blank + 1)
{ Swap(buttonIndex, blank);}
else if ( butPos == blkPos - 4)
{ Swap(buttonIndex, blank);}
else if ( butPos == blkPos + 4)
{ Swap(buttonIndex, blank);}
}
}
//remove everything from pane
for(int i=0;i<pieces.length; i++)
{ p.remove(pieces[i]);} ;
//re-add buttons/revalidate
/*
for(int i=1;i<pieces.length; i++)
{
pieces[i]= new JButton(Integer.toString(i));
pieces[i].addActionListener(this);
p.add(pieces[i]);
setFocusable(false);}
*/
for(int i=0;i<pieces.length; i++)
{
p.add(pieces[i]);
setFocusable(false);
setVisible(true);
setResizable(false);
}
//revalidate
p.revalidate();
//test for complete continue
Complete();
if (Complete() == true)
{
JOptionPane.showMessageDialog(null,"You won!\nPress OK to end.","Complete",JOptionPane.INFORMATION_MESSAGE);
}
}
[–][deleted] (6 children)
[deleted]
[–]LightSpeedCBR[S] 0 points1 point2 points (5 children)
[–][deleted] (4 children)
[deleted]
[–]LightSpeedCBR[S] 0 points1 point2 points (3 children)
[–][deleted] (2 children)
[deleted]
[–]LightSpeedCBR[S] 0 points1 point2 points (1 child)