Saturday, 2 October 2021

Fifteen (Part 1)

As a child I had a puzzle made of sliding plastic tiles in a 4x4 grid. I can no longer find it so I have to assume it has been lost, given away or broken and thrown away (I rarely throw things away if they aren't broken). Here is a picture of a similar puzzle, which I found elsewhere on the Internet:

This one retails at £270.92, which seems excessive but when I looked more closely I saw that it measures 50cm x 50cm x 5cm, which is much larger than normal and probably explains why it costs so much.

The objective of the puzzle is to slide the numbers around until they are in numerical order as shown below:

There are also several versions of this puzzle to play online. I have been teaching myself Java from a book for a while so I decided to create one of my own. I found the source code for a 3x3 version and tried it out. I found that I could not click on the X in the top right hand corner of the screen to close the application so I added code to do this. Then I changed it to a 4x4 version and mixed up the starting sequence of the numbers so that it looked like this:


If you would like to run it yourself, you will need a PC with Java installed. Copy the source code, which is at the end of this post, into a text file called Fifteen.java and run it like this:

andrew@ASUS-Laptop:~/Java$ javac Fifteen.java
andrew@ASUS-Laptop:~/Java$ java Fifteen

import java.awt.*;  
import java.awt.event.*;  
import javax.swing.JOptionPane;  
public class Fifteen extends Frame implements WindowListener, ActionListener
{  
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;  
Fifteen()
    {  
    super("Fifteen");  
    b1=new Button();  
    b1.setBounds(50,100,40,40);  
    b2=new Button();  
    b2.setBounds(100,100,40,40);  
    b3=new Button();  
    b3.setBounds(150,100,40,40);  
    b4=new Button();  
    b4.setBounds(200,100,40,40);  
    b5=new Button();  
    b5.setBounds(50,150,40,40);  
    b6=new Button();  
    b6.setBounds(100,150,40,40);  
    b7=new Button();  
    b7.setBounds(150,150,40,40);  
    b8=new Button();  
    b8.setBounds(200,150,40,40);  
    b9=new Button();  
    b9.setBounds(50,200,40,40);  
    b10=new Button();  
    b10.setBounds(100,200,40,40);  
    b11=new Button();  
    b11.setBounds(150,200,40,40);  
    b12=new Button();  
    b12.setBounds(200,200,40,40);  
    b13=new Button();  
    b13.setBounds(50,250,40,40);  
    b14=new Button();  
    b14.setBounds(100,250,40,40);  
    b15=new Button();  
    b15.setBounds(150,250,40,40);  
    b16=new Button();  
    b16.setBounds(200,250,40,40);  

    b1.setLabel("6");
    b2.setLabel("7");
    b3.setLabel("4");
    b4.setLabel("11");
    b5.setLabel("2");
    b6.setLabel("1");
    b7.setLabel("15");
    b8.setLabel("10");
    b9.setLabel("13");
    b10.setLabel("5");
    b11.setLabel("");
    b12.setLabel("3");
    b13.setLabel("9");
    b14.setLabel("14");
    b15.setLabel("12");
    b16.setLabel("8");
      
    b1.addActionListener(this);  
    b2.addActionListener(this);  
    b3.addActionListener(this);  
    b4.addActionListener(this);  
    b5.addActionListener(this);  
    b6.addActionListener(this);  
    b7.addActionListener(this);  
    b8.addActionListener(this);  
    b9.addActionListener(this);  
    b10.addActionListener(this);  
    b11.addActionListener(this);  
    b12.addActionListener(this);  
    b13.addActionListener(this);  
    b14.addActionListener(this);  
    b15.addActionListener(this);  
    b16.addActionListener(this);  
      
    add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);  
    add(b9);add(b10);add(b11);add(b12);add(b13);add(b14);add(b15);add(b16);  
    setSize(400,400);  
    setLayout(null);  
    setVisible(true);  
    addWindowListener(this);
    }  
public void windowClosing(WindowEvent x)
    {
    dispose();
    System.exit(0);
    }
public void windowActivated(WindowEvent x) { }
public void windowDeactivated(WindowEvent x) { }
public void windowOpened(WindowEvent x) { }
public void windowClosed(WindowEvent x) { }
public void windowIconified(WindowEvent x) { }
public void windowDeiconified(WindowEvent x) { }
public void actionPerformed(ActionEvent e)
    {  
    if(e.getSource()==b1)
        {  
        String label=b1.getLabel();  
        if(b2.getLabel().equals(""))
            {  
            b2.setLabel(label);  
            b1.setLabel("");  
            }  
        if(b5.getLabel().equals(""))
            {  
            b5.setLabel(label);  
            b1.setLabel("");  
            }  
        }  
    if(e.getSource()==b2)
        {  
        String label=b2.getLabel();  
        if(b1.getLabel().equals(""))
            {  
            b1.setLabel(label);  
            b2.setLabel("");  
            }  
        if(b3.getLabel().equals(""))
            {  
            b3.setLabel(label);  
            b2.setLabel("");  
            }  
        if(b6.getLabel().equals(""))
            {  
            b6.setLabel(label);  
            b2.setLabel("");  
            }  
        }  
    if(e.getSource()==b3)
        {  
        String label=b3.getLabel();  
        if(b2.getLabel().equals(""))
            {  
            b2.setLabel(label);  
            b3.setLabel("");  
            }  
        if(b4.getLabel().equals(""))
            {  
            b4.setLabel(label);  
            b3.setLabel("");  
            }  
        if(b7.getLabel().equals(""))
            {  
            b7.setLabel(label);  
            b3.setLabel("");  
            }  
        }  
    if(e.getSource()==b4)
        {  
        String label=b4.getLabel();  
        if(b3.getLabel().equals(""))
            {  
            b3.setLabel(label);  
            b4.setLabel("");  
            }  
        if(b8.getLabel().equals(""))
            {  
            b8.setLabel(label);  
            b4.setLabel("");  
            }  
        }  
    if(e.getSource()==b5)
        {  
        String label=b5.getLabel();  
        if(b1.getLabel().equals(""))
            {  
            b1.setLabel(label);  
            b5.setLabel("");  
            }  
        if(b6.getLabel().equals(""))
            {  
            b6.setLabel(label);  
            b5.setLabel("");  
            }  
        if(b9.getLabel().equals(""))
            {  
            b9.setLabel(label);  
            b5.setLabel("");  
            }  
        }  
    if(e.getSource()==b6)
        {  
        String label=b6.getLabel();  
        if(b2.getLabel().equals(""))
            {  
            b2.setLabel(label);  
            b6.setLabel("");  
            }  
        if(b5.getLabel().equals(""))
            {  
            b5.setLabel(label);  
            b6.setLabel("");  
            }  
        if(b7.getLabel().equals(""))
            {  
            b7.setLabel(label);  
            b6.setLabel("");  
            }  
        if(b10.getLabel().equals(""))
            {  
            b10.setLabel(label);  
            b6.setLabel("");  
            }  
        }  
    if(e.getSource()==b7)
        {  
        String label=b7.getLabel();  
        if(b3.getLabel().equals(""))
            {  
            b3.setLabel(label);  
            b7.setLabel("");  
            }  
        if(b6.getLabel().equals(""))
            {  
            b6.setLabel(label);  
            b7.setLabel("");  
            }  
        if(b8.getLabel().equals(""))
            {  
            b8.setLabel(label);  
            b7.setLabel("");  
            }  
        if(b11.getLabel().equals(""))
            {  
            b11.setLabel(label);  
            b7.setLabel("");  
            }  
        }  
    if(e.getSource()==b8)
        {  
        String label=b8.getLabel();  
        if(b4.getLabel().equals(""))
            {  
            b4.setLabel(label);  
            b8.setLabel("");  
            }  
        if(b7.getLabel().equals(""))
            {  
            b7.setLabel(label);  
            b8.setLabel("");  
            }  
        if(b12.getLabel().equals(""))
            {  
            b12.setLabel(label);  
            b8.setLabel("");  
            }  
        }  
    if(e.getSource()==b9)
        {  
        String label=b9.getLabel();  
        if(b5.getLabel().equals(""))
            {  
            b5.setLabel(label);  
            b9.setLabel("");  
            }  
        if(b10.getLabel().equals(""))
            {  
            b10.setLabel(label);  
            b9.setLabel("");  
            }  
        if(b13.getLabel().equals(""))
            {  
            b13.setLabel(label);  
            b9.setLabel("");  
            }  
        }  
    if(e.getSource()==b10)
        {  
        String label=b10.getLabel();  
        if(b6.getLabel().equals(""))
            {  
            b6.setLabel(label);  
            b10.setLabel("");  
            }  
        if(b9.getLabel().equals(""))
            {  
            b9.setLabel(label);  
            b10.setLabel("");  
            }  
        if(b11.getLabel().equals(""))
            {  
            b11.setLabel(label);  
            b10.setLabel("");  
            }  
        if(b14.getLabel().equals(""))
            {  
            b14.setLabel(label);  
            b10.setLabel("");  
            }  
        }  
    if(e.getSource()==b11)
        {  
        String label=b11.getLabel();  
        if(b7.getLabel().equals(""))
            {  
            b7.setLabel(label);  
            b11.setLabel("");  
            }  
        if(b10.getLabel().equals(""))
            {  
            b10.setLabel(label);  
            b11.setLabel("");  
            }  
        if(b12.getLabel().equals(""))
            {  
            b12.setLabel(label);  
            b11.setLabel("");  
            }  
        if(b15.getLabel().equals(""))
            {  
            b15.setLabel(label);  
            b11.setLabel("");  
            }  
        }  
    if(e.getSource()==b12)
        {  
        String label=b12.getLabel();  
        if(b8.getLabel().equals(""))
            {  
            b8.setLabel(label);  
            b12.setLabel("");  
            }  
        if(b11.getLabel().equals(""))
            {  
            b11.setLabel(label);  
            b12.setLabel("");  
            }  
        if(b16.getLabel().equals(""))
            {  
            b16.setLabel(label);  
            b12.setLabel("");  
            }  
        }  
    if(e.getSource()==b13)
        {  
        String label=b13.getLabel();  
        if(b9.getLabel().equals(""))
            {  
            b9.setLabel(label);  
            b13.setLabel("");  
            }  
        if(b14.getLabel().equals(""))
            {  
            b14.setLabel(label);  
            b13.setLabel("");  
            }  
        }  
    if(e.getSource()==b14)
        {  
        String label=b14.getLabel();  
        if(b10.getLabel().equals(""))
            {  
            b10.setLabel(label);  
            b14.setLabel("");  
            }  
        if(b13.getLabel().equals(""))
            {  
            b13.setLabel(label);  
            b14.setLabel("");  
            }  
        if(b15.getLabel().equals(""))
            {  
            b15.setLabel(label);  
            b14.setLabel("");  
            }  
        }  
    if(e.getSource()==b15)
        {  
        String label=b15.getLabel();  
        if(b11.getLabel().equals(""))
            {  
            b11.setLabel(label);  
            b15.setLabel("");  
            }  
        if(b14.getLabel().equals(""))
            {  
            b14.setLabel(label);  
            b15.setLabel("");  
            }  
        if(b16.getLabel().equals(""))
            {  
            b16.setLabel(label);  
            b15.setLabel("");  
            }  
        }  
    if(e.getSource()==b16)
        {  
        String label=b16.getLabel();  
        if(b12.getLabel().equals(""))
            {  
            b12.setLabel(label);  
            b16.setLabel("");  
            }  
        if(b15.getLabel().equals(""))
            {  
            b15.setLabel(label);  
            b16.setLabel("");  
            }  
        }  
    // Check if game is complete:
    if(b1.getLabel().equals("1")&&b2.getLabel().equals("2")
        &&b3.getLabel().equals("3")&&b4.getLabel().equals("4")   
        &&b5.getLabel().equals("5")&&b6.getLabel().equals("6")
        &&b7.getLabel().equals("7")&&b8.getLabel().equals("8")    
        &&b9.getLabel().equals("9")&&b10.getLabel().equals("10")
        &&b11.getLabel().equals("11")&&b12.getLabel().equals("12")    
        &&b13.getLabel().equals("13")&&b14.getLabel().equals("14")
        &&b15.getLabel().equals("15"))  
        {
        JOptionPane.showMessageDialog(this,"Well done!");    
        }    
}  
public static void main(String[] args)
    {  
    new Fifteen();  
    }  
}