/*
 * @(#)Tilegen.java 1.0 08/08/18
 *
 * 
 * 
 *
 * 
 * 
 * 
 *
 */

import java.awt.*;
import java.applet.*;
import java.net.*;
import java.awt.event.*;
import java.io.FilePermission;


import java.util.Calendar;
import java.text.SimpleDateFormat;


import java.security.AccessController;
import java.lang.*;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.lang.String;

public class Tilegen extends Applet implements ActionListener {
	
	 Image tile_gif,qug_gif;
	 URL base;
	 MediaTracker mt;
	 Button okButton; 
	 Button confirmButton; 
	 Button frameButton; 
	 TextField north_inputs,east_inputs,south_inputs,west_inputs,frame_inputs;
	 TextArea gls,tls; 
	 int cflag,frflag;
	 String newline;
	 public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	 
	 static Formatter output1;
	 static Formatter output2;
	 static Formatter output3;
	 
	
	public void init() {
		
		setLayout(null); 
		okButton = new Button("Submit Inputs"); 
		frameButton = new Button("Submit frame glues");
		confirmButton = new Button("Click to confirm"); 
		north_inputs = new TextField("North outputs appear here",40); 
		east_inputs = new TextField("East inputs appear here",40); 
		south_inputs = new TextField("South inputs appear here",40); 
		west_inputs = new TextField("West outputs appear here",40); 
		gls = new TextArea(10,40);
		gls.setEditable(false);
		
		 
		tls = new TextArea(10,40);
		tls.setEditable(false);
		
		frame_inputs = new TextField("Type frame inputs here",80);
		frame_inputs.setBounds(500,340,400,40); 
		add(frame_inputs);
		
		cflag=0;
		frflag=0;
		north_inputs.setBounds(130,40,200,30); 
		add(north_inputs); 
		
		east_inputs.setBounds(340,150,130,30); 
		add(east_inputs); 
		
		south_inputs.setBounds(130,250,200,30); 
		add(south_inputs); 
		
		west_inputs.setBounds(10,150,130,30); 
		add(west_inputs); 
		
		gls.setBounds(500,10,400,200);
		add(gls);
		
		tls.setBounds(500,440,400,260);
		add(tls);
		
		okButton.setBounds(130,280,100,30);
		add(okButton); 
		okButton.addActionListener(this); 
		
		confirmButton.setBounds(10,430,120,30);
		add(confirmButton); 
		confirmButton.addActionListener(this); 
		
		frameButton.setBounds(500,390,200,30);
		add(frameButton); 
		frameButton.addActionListener(this); 
		
		newline = System.getProperty("line.separator");
		
		mt = new MediaTracker(this);
		
		try {
   
               base = getDocumentBase();
         }
         catch (Exception e) {}
         tile_gif = getImage(base,"tile_sample.gif");
  		 qug_gif = getImage(base, "quick_user_guide.gif");
         try {
               mt.waitForAll();
          }
          catch (InterruptedException  e) {} 
	}

	public void paint(Graphics g) {
	
		g.drawString("Welcome to XTile ver 1.0 for basic .tiles  generation", 10, 20 );
		g.drawString("\n Please enter inputs for the four sides of the computational tile design.", 10, 35 );
		
		//g.drawImage(tile_gif,150,80,this);
		//g.drawImage(qug_gif,10,540,this); 
		 
		 
		g.drawString("North inputs :"+north_inputs.getText(),10,360); 
		g.drawString("East inputs :"+east_inputs.getText(),10,380); 
		g.drawString("South inputs :"+south_inputs.getText(),10,400); 
		g.drawString("West inputs :"+west_inputs.getText(),10,420); 
		g.drawString("Below you enter glues from either the table given",500,240);
		g.drawString(",by entering the glue no. as per your need.",500,260);
		g.drawString("If you enter a new glue type not present in the table, that would be added",500,280);
		g.drawString("following this format a,b,c,d;e,f,g,h;....;w,x,y,z",500,300);
		
		g.drawString("Quick User Guide:\n \n  ",10,480);
		g.drawString("1. Enter inputs in this form : a+b,(a.b)^c,a,a.(~b)\n",10,510);
		g.drawString("2. only four gates : AND(.), OR(+), NOT(~), XOR(^) are allowed\n",10,535);
		g.drawString("3. If more than one gates are used, two operands and an \n",10,560);
		g.drawString("   operator must be bracketed by ( ).\n",10,580);
		g.drawString("4. Do take care in entering frame tile glues. commas ',' seperate \n",10,605);
		g.drawString("glues and semicolon ';' seperate tiles, with NO semicolon in the ending\n",10,625);
		g.drawString("5.Error handling has not been implemented till now. So if you dont \n ",10,650);
		g.drawString("follow the directions, incorrect results are surely expected",10,670);

				
		
		int[] xcd = new int[3];
		int[] ycd = new int[3];
		xcd[0]=150;xcd[1]=235;xcd[2]=150;
		ycd[0]=80;ycd[1]=160;ycd[2]=240;
		g.setColor( Color.red );
		g.drawPolygon(xcd, ycd, 3);
		g.fillPolygon(xcd, ycd, 3);
		
		xcd[0]=150;xcd[1]=235;xcd[2]=320;
		ycd[0]=80;ycd[1]=160;ycd[2]=80;
		g.setColor( Color.blue );
		g.drawPolygon(xcd, ycd, 3);
		g.fillPolygon(xcd, ycd, 3);
		
		xcd[0]=320;xcd[1]=235;xcd[2]=320;
		ycd[0]=80;ycd[1]=160;ycd[2]=240;
		g.setColor( Color.yellow );
		g.drawPolygon(xcd, ycd, 3);
		g.fillPolygon(xcd, ycd, 3);
		
		xcd[0]=150;xcd[1]=235;xcd[2]=320;
		ycd[0]=240;ycd[1]=160;ycd[2]=240;
		g.setColor( Color.green );
		g.drawPolygon(xcd, ycd, 3);
		g.fillPolygon(xcd, ycd, 3);
		
		g.setColor( Color.black );
		g.drawString("\n W", 192, 160 );
		g.drawString("\n E", 276, 160 );
		g.drawString("\n N", 235, 120 );
		g.drawString("\n S", 234, 200 );
	}
	
	     public void actionPerformed(ActionEvent evt) 
         {
         	frflag=0;
         	if(evt.getSource() == frameButton)
              {
              	frflag=1;
              }
         
              if (evt.getSource() == okButton){ repaint();okButton.setLabel("Submitted");cflag=0;frflag=1;}
              else if ((evt.getSource() == confirmButton && cflag==0) || frflag==1) 
          	{
		
		
			String[] in = new String[100];
			
            int i=0;
			for(i=0;i<100;i++) {in[i]="";}
            int xi=0;
            int l1,l2,l3,l4;
            l1=l2=l3=l4=0;
            
            char[] ni = north_inputs.getText().toCharArray();
            int nil = north_inputs.getText().length();
            if(north_inputs.getText()!="\n")
            {
            l1++;
            for(i=0;i<nil;i++)
            {
            	if(ni[i]==',') { xi++;l1++;}
            	else 
            	{
 					String ch1 = null;
 					ch1 = "" + ni[i];           	
            		in[xi]=in[xi].concat(ch1);
            	}	
        	}
        	
        	
        	}
        	xi++;
        	
        	
        	char[] ei = east_inputs.getText().toCharArray();
            int eil = east_inputs.getText().length();
            if(east_inputs.getText()!="\n")
            {
            l2++;
            for(i=0;i<eil;i++)
            {
            	if(ei[i]==',')  {xi++;l2++;}
            	else 
            	{
            		String ch2 = null;	
            		ch2 = "" + ei[i];
            		in[xi]=in[xi].concat(ch2);
            	}	
        	}
        	
        	}
        	xi++;
        	
        	
        	char[] si = south_inputs.getText().toCharArray();
            int sil = south_inputs.getText().length();
            if(south_inputs.getText()!="\n")
            {
            l3++;
            for(i=0;i<sil;i++)
            {
            	if(si[i]==',')  {xi++;l3++;}
            	else
            	{
            		String ch3 = null;
            		ch3 = "" +si[i];            		
            		in[xi]=in[xi].concat(ch3);
            	}
        	}
        	
        	}
        	xi++;
        	
        	
        	
        	char[] wi = west_inputs.getText().toCharArray();
            int wil = west_inputs.getText().length();
            if(west_inputs.getText()!="\n")
            {
            l4++;
            for(i=0;i<wil;i++)
            {
            	if(wi[i]==',')  {xi++;l4++;}
            	else
            	{
            		String ch4;	
            		ch4 = "" +wi[i];
            		in[xi]=in[xi].concat(ch4);
            	}
        	}
        	
        	}
        	xi++;
            
            in[xi]="\n";
		

            //declaring tables to store inputs from user
            String[] iptable = new String[1000];
            String[] uiptable = new String[1000];
            
            String glue_strap = new String();
            int[] lctable = new int [1000];
            int x=0;
  			int nx=0;
            int size_north,size_south,size_east,size_west;
			int n,flag;
			flag=0;n=0;            
 //           System.out.printf("welcome to tilegen !!\n");
            
            //accepting inputs for side North
//            System.out.printf("How many inputs for side NORTH ? please enter them too");
            size_north = l1;
            String[] a1 =new String[size_north];
            for(i=0;i<size_north;i++)
            {
              a1[i] = in[i];
  //          System.out.printf("\n");
            flag=0;
            char[] a11 = a1[i].toCharArray();
            for(int j=0; j<a1[i].length();j++)
            {
            	if(a11[j]=='+' || a11[j]=='.' || a11[j]=='^' || a11[j]=='~') flag=1;
            }
            iptable[x]= a1[i];		//storing input given by user to string array
            x++;
            if(flag==0)   //checking for basic input
            {
            	 lctable[n]= i;
            	 n++;
            }	
            
 			}
 			
 			//accepting inputs for side East
  //          System.out.printf("How many inputs for side EAST ? please enter them too");
            size_east = l2;
            String[] a2 =new String[size_east];
            for(i=0;i<size_east;i++)
            {
              a2[i] = in[size_north+i];
       //     System.out.printf("\n");
            flag=0;
            char[] a12 = a2[i].toCharArray();
            for(int j=0; j<a2[i].length();j++)
            {
            	if(a12[j]=='+' || a12[j]=='.' || a12[j]=='^' || a12[j]=='~') flag=1;
            }
            iptable[x]= a2[i];		//storing input given by user to string array
            x++;
            if(flag==0)		//checking for basic input  
            {
            	 lctable[n]= i;
            	 n++;
            }	
            
 			}
 			
 			//accepting inputs for side  South
    //        System.out.printf("How many inputs for side SOUTH ? please enter them too");
            size_south = l3;
            String[] a3 =new String[size_south];
            for(i=0;i<size_south;i++)
            {
              a3[i] = in[size_north+size_east+i];
   //         System.out.printf("\n");
            flag=0;
            char[] a13 = a3[i].toCharArray();
            for(int j=0; j<a3[i].length();j++)
            {
            	if(a13[j]=='+' || a13[j]=='.' || a13[j]=='^' || a13[j]=='~') flag=1;
            }
            iptable[x]= a3[i];		//storing input given by user to string array
           	x++;
            if(flag==0)		//checking for basic input
            {
            	 lctable[n]= i;
            	 n++;
            }	
            
 			}
 			
 			//accepting inputs for side West
       //     System.out.printf("How many inputs for side WEST ? please enter them too");
            size_west = l4;
            String[] a4 =new String[size_west];
            for(i=0;i<size_west;i++)
            {
              a4[i] = in[size_north+size_east+size_south+i];
      //      System.out.printf("\n");
            flag=0;
            char[] a14 = a4[i].toCharArray();
            for(int j=0; j<a4[i].length();j++)
            {
            	if(a14[j]=='+' || a14[j]=='.' || a14[j]=='^' || a14[j]=='~') flag=1;
            }
            iptable[x]= a4[i];		//storing input given by user to string array
            x++;
            if(flag==0)		//checking for basic input
            {
            	 lctable[n]= i;
            	 n++;
            }	
            
 			}
 			
 			//copying all inputs as user entered in another string array
 			for(int b=0;b<x;b++)
 			{
 				uiptable[b]=iptable[b];
 			}
 			
 			nx=x; //copying no of inputs user entered to another backup integer
 			
 			//nested looping to find all repetitions in user's inputs
 			for(int q=0;q<x;q++)
 			{
 				for(int w=0;w<x;w++)
 				{
 					int newflag=0;
            		char[] newan = iptable[q].toCharArray();
            		for(int j=0; j<iptable[q].length();j++)
            		{
            			if(newan[j]=='+' || newan[j]=='.' || newan[j]=='^' || newan[j]=='~') newflag=1;
            		}
 					if(iptable[q].compareTo(iptable[w]) == 0 && w!=q && newflag==0)
 					{	int e;
 						for(e=w;e<x-1;e++)
 						{
 							iptable[e]=iptable[e+1];
 							
 						}
 						iptable[e]="\0";
 						x--;
 					}
 				}
 			}
 			
 	
 			n=n-(nx-x); //calculating number of simple inputs to the computational tile design
 			
 			
 			//calculating size of the double dimensional array needed to store
 			//the binary permutations for all the inputs user entered (repetitions not removed)
 	
 			int c=nx;
 			int r=2;
 			for(int y=1;y<n;y++)
 			{
 				r=r*2;
 			}
 		
 			
 			int[][] arr = new int[r][c];
 			
 			//filling with dummy zeros the array created
 			for(int a=0;a<r;a++)
 			{	
 				for(int b=0;b<c;b++)
 				{
 					arr[a][b]=0;
 				}
 				
 			}
 			
 			
 			//calculating size of the double dimensional array needed to store
 			//the binary permutations for all the inputs user entered (repetitions removed)
 	
 			
 			int c2=x;
 			int r2=2;
 			for(int y=1;y<n;y++)
 			{
 				r2=r2*2;
 			}
 		
 			int[][] arr2 = new int[r2][c2];
 			
 			//filling with dummy zeros the array created
 			for(int a=0;a<r2;a++)
 			{	
 				for(int b=0;b<c2;b++)
 				{
 					arr2[a][b]=0;
 				}
 				
 			}
 			
 		
 			n++;
 			
 			//nested looping to store binary permutations for basic inputs
 			for(int gg=0;gg<x;gg++)
 			{
 				flag=0;
 				
            	char[] ch = iptable[gg].toCharArray();
            	for(int j=0; j<iptable[gg].length();j++)
            	{
            		if(ch[j]=='+' || ch[j]=='.' || ch[j]=='^' || ch[j]=='~') flag=1;
            	}
            	if(flag==0)		//checking if the input in consideration is a basic input
            	{	
            		n=n-1;
            		
            		int r1=1;
            		
            		//looping to calcualte the number of times a particular bit (0/1) must be repeated
 					for(int y=1;y<n;y++)
 					{
 						r1=r1*2;
 					}
 					int start=0;
 					int end=r1;
 					int val=0;
 					//main looping which stores the binary values 
 					do
 					{	
 						for(int xs=start;xs<end;xs++)
 						{
 							arr2[xs][gg]=val;
 						}
 						val=1-val;
 						start=end;
 						end=end+r1;
 						
 					}while((r+1)>end);
 					
 				}
 			}
 			
 		
    //converting infix expression to postfix expression
    String[] piptable = new String[1000];
    for(int g=0;g<nx;g++)
    {
    	char[] inf = uiptable[g].toCharArray();
    	
    	char[] stk = new char[uiptable[g].length()];
    	char[] pof = new char[uiptable[g].length()];
    	int op=0;
    	int pp=0;
    	for(int g1=0;g1<uiptable[g].length();g1++)
    	{
    		if(inf[g1]=='+' || inf[g1]=='.' || inf[g1]=='^' || inf[g1]=='~')
    		{
    			
    			stk[op] = inf[g1];
    			op++;
    		}	
    		
    		else if(inf[g1]=='(')
    		{
    			
    			stk[op] = inf[g1];
    			op++;
    		}
    		else if(inf[g1]==')')
    		{
    			op--;
    			do
    			{	
    				pof[pp]=stk[op];
    				op--;
    				pp++;
    			}while(stk[op]!='(');
    			
    		}
    		else
    		{
    			pof[pp]=inf[g1];
    			pp++;
    		}
    		
    	}
    	
    	if(pp!=uiptable[g].length())
    		{
    			do{
    				
    				op--;
    				pof[pp]=stk[op];
    				
    				pp++;
    				
    			  }while(op>0);
    		}
    	piptable[g]="";
    	for(int g2=0;g2<pp;g2++)
    	{
    		piptable[g]=String.valueOf(pof[g2]) + piptable[g];
    	}	
    	
    	String reversed = "";
            for (i=0; i<pp; i++) {
                reversed = piptable[g].substring(i, i+1) + reversed;
            }
		piptable[g] = reversed;
    	
    	
    	
    	
    }
  
 
// 	System.out.printf("\n");
 	
 	//evaluating the converted postfix expression
 	
 	
    for(int gx=0;gx<nx;gx++)
    {
    char[] chal = piptable[gx].toCharArray();
    int[] stk = new int[piptable[gx].length()];
    
    for(int gy=0;gy<r;gy++)
    {
    	
    	int gs=0;
    	int go=0; 
    	for(int gt=0;gt<piptable[gx].length();gt++)
    	{	
    		
    		if(chal[gt]=='+' || chal[gt]=='.' || chal[gt]=='^' || chal[gt]=='~')//condition to evaluate for operators
    		{
    			
    			if(chal[gt]=='+')
    			{	
    				gs--;int t2 = stk[gs];
    				gs--;int t1 = stk[gs];
    				if(t1==0 && t2==0)stk[gs]=0;
    				else stk[gs]=1;
    				gs++;
    			}
    			else if(chal[gt]=='.')
    			{	
    				gs--;int t2 = stk[gs];
    				gs--;int t1 = stk[gs];
    				if(t1==1 && t2==1)stk[gs]=1;
    				else stk[gs]=0;
    				gs++;
    			}
    			else if(chal[gt]=='^')
    			{	
    				gs--;int t2 = stk[gs];
    				gs--;int t1 = stk[gs];
    				if(t1==1 && t2==0)stk[gs]=1;
    				else if(t1==0 && t2==1)stk[gs]=1;
    				else stk[gs]=0;
    				gs++;
    			}
    			else if(chal[gt]=='~')
    			{	
    				gs--;
    				int t1=1-stk[gs];
    				
    				stk[gs]=t1;
    				gs++;
    			}
    			
    		}
    		
    		else	//to evaluate for operands
    		{   
    			
    			
    			int mark=0;
    			String ttg = String.valueOf(chal[gt]);
    		
    			for(int s=0;s<x;s++)
    			{
    				
    				if(ttg.compareTo(iptable[s])==0) mark=s;
    			}
    			
    			stk[gs]=arr2[gy][mark]; //storing the binary value in stack
    			gs++;
    		}
    	
    	}
    	
    	if(gs>0)gs--;
    	arr[gy][gx]=stk[gs];  //storing the computed value from stack to array
    	
    	
    	
    	}
    }

    for(int a=0;a<r;a++)	//dummy coding the binary values in the array 
 	{	
 		for(int b=0;b<c;b++)
 		{
 			if(arr[a][b]==0) arr[a][b]=4;
 			else arr[a][b]=5;
 		}
 		
 	}
 
 	//process of conversion of binary digits to glues
 	
    int[] gv = new int[4]; 
    gv[0]=size_north;
    gv[1]=size_east;
    gv[2]=size_south;
    gv[3]=size_west;
    int lgv=4;
 
    for(int w1=0;w1<lgv;w1++)	//looping to find the no. of bits in input on each face and removing similiar ones
    {
    	for(int w2=0;w2<lgv;w2++)
    	if(gv[w1]==gv[w2] && w1!=w2)
    	{
    		int w3;
    		
    	   	for(w3=w2;w3<lgv-1;w3++)
    		{
    			gv[w3]=gv[w3+1];
    		}
    		gv[w3]=0;
    		lgv--;
    	}	
    }
    
  
    //looping to arrange in increasing order the no of bits in the inputs on each face
    for(int v1=0;v1<lgv;v1++)
    {
    	for(int v2=0;v2<(lgv-1);v2++)
    	{
    		if(gv[v2]>gv[v2+1])
    		{
    			int temp=gv[v2];
    			gv[v2]=gv[v2+1];
    			gv[v2+1]=temp;
    		}
    	}
    }
    
  
    
    int nullflag=0;
    int[][] gc = new int[5000][3];
    int lgc=0;
    
    int w1=0;
    int w4=0;
    
    //looping to create a glue compare strap according to the no of bits in input taking all faces into consideration
    for(int q1=0;q1<lgv;q1++)
    {
    	int w2=gv[q1];
    	if(w1<w2) w1=w1+w2;
    	int esl = 2;
    	for(int es=1;es<gv[q1];es++)
    	{
    		esl = esl*2;
    	}
    	
    	if(gv[q1]==0) esl=0;
    	
    	if(esl==0)
    	{
    		gc[lgc][0] = 0;
    		gc[lgc][1] = 0;
    		gc[lgc][2] = 3;
    		lgc++;nullflag=1;
    	}
    	
    	
    	
    	for(int q2=0;q2<esl;q2++)
    	{
    		
    		
    		
    		gc[lgc][0] = w4;
    		
    		gc[lgc][1] = w2;
    		gc[lgc][2] = tobin(w1,w2); //calling binary padding function 
    		gc[lgc][0]++;
    		lgc++;w1++;w4++;
    	}
    	
    	  	
    }
    
  	//displaying the glue pattern used
    glue_strap="\n in binary format..the glues we have used are\n";
    glue_strap=glue_strap.concat("\n").concat("Glue no").concat("\t").concat("no. of bits").concat("\t").concat("binary code");
    for(int vb=0;vb<lgc;vb++)
    {	
    	String fk2 = null;
    	fk2 = ""+gc[vb][0];
    	String fk3 = null;
    	fk3 = ""+gc[vb][1];
    	glue_strap=glue_strap.concat("\n").concat(fk2).concat("\t\t").concat(fk3).concat("\t\t");
    	int tv = gc[vb][2];
    	
    	
    		int temp=tv;
    		int pww=1;
    		int pkr=0;
    		do
    		{	
    			temp=temp/10;
    			pkr++;
    		}while(temp>0);
    		
    		for(int nc=1;nc<pkr;nc++)
    		{
    			pww=pww*10;
    		}
    		
    		int temp1=0;
    		
    		do		//reversing
    		{
    			int ug1 = tv%10;
    			temp1=temp1+pww*ug1;
    			tv=tv/10;pww=pww/10;
    		}while(tv>0);
    		
    		tv=temp1;
    		
    	//removing the dummy mask and displaying the correct used codes for the glues
    	do{
    		int tp = tv%10;			
    		if(tp==4) glue_strap=glue_strap.concat("0");
    		else if(tp==5) glue_strap=glue_strap.concat("1");
    		else if(tp==3) glue_strap=glue_strap.concat("null");
    		tv=tv/10;
    	}while(tv>0);
    }
    
    //north glues array
    int[][] nga = new int[r][2];
    if(size_north == 0)
    {
    	for(int zt=0;zt<r;zt++)		//entering code for null glue when no inputs present
    	{
    	   	nga[zt][0]=3;
    		nga[zt][1]=0;
    	}
    }
    else
    {
    	int ft=1;
    	for(int tf=0;tf<size_north;tf++)
    	{
    		ft=ft*2;
    	}
    	for(int v=0;v<r;v++)
    	{
    		nga[v][0]=0;
    	}
    	
    	//nested looping to convert binary codes in a format 
    	//so as to be comparable to glue codes
    	for(int g1=0;g1<r;g1++)
    	{
    		int pwo=1;
    		for(int g2=0;g2<size_north;g2++)
    		{
    			nga[g1][0]=nga[g1][0]+arr[g1][g2]*pwo;
    			pwo=pwo*10;
    		
    		}
    		
    	
    		int temp=nga[g1][0];
    		int pww=1;
    		int pkr=0;
    		do
    		{	
    			temp=temp/10;
    			pkr++;
    		}while(temp>0);
    		
    		for(int nc=1;nc<pkr;nc++)
    		{
    			pww=pww*10;
    		}
    		
    		int temp1=0;
    		//reversing
    		do
    		{
    			int ug1 = nga[g1][0]%10;
    			temp1=temp1+pww*ug1;
    			nga[g1][0]=nga[g1][0]/10;pww=pww/10;
    		}while(nga[g1][0]>0);
    		
    		nga[g1][0]=temp1;
    		//comparing with glue strap
    		for(int g3=0;g3<lgc;g3++)
    		{
    			if(nga[g1][0]==gc[g3][2]){nga[g1][1]=gc[g3][0];break;}
    		}
    		
    		
    	}
    
	}
	
	
	//east glues array
    int[][] ega = new int[r][2];
    if(size_east == 0)
    {
    	for(int zt=0;zt<r;zt++)
    	{
    	   	ega[zt][0]=3;
    		ega[zt][1]=0;
    	}
    }
    else
    {
    	int ft=1;
    	for(int tf=0;tf<size_east;tf++)
    	{
    		ft=ft*2;
    	}
    	for(int v=0;v<r;v++)
    	{
    		ega[v][0]=0;
    	}
    	for(int g1=0;g1<r;g1++)
    	{
    		int pwo=1;
    		for(int g2=size_north;g2<size_east+size_north;g2++)
    		{
    			ega[g1][0]=ega[g1][0]+arr[g1][g2]*pwo;
    			pwo=pwo*10;
    		
    		}
    		
    	
    		int temp=ega[g1][0];
    		int pww=1;
    		int pkr=0;
    		do
    		{	
    			temp=temp/10;
    			pkr++;
    		}while(temp>0);
    		
    		for(int nc=1;nc<pkr;nc++)
    		{
    			pww=pww*10;
    		}
    		
    		int temp1=0;
    		
    		do
    		{
    			int ug1 = ega[g1][0]%10;
    			temp1=temp1+pww*ug1;
    			ega[g1][0]=ega[g1][0]/10;pww=pww/10;
    		}while(ega[g1][0]>0);
    		
    		ega[g1][0]=temp1;
    		for(int g3=0;g3<lgc;g3++)
    		{
    			if(ega[g1][0]==gc[g3][2]){ega[g1][1]=gc[g3][0];break;}
    		}
    		
    		
    	}
    
	}
	
	
	
	//south glues array
    int[][] sga = new int[r][2];
    if(size_south == 0)
    {
    	for(int zt=0;zt<r;zt++)
    	{
    	   	sga[zt][0]=3;
    		sga[zt][1]=0;
    	}
    }
    else
    {
    	int ft=1;
    	for(int tf=0;tf<size_south;tf++)
    	{
    		ft=ft*2;
    	}
    	for(int v=0;v<r;v++)
    	{
    		sga[v][0]=0;
    	}
    	for(int g1=0;g1<r;g1++)
    	{
    		int pwo=1;
    		for(int g2=size_north+size_east;g2<size_north+size_east+size_south;g2++)
    		{
    			sga[g1][0]=sga[g1][0]+arr[g1][g2]*pwo;
    			pwo=pwo*10;
    		
    		}
    		
    		
    		int temp=sga[g1][0];
    		int pww=1;
    		int pkr=0;
    		do
    		{	
    			temp=temp/10;
    			pkr++;
    		}while(temp>0);
    		
    		for(int nc=1;nc<pkr;nc++)
    		{
    			pww=pww*10;
    		}
    		
    		int temp1=0;
    		
    		do
    		{
    			int ug1 = sga[g1][0]%10;
    			temp1=temp1+pww*ug1;
    			sga[g1][0]=sga[g1][0]/10;pww=pww/10;
    		}while(sga[g1][0]>0);
    		
    		sga[g1][0]=temp1;
    		for(int g3=0;g3<lgc;g3++)
    		{
    			if(sga[g1][0]==gc[g3][2]){sga[g1][1]=gc[g3][0];break;}
    		}
    		
    		
    	}
    
	}
	
	//west glues array
    int[][] wga = new int[r][2];
    if(size_west == 0)
    {
    	for(int zt=0;zt<r;zt++)
    	{
    	   	wga[zt][0]=3;
    		wga[zt][1]=0;
    	}
    }
    else
    {
    	int ft=1;
    	for(int tf=0;tf<size_west;tf++)
    	{
    		ft=ft*2;
    	}
    	for(int v=0;v<r;v++)
    	{
    		wga[v][0]=0;
    	}
    	for(int g1=0;g1<r;g1++)
    	{
    		int pwo=1;
    		for(int g2=(size_south+size_east+size_north);g2<(size_south+size_east+size_north+size_west);g2++)
    		{
    			wga[g1][0]=wga[g1][0]+arr[g1][g2]*pwo;
    			pwo=pwo*10;
    		
    		}
    		
    		int temp=wga[g1][0];
    		int pww=1;
    		int pkr=0;
    		do
    		{	
    			temp=temp/10;
    			pkr++;
    		}while(temp>0);
    		
    		for(int nc=1;nc<pkr;nc++)
    		{
    			pww=pww*10;
    		}
    		
    		int temp1=0;
    		
    		do
    		{
    			int ug1 = wga[g1][0]%10;
    			temp1=temp1+pww*ug1;
    			wga[g1][0]=wga[g1][0]/10;pww=pww/10;
    		}while(wga[g1][0]>0);
    		
    		wga[g1][0]=temp1;
    		
    		
    		for(int g3=0;g3<lgc;g3++)
    		{
    			if(wga[g1][0]==gc[g3][2]){wga[g1][1]=gc[g3][0];break;}
    		}
    		
    		
    	}
    
	}
	
	//Displaying menus to accept the frame tile set
	glue_strap=glue_strap.concat("\n");
	if(nullflag==1) lgc--;
	String fk1 = null;
	fk1 = "" + lgc;
	glue_strap=glue_strap.concat("no of glues used till now =").concat(fk1);
	int mglue=0;
			
			
        	gls.setText(glue_strap + newline);		
          	confirmButton.setLabel("Confirmation Done");
          	okButton.setLabel("Confirmation Done");
            north_inputs.setText(north_inputs.getText());
            east_inputs.setText(east_inputs.getText());
            south_inputs.setText(south_inputs.getText());
            west_inputs.setText(west_inputs.getText());
            repaint();
            cflag=1;
            
            if(frflag==1)
            {
            	frameButton.setLabel("frame glues submitted");
            	char[] frameip = frame_inputs.getText().toCharArray();
            	int fip1,fip2,fck1,fck2;
            	fck2=1;
            	fip1 = 0;
            /*	do
            	{
            		if(frameip[fip2]==';') fip1++;
            		fip2++;  
            		
            	}while(fip2<frame_inputs.getText().length());*/
            	
            	int[][] frameint = new int[100][4];
            	for(int ghod1=0;ghod1<(100);ghod1++)
            	{
            		for(int ghod2=0;ghod2<4;ghod2++)
            		{
            			frameint[ghod1][ghod2]=0;
            		}
            	}
            	fip2 = 0;
            	fip1 = 0;
            	int qrr,qrc;
            	qrr=0;qrc=0;
            	do
            	{
            		for(int fcku=0;fcku<fip1;fcku++)
            		{
            			fck2=fck2*10;	
            			
            		}	
            		if(frameip[fip2]!=',' && frameip[fip2]!=';')
            		{
            			//String must = null;
            			//must=""+frameip[fip2];
            		
            			//int herm;
            			//herm = intValue(Integer.valueOf(String.valueOf(frameip[fip2])));
            			frameint[qrr][qrc]=(((int)frameip[fip2])-48)+frameint[qrr][qrc]*10;;//herm+frameint[qrr][qrc]*fck2;
            			
            			fip1++;
            		}
            		else
            		{
            		
            			fip1=0;	
            			if(qrc==3){ qrc=0;qrr++;}
            			else qrc++;	
            			
            		}
            		
            		if((fip2+1)==frame_inputs.getText().length())
            		{
            		
            			fip1=0;	
            			if(qrc==3){ qrc=0;qrr++;}
            			else qrc++;	
            		}
            		
            		fip2++;
            		
            	}while(fip2<frame_inputs.getText().length());	
            	
            	String disp = new String();
            	disp = "";
            	
            	int nft = qrr;
            	         	
            	
            	int[] aux= new int[1000];
	
				for(int fg=0;fg<1000;fg++)
				{
					aux[fg]=0;
				}
            	int[][] glue_array = new int[nft+r][4];
			for(int as=0;as<nft;as++)
			{
				glue_array[as][0] = frameint[as][0];
				if(glue_array[as][0]>lgc)	//checking to see if new glues are entered
				{
					int flagg=0;
					for(int ui=0;ui<mglue;ui++)
					{
						if(glue_array[as][0] == aux[ui]) flagg=1;
					}
				
					if(flagg==0)
					{
						aux[mglue]=glue_array[as][0];	
						mglue++;
					}
				}
				glue_array[as][1] = frameint[as][1];
				if(glue_array[as][1]>lgc)
				{
					int flagg=0;
					for(int ui=0;ui<mglue;ui++)
					{
						if(glue_array[as][1] == aux[ui]) flagg=1;
					}
			
					if(flagg==0)
					{
						aux[mglue]=glue_array[as][1];
						mglue++;
					}
		}
		glue_array[as][2] = frameint[as][2];
		if(glue_array[as][2]>lgc)
		{
			int flagg=0;
			for(int ui=0;ui<mglue;ui++)
			{
				if(glue_array[as][2] == aux[ui]) flagg=1;
			}
			
			if(flagg==0)
			{
				aux[mglue]=glue_array[as][2];
				mglue++;
			}
		}
		glue_array[as][3] = frameint[as][3];
		if(glue_array[as][3]>lgc)
		{
			int flagg=0;
			for(int ui=0;ui<mglue;ui++)
			{
				if(glue_array[as][3] == aux[ui]) flagg=1;
			}
			
			if(flagg==0)
			{
				aux[mglue]=glue_array[as][3];
				mglue++;
			}
		}
			
	}
	int g=0;
	//storing the computational tiles glues in the final glue array
	for(int as=nft;as<(r+nft);as++)
	{
		glue_array[as][0] = nga[g][1];
		glue_array[as][1] = ega[g][1];
		glue_array[as][2] = sga[g][1];
		glue_array[as][3] = wga[g][1];
		g++;
		
	}
	
	//displaying the created .tile file
	String timed = Tilegen.now();
		
	disp = disp.concat("/*This .tiles file was generated using XTile 1.0 ").concat("at ").concat(timed);
	disp = disp.concat("\n").concat("developed by Anshul Chaurasia, Sudhanshu Dwivedi and Prateek Jain, DA-IICT,INDIA");
	disp = disp.concat("\n").concat("detailed documentation and tool available at www.guptalab.org/xtile");
	disp = disp.concat("\n").concat("this .tiles file is input to Xgrow  developed by Eric Winfree, Caltech ").concat("*/");
	disp = disp.concat("\n");
	
	disp = disp.concat("tile edges matches {{N E S W}*}");
	String dk1 = ""; dk1 = dk1 + (nft+r);
	disp = disp.concat("\n").concat("num tile types=").concat(dk1);
	String dk2 = ""; dk2= dk2 + (mglue+lgc);
	disp = disp.concat("\n").concat("num binding types=").concat(dk2);
	disp = disp.concat("\n").concat("tile edges={");
	for(int bh=0;bh<(nft+r);bh++)
	{
		String dk3 = ""; dk3 = ""+glue_array[bh][0];
		String dk4 = ""; dk4 = ""+glue_array[bh][1];
		String dk5 = ""; dk5 = ""+glue_array[bh][2];
		String dk6 = ""; dk6 = ""+glue_array[bh][3];
		disp = disp.concat("\n").concat("{").concat(dk3).concat("\t").concat(dk4).concat("\t").concat(dk5).concat("\t").concat(dk6).concat("}");
		repaint();
	}
	disp = disp.concat("\n").concat("}").concat("\n").concat("binding strengths=").concat("\n").concat("{");
	for(int d1=0;d1<lgc;d1++)
	{
		disp = disp.concat("1").concat("\t");
	}
	for(int d1=0;d1<mglue;d1++)
	{
		disp = disp.concat("2").concat("\t");
	}
	disp = disp.concat("}").concat("\n");
            	
     tls.setText(disp + newline);  
     
          
		 	
            	
            }	
            
            
          } 
          
          
         }
         
    static String now() {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
    return sdf.format(cal.getTime()); 
    }     
         
         
        static int tobin(int x1,int x2)
		{
		
		
		int x3,x4;
		x3=0;
		x4=0;
		
		int n=0;
		do{
			int pw=1;
			for(int t=0;t<n;t++)
			{
				pw=pw*10;
			}
			x3=x1%2;
			x1=x1/2;
			x4=x4+(x3+4)*pw;
			n++;
			x2--;
			if(x2==0) break;
		}while(x1>0);
		
		if(x2>0)
		{
			do
			{
				int pw=1;
				for(int t=0;t<n;t++)
				{
					pw=pw*10;
				}
				x4=x4+(4)*pw;
				n++;
				x2--;
			}while(x2>0);	
		}
		
		return x4;
         
         }  
       
}
