Skip to content

Instantly share code, notes, and snippets.

@mhd-zulqarnain
Created May 10, 2017 05:53
Show Gist options
  • Save mhd-zulqarnain/c216773d0c685b7655de1dda59e4c814 to your computer and use it in GitHub Desktop.
Save mhd-zulqarnain/c216773d0c685b7655de1dda59e4c814 to your computer and use it in GitHub Desktop.
Puzzel8
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>1.8</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/puzz.iml" filepath="$PROJECT_DIR$/puzz.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
import java.util.ArrayList;
/**
* Created by zulup on 5/8/2017.
*/
public class main {
public static void main(String[] args) throws InterruptedException {
int A[][] = new int[][]{{1,6,4}, {2,8,3}, {7,0,5}}; // the initial array
int B[][] = new int[][]{{1,2,3}, {7,6,5}, {8,0,4}};
int i, j;
int d; // difference between the desired array anfd the test array
ArrayList<Node> arr=new ArrayList<Node>();
arr.add(new Node(B));
System.out.println(isRepeat(A,arr));
Thread.sleep(12000);
Solver s1=new Solver(A,B);
while(true)
{
d = s1.difference(A, B);
if(d==0)
{
System.out.println("files matched in steps:"+s1.steps);
break;
}
s1.steps++;
System.out.println("Step:"+s1.steps);
System.out.println();
if(s1.steps>150){
break;
}
s1.alter(A, B);
arr.add(new Node(A));
s1.display(A);
}
}
static boolean isRepeat(int[][] A,ArrayList<Node> list){
boolean status=false;
int[][] temp=new int[3][3];
int j;
for(Node s1:list) {
int[][] len = s1.getData();
for (int i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
temp[i][j] = len[i][j];
if(isEqual(A,temp))return true;
}
}
return status;
}
public static boolean isEqual(final int[][] A, int[][] temp) {
boolean state=true;
int counter =0 ,i,j;
for(i=0;i<3; i++)
for( j=0;j<3;j++)
if(A[i][j] != temp[i][j]) return false;
return state;
}
public static void showList(ArrayList<Node> list){
for(Node s1:list){
int[][] len=s1.getData();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
System.out.print(" " + len[i][j]);
System.out.println(" ");
}
}
}
}
/**
* Created by zulup on 5/8/2017.
*/
public class Node {
int[][] data;
public Node(int[][] data){
this.data=data;
}
public int[][] getData() {
return data;
}
public void setData(int[][] data) {
this.data = data;
}
void add(int a){
}
}
import java.util.ArrayList;
import java.util.Random;
/**
* Created by zulup on 5/8/2017.
*/
public class Solver {
int[][] A=new int[3][3];
int[][] B=new int[3][3];
char[] arr=new char[2];
Node node;
int steps;
ArrayList<Node> list;
public Solver(int A[][],int B[][]){
this.A=A;
this.B=B;
list=new ArrayList<Node>();
}
void display(int A[][])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
System.out.print(" "+A[i][j]);
System.out.println();
}
}
int difference(int A[][], int B[][])
{
int counter =0 ,i,j;
for(i=0;i<3; i++)
for( j=0;j<3;j++)
if(A[i][j] != B[i][j]) counter++;
return counter;
}
int alter(int A[][], int B[][])
{
int dup, ddown, dleft, dright;
int temp, i , j, flag=0, serial=0;
char[] ran=new char[6];
char change ;
dup = diffup(A, B); //3
ddown = diffdown(A, B); //4
dleft = diffleft(A, B); //4
dright = diffright(A, B);//2
System.out.println("r l u d ");
System.out.println(dright+" "+dleft+" "+dup+" "+ ddown);
int min = minimum(dup, ddown, dleft, dright);
if (min == dright)
ran[serial++] = 'r';
if (min == dleft)
ran[serial++] = 'l';
if (min == dup)
ran[serial++] = 'u';
if (min == ddown)
ran[serial++] = 'd';
Random rand = new Random();
int sel = rand.nextInt(serial);
change = ran[sel];
System.out.println(change);
/* for( i=0;i<ran.length;i++){
System.out.println(ran[i]);
}
if(arr[0]==ran[--serial]){
change = ran[++serial];
}else
change = ran[--serial];
change=ran[serial--]==
]?ran[serial]:ran[serial++];*/
if(change == 'r')
{
arr[0]='r';
for(i=0;i<3;i++)
for (j=0;j<2;j++)
if(A[i][j]==0){
A[i][j] = A[i][j+1];
A[i][j+1] = 0;
System.out.println("right");
return 0; }
}
else if(change == 'l')
{
arr[0]='l';
for(i=0;i<3;i++)
for (j=1;j<3;j++)
if(A[i][j]==0)
{
A[i][j] = A[i][j-1];
A[i][j-1] = 0;
System.out.println("left");
return 0;
}
}
else if(change == 'u')
{
arr[0]='u';
for(i=1;i<3;i++)
for (j=0;j<3;j++)
if(A[i][j]==0) {
A[i][j] = A[i-1][j];
A[i-1][j] = 0;
System.out.println("up");
return 0;}
}
else if(change == 'd')
{
System.out.println("ddd");
arr[0]='d';
for(i=0;i<2;i++)
for (j=0;j<3;j++)
if(A[i][j]==0)
{
A[i][j] = A[i+1][j];
A[i+1][j] = 0;
System.out.println("down");
return 0;
}
}
return 0;
}
int diffup(int A[][], int B[][])
{
int[][] temp=new int[3][3];
int i, j;
for(i=0;i<3;i++) //copying the array A in array temp for testing
for (j=0;j<3;j++)
temp[i][j] = A[i][j];
for(i=1;i<3;i++) // swapping the space in upward direction
for (j=0;j<3;j++) // starting the array from i=1 insures that the condition i=0 avoided
if(A[i][j]==0)
{
temp[i-1][j] = 0;
temp[i][j] = A[i-1][j];
}
return difference(temp, B);
}
int diffdown(int A[][], int B[][])
{
int[][] temp=new int[3][3];
int i, j;
for(i=0;i<3;i++) //copying the array A in array temp for testing
for (j=0;j<3;j++)
temp[i][j] = A[i][j];
for(i=0;i<2;i++) // swapping the space in downward direction
for (j=0;j<3;j++) // ending at i=1 insures that the condition i=0 avoided
if(A[i][j]==0)
{
temp[i+1][j] = 0;
temp[i][j] = A[i+1][j];
}
return difference(temp, B);
}
int diffleft(int A[][], int B[][])
{
int[][] temp=new int[3][3];
int i, j;
for(i=0;i<3;i++) //copying the array A in array temp for testing
for (j=0;j<3;j++)
temp[i][j] = A[i][j];
for(i=0;i<3;i++) // swapping the space in the left direction
for (j=1;j<3;j++)
if(A[i][j]==0)
{
temp[i][j-1] = 0;
temp[i][j] = A[i][j-1];
}
return difference(temp, B);
}
int diffright(int A[][], int B[][])
{
int[][] temp=new int[3][3];
int i, j;
for(i=0;i<3;i++) //copying the array A in array temp for testing
for (j=0;j<3;j++)
temp[i][j] = A[i][j];
for(i=0;i<3;i++) // swapping the space in the right direction
for (j=0;j<2;j++)
if(A[i][j]==0)
{
temp[i][j+1] = 0;
temp[i][j] = A[i][j+1];
}
return difference(temp, B);
}
int minimum (int a, int b, int c , int d)
{
int min = a;
if(b<min)
min= b;
if(c<min)
min = c;
if(d<min)
min = d;
return min;
}
}
/**
* Created by zulup on 5/9/2017.
*/
public class Tesgt {
public static void main(String[] args){
int A[][] = new int[][]{{2,8,3}, {1,6,4}, {7,0,5}}; // the initial array
int B[][] = new int[][]{{1,2,3}, {8,0,4}, {7,6,5}};
System.out.println(isEqual(A,A));
}
public static boolean isEqual( int[][] A, int[][] temp) {
boolean state=false;
int counter =0 ,i,j;
for(i=0;i<3; i++)
for( j=0;j<3;j++)
if(A[i][j] != temp[i][j]) return false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment