/*
 * @(#)Tilegen.java 1.0 08/08/18
 *
 * 
 * 
 *
 * 
 * 
 * 
 *
 */

import java.awt.*;
import java.applet.*;
import java.net.*;
import java.awt.event.*;

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;

import java.util.Calendar;
import java.text.SimpleDateFormat;

import java.util.NoSuchElementException;

public class Tilegen11 extends Applet implements ActionListener {
	
	 Image tile_gif,qug_gif;
	 URL base;
	 MediaTracker mt;
	 Button okButton; 
	 Button confirmButton; 
	 Button frameButton; 
	 Button proofButton;
	 Button snakeButton;
	 TextField north_inputs,east_inputs,south_inputs,west_inputs,frame_inputs,proof_inputs,snake_inputs;
	 TextArea gls,tls,proof,snake; 
	 int cflag,frflag,proofflag,snakeflag;
	 String newline;
	 
	 public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	 
	 
	 
	 static Formatter output1k;
	 static Formatter output2k;
	 static Formatter output3k;
	 
	static Formatter output;
	static Formatter output1;
	static Formatter output2;//for writing the vertical frame tiles
	static Formatter output3;//for writing the horiz6ontal frame tiles
	static Formatter output4;//for writing the seed tile
	
	static Formatter output5;
	static Formatter output6;
	static Formatter output7;
	
	static Formatter output8;
	
	static Scanner input1;
	static Scanner input2;
	static Scanner input3;
	static Scanner input4;
	static Scanner input5;//for reading frame tiles' glues
	
	static Scanner input6;//for proofreading verticalframe tiles' glues
	static Scanner input7;//for proofreading horizontal frame tiles' glues
	static Scanner input8;//for proofreading seed frame tiles' glues
	
	static Scanner input9;//for finding total glues in computational tiles before any error correction;
	
	static Scanner input10;
	
	static Scanner input11;
	static Scanner input12;
	static Scanner input13;
	
	static Scanner sinput1;
	static Scanner sinput2;
	static Scanner sinput3;
	static Scanner sinput4;
	static Scanner sinput5;
	static Scanner sinput6;
	static Scanner sinput7;
	static Scanner sinput8;
	static Scanner sinput9;
	static Scanner sinput10;
		
	static Formatter soutput1;//for writing vertical frame tiles without any manipulation to the original .tile structure
	static Formatter soutput2;//for writing horizontal frame tiles without any manipulation to the original .tile structure
	static Formatter soutput3;//for writing seed tiles without any manipulation to the original .tile structure
	static Formatter sgh4;//for writing all broken frame tiles into snake_frame.txt file
	static Formatter sgh5;
	
	
	public void init() {
		
		setLayout(null); 
		okButton = new Button("Submit Inputs"); 
		frameButton = new Button("Submit frame glues");
		confirmButton = new Button("Click to confirm"); 
		proofButton = new Button("Apply proofread"); 
		snakeButton = new Button("Apply Snake");
			
		north_inputs = new TextField("Type north outputs here",40); 
		east_inputs = new TextField("Type east inputs here",40); 
		south_inputs = new TextField("Type south inputs here",40); 
		west_inputs = new TextField("Type west outputs here",40); 
		snake_inputs = new TextField("m",80);
		snake_inputs.setBounds(500,740,80,30);
		add(snake_inputs);
			
		gls = new TextArea(10,40);
		gls.setEditable(false);
		
		 
		tls = new TextArea(10,40);
		tls.setEditable(false);
		
		proof = new TextArea(10,40);
		proof.setEditable(false);
		
		snake = new TextArea(10,40);
		snake.setEditable(false);
		
		frame_inputs = new TextField("Type frame inputs here",80);
		frame_inputs.setBounds(500,320,400,30); 
		add(frame_inputs);
		
		proof_inputs = new TextField("m",80);
		proof_inputs.setBounds(10,740,80,30); 
		add(proof_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,420,400,280);
		add(tls);
		
		proof.setBounds(10,790,400,300);
		add(proof);
		
		snake.setBounds(500,790,400,300);
		add(snake);
			
		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,370,200,30);
		add(frameButton); 
		frameButton.addActionListener(this); 
		
		proofButton.setBounds(120,740,100,30);
		add(proofButton);
		proofButton.addActionListener(this); 
			
		snakeButton.setBounds(600,740,100,30);
		add(snakeButton);
		snakeButton.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 Tilegen Applet", 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 );
		
		
		g.drawString("  below give value for 'm' to perform m*m proofread ",10,720);
		g.drawString("  below give value for 'm' to perform m*m snake ( m should be an even integer)",500,720);
	}
	
	     public void actionPerformed(ActionEvent evt) 
         {
         	frflag=0;
         	proofflag=0;
         	snakeflag=0;
         	if(evt.getSource() == frameButton)
              {
              	frflag=1;
              }
              
              if(evt.getSource() == proofButton)
              {
              	proofflag=1;
              }
              
              if(evt.getSource() == snakeButton)
              {
              	snakeflag=1;
              }
         
              if (evt.getSource() == okButton){ repaint();okButton.setLabel("Submitted");cflag=0;frflag=1;}
              else if ((evt.getSource() == confirmButton && cflag==0) || frflag==1 || proofflag==1 || snakeflag==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 || proofflag==1 || snakeflag==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 = Tilegen11.now();
		
	disp = disp.concat("/*This .tiles file was generated using XTile 1.1 ").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);  
     
     
     
     try
		{
			output1k = new Formatter("glues.txt");//for non frame tiles
			output2k = new Formatter("normal.tiles");// writing a normal .tile file
			output3k = new Formatter("glues2.txt");//for frame tiles
			
					
			//System.out.printf("\n**************************************************");
			//System.out.printf("\nThe final .tile file is in your current folder");
			//System.out.printf("\n**************************************************");
			output2k.format("tile edges matches {{N E S W}*}\n");
			output2k.format("num tile types=%d\n",nft+r);
			output2k.format("num binding types=%d\n",mglue+lgc);
			output2k.format("tile edges={\n");
			int bh;
			for(bh=0;bh<(nft+r);bh++)
			{
				output2k.format("{%d\t%d\t%d\t%d}\n",glue_array[bh][0],glue_array[bh][1],glue_array[bh][2],glue_array[bh][3]);
				i=0;
				
				if(glue_array[bh][i]==0||glue_array[bh][i+1]==0||glue_array[bh][i+2]==0||glue_array[bh][i+3]==0)
					output3k.format("%d\t%d\t%d\t%d\n",glue_array[bh][0],glue_array[bh][1],glue_array[bh][2],glue_array[bh][3]);
				else
					output1k.format("%d\t%d\t%d\t%d\n",glue_array[bh][0],glue_array[bh][1],glue_array[bh][2],glue_array[bh][3]);
				
				
			}
			
			output2k.format("}\nbinding strengths=\n{");
			
			for(int d1=0;d1<lgc;d1++)
			{
				output2k.format("1\t");
			}
			
			for(int d1=0;d1<mglue;d1++)
			{
				output2k.format("2\t");
			}
			output2k.format("}\n");
			output1k.close();
			output2k.close();
			output3k.close();
		}
		
		catch ( SecurityException securityException )
      	{
         System.err.println("You do not have write access to this file." );
         System.exit( 1 );
      	} // end catch
      	catch ( FileNotFoundException filesNotFoundException )
      	{
         System.err.println( "Error creating file." );
         System.exit( 1 );
      	} // end catch
      	catch ( FormatterClosedException formatterClosedException )
        {
            System.err.println( "Error writing to file." );
            return;
        } // end catch     	
            	
        if(proofflag==1)
        {
        	int m;//for m*m proofread the value of m
			m=0;
		//System.out.println("We can give you the tileset for proofread of m*m give us the value of m \n allowable values of n are 2,4,16 and so on ");
		//Scanner input = new Scanner(System.in);
		//char[] frameip = frame_inputs.getText().toCharArray();
		proofButton.setLabel("m submitted");
		char[] housem = proof_inputs.getText().toCharArray();
		int houseml = proof_inputs.getText().length();
		int sky=0;
		do
		{
			m=m*10+((int)housem[sky])-48;
			sky++;
		}while(sky<houseml);
		
		String dk31 = ""; dk31 = ""+m;
		proof.setText(dk31 + newline);
		
		int strength[] = new int[2000];//array for glue strength 
		
		int y = 0, big = 0;
		
		int glue_strength_1 =0, glue_strength_2 = 0;
		
		try
		{
			input1 = new Scanner(new File("glues2.txt"));//all the initial frame & seed tiles' glue
			input2 = new Scanner(new File("glues.txt"));//all the initial computational tiles(non frame and non seed)
			input5 = new Scanner(new File("glues2.txt"));
			input9 = new Scanner(new File("glues.txt"));
									
			
			output2 = new Formatter("vertical_frame_tiles.txt");
			output3 = new Formatter("horizontal_frame_tiles.txt");
			output4 = new Formatter("seed_tile.txt");
			
			output5 = new Formatter("seed_only.txt");
			output6 = new Formatter("frame_only.txt");
			output7 = new Formatter("comp_only.txt");
			
			
			int ndd,edd,sdd,wdd;
			//to find the total glues in computational tiles before any error correction;
			int x1=0,big2=0;
			
			while(input9.hasNext())
			 	{
			 		x1 = input9.nextInt();
			 		if(big2<x1)
			 		big2=x1;//big2 is highest no. of the inner tile without any manipulation
			 		
			 	}
			//System.out.printf("%d\n",big2);
			while(y < big2)
			{
				strength[y] = 1;
				y++;
			}//strength of the inner tiles
			
			while(input5.hasNext())
			{
				ndd=input5.nextInt();
				edd=input5.nextInt();
				sdd=input5.nextInt();
				wdd=input5.nextInt();
				
				if(edd==0 && sdd!=0)
					output2.format("%d %d %d %d\n",ndd,edd,sdd,wdd);
				if(edd==0 && sdd==0)
					output4.format("%d %d %d %d\n",ndd,edd,sdd,wdd);
				if(edd!=0 && sdd==0)
					output3.format("%d %d %d %d\n",ndd,edd,sdd,wdd);
			
			}
			output2.close();
			output3.close();
			output4.close();
			
			output = new Formatter("glue_proof_read.txt");
			int xdd;
			big=0;
			while(input1.hasNext())
			 	{
			 		xdd = input1.nextInt();
			 		if(big<xdd)
			 		big=xdd;
			 		
			 	}
			 	int big3 = big;//big3 is highest no. before any manipulation in frames as well as inner tiles
			 	//System.out.printf("\n %d\n",big);
			 	while(y < big3)
			 	{
			 		strength[y] = 2;
			 		y++;
			 	}//for strength of the initial frame tiles
			 	
			int a,b,cfg,d;
			int h[]=new int[(m*m)+m];//for specifying the inner horizontal glues while Proofreading
			int v[]=new int[(m*m)+m];//for specifying the inner vertical glues while Proofreading
			int j;
			
			while(input2.hasNext())
			{
				a = input2.nextInt();
				b = input2.nextInt();
				cfg = input2.nextInt();
				d = input2.nextInt();
				
				for(i=0;i<(m*m);i = i+(m+1))//for filling the side glues of a broken computational tile
				{
					v[i]=b;
					v[i+m]=d;
					h[i]=cfg;
					h[i+m]=a;
				}
				
					/*for(i=0;i<(m*m);i = i+(m+1))
				{
					v[i]=b;
					v[i+m]=d;
					h[i]=c;
					h[i+m]=a;
				}*/
				
				for(j=1;j<=m;j++)//for filling the horizontal glues of inner tiles of a broken tile while Proofreading
				{
					for(i=1+((m+1)*(j-1));i<((m+1)*j)-1;i++)
					{
						h[i]=++big;
					}	
				}
				for(j=1;j<=m;j++)//for filling the vertical glues of inner tiles of a broken tile while Proofreading
				{
					for(i=1+((m+1)*(j-1));i<((m+1)*j)-1;i++)
					{
						v[i]=++big;
					}
				}
				
				int z1,z2,z3,z4;
				
				for(j=0;j<m;j++)//for writting the computational tiles glues after proofreading in a table in file glues_proof_read.txt
				{
					for(i=0,z1=j*((m+1))+1,z2=j,z3=j*(m+1),z4=j+1;i<m;i++,z1++,z2=z2+(m+1),z3++,z4=z4+(m+1))
					{
						output.format("%d %d %d %d\n",h[z1],v[z2],h[z3],v[z4]);
						output7.format("%d %d %d %d\n",h[z1],v[z2],h[z3],v[z4]);
						
					}
				}
			}
			//glue_strength_1 = big - (big3 - big2);
			//System.out.printf("\n%d\n",big);
			while(y < big)//strength of all the computational tiles which have been broken
			{
				strength[y] = 1;
				y++;
			}
			
			
			
		//vertical frame tiles being broken for proofread
		
		input6 = new Scanner(new File("vertical_frame_tiles.txt"));
		a= b= cfg= d= 0;
		int hr[] = new int[m+1];//m+1
		int vr[] = new int[2*m]; //2*m		
		while(input6.hasNext())
		{
			a = input6.nextInt();
			b = input6.nextInt();
			cfg = input6.nextInt();
			d = input6.nextInt();
			
			for(i = 0; i<= m-1; i++) //i <= m-1
				vr[i] = b;
			
			for(i = m; i<= 2*m - 1; i++) //i = n; i <= 2*n - 1
				vr[i] = d;
			
				hr[0] = cfg;
			
				hr[m] = a; //hr[n]
			
			for(i = 1; i <= m-1; i++ ) //	i <=n-1		
				hr[i] = ++big;
			
			for(i = 0;i < m; i++ ) //i<n
			{	
				output.format("%d %d %d %d\n",hr[i+1], vr[i], hr[i], vr[i+m]); //vr[i+n]
				output6.format("%d %d %d %d\n",hr[i+1], vr[i], hr[i], vr[i+m]);
			}
		}
			
			//horizontal frame tiles being broken for proofread
		
		input7 = new Scanner(new File("horizontal_frame_tiles.txt"));
		a= b= cfg= d= 0;
		int vr1[] = new int[m+1];//n+1
		int hr1[] = new int[2*m]; //2*n		
		while(input7.hasNext())
		{
			a = input7.nextInt();
			b = input7.nextInt();
			cfg = input7.nextInt();
			d = input7.nextInt();
			
			for(i = 0; i<= m-1; i++) //i <= n-1
				hr1[i] = cfg;
			
			for(i = m; i<= 2*m - 1; i++) //i = n; i <= 2*n - 1
				hr1[i] = a;
			
				vr1[0] = b;
			
				vr1[m] = d; //vr1[n]
			
			for(i = 1; i <= m-1; i++ ) //	i <=n-1		
				vr1[i] = ++big;
			
			for(i = 0;i < m; i++ ) //i<n
			{
				output.format("%d %d %d %d\n",hr1[i+m], vr1[i], hr1[i], vr1[i+1]); //hr1[i+n]
				output6.format("%d %d %d %d\n",hr1[i+m], vr1[i], hr1[i], vr1[i+1]); //hr1[i+n]
			}
		}
			
		//for seed tile
		input8 = new Scanner(new File("seed_tile.txt"));
		input10 = new Scanner(new File("seed_tile.txt"));
		
		input11 = new Scanner(new File("seed_only.txt"));
		input12 = new Scanner(new File("frame_only.txt"));
		input13 = new Scanner(new File("comp_only.txt"));
		
		output.format("%d %d %d %d\n", input8.nextInt(),input8.nextInt(),input8.nextInt(),input8.nextInt());	
		output5.format("%d %d %d %d\n", input10.nextInt(),input10.nextInt(),input10.nextInt(),input10.nextInt());	
					
		output.close();	
		output5.close();
		output6.close();
		output7.close();
		//glue_strength_2 = big - glue_strength_1;
		//System.out.printf("%d\n",big);
		
		//writing tiles in the correct order------------------------------------------------------
		
		output8 = new Formatter("gpf_read.txt");
		
		while(input11.hasNext())
		{
			output8.format("%d %d %d %d\n",input11.nextInt(),input11.nextInt(),input11.nextInt(),input11.nextInt());
		}
		
		while(input12.hasNext())
		{
			output8.format("%d %d %d %d\n",input12.nextInt(),input12.nextInt(),input12.nextInt(),input12.nextInt());
		}
		
		while(input13.hasNext())
		{
			output8.format("%d %d %d %d\n",input13.nextInt(),input13.nextInt(),input13.nextInt(),input13.nextInt());
		}
		
		output8.close();
		
		while(y < big)
		{
			strength[y] = 2;
			y++;
		}
		}
		
		catch ( SecurityException securityException )
      {
         System.err.println(
            "You do not have write access to this file." );
         System.exit( 1 );
      } // end catch
      catch ( FileNotFoundException filesNotFoundException )
      {
         System.err.println( "Error creating file." );
         System.exit( 1 );
      } // end catch
      catch ( FormatterClosedException formatterClosedException )
         {
            System.err.println( "Error writing to file." );
            return;
         } // end catch
         catch ( NoSuchElementException elementException )
        {
            System.err.println( "Invalid input. Please try again." );
            input6.nextLine(); // discard input so user can try again
         } // end catch
		
		
		//converting the glue table into a .tiles file
		
		try
		 {
		 	input3 = new Scanner(new File("gpf_read.txt"));
		 	input4 = new Scanner(new File("gpf_read.txt"));
		 	int counter,big1,a1c;
			counter = 0;
			big1=0;	
		 	while(input3.hasNext())
		 	{
		 		a1c = input3.nextInt();
		 		if(big1<a1c)
		 		big1=a1c;
		 		++counter;
		 	}
		 	int num_tile_types = counter/4;
		 	//System.out.printf("big ele is %d and no. o tiles is %d\n",big1,num_tile_types);
		 String proofread = "";
		 output1 = new Formatter("proof_read.tiles");
		 output1.format("tile edges matches {{N E S W}*}\n");
		 output1.format("num tile types=%d\n",num_tile_types);
		 output1.format("num binding types=%d\ntile edges={\n",big1);
		 
		 String timedp = Tilegen11.now();
		
		proofread = proofread.concat("/*This .tiles file was generated using XTile 1.1 ").concat("at ").concat(timedp);
		proofread = proofread.concat("\n").concat("developed by Anshul Chaurasia, Sudhanshu Dwivedi and Prateek Jain, DA-IICT,INDIA");
		proofread = proofread.concat("\n").concat("detailed documentation and tool available at www.guptalab.org/xtile");
		proofread = proofread.concat("\n").concat("this .tiles file is input to Xgrow  developed by Eric Winfree, Caltech ").concat("*/");
		proofread = proofread.concat("\n");
		 
		 proofread = proofread.concat("tile edges matches {{N E S W}*}\n");
		 proofread = proofread.concat("num tile types=").concat(String.valueOf(num_tile_types)).concat("\n");
		 proofread = proofread.concat("num binding types=").concat(String.valueOf(big1)).concat("\n").concat("tile edges={\n");
		 
		 int p,q,rdd,s;
		 while(input4.hasNext())
		 {
		 	p=input4.nextInt();
		 	q=input4.nextInt();
		 	rdd=input4.nextInt();
		 	s=input4.nextInt();
		 	output1.format("{%d\t%d\t%d\t%d}\n",p,q,rdd,s);
		 	proofread = proofread.concat("{").concat(String.valueOf(p)).concat("\t").concat(String.valueOf(q)).concat("\t").concat(String.valueOf(rdd)).concat("\t").concat(String.valueOf(s)).concat("}\n");
		 }
		 output1.format("}\n");
		 output1.format("binding strengths=\n{");
		proofread = proofread.concat("}\n");
		 proofread = proofread.concat("binding strengths=\n{");	
			for(y = 0; y < big; y++)
			{
				output1.format("%d\t",strength[y]);
				proofread = proofread.concat(String.valueOf(strength[y])).concat("\t");
			}
			
			/*for(int d1=0; d1< glue_strength_2; d1++)
			{
				output1.format("2\t");
			}*/	 
			output1.format("}");
			proofread = proofread.concat("}");
			proof.setText(proofread + newline);
		 output1.close();
		 }
		 catch ( FileNotFoundException fileNotFoundException )
      	 {
          	System.err.println( "Error opening file." );
         	System.exit( 1 );
         	
      	 } // end catch
      	 
      	 repaint();
        }	
        	
        if(snakeflag==1)
        {
        	int sstrength[] = new int[4000];
		int sglue[][][] = new int[1000][1000][4];
		int sn , ss, se, sw;//they are the four glues for four different sides of a computational tile
		int sa, sb, sc, sd;//
		int sx;//would help to find the biggest glue no. before any manipulation
		int ssi,sj;//would work as indexes for the tile nos. inside a broken computational tile
		int sbig=0;//highest number of thew glue upto that point
		int sbig1=0 , sbig2 = 0;
		int sk;//k = m /2 which would be used for utilizing for the Ashish Goel's Snake condition
		int sm=0;
		int stemp = 0; //a temperory variable which would be used for giving strength value to the glue numbers
		int snum_tile_types = 0;
		int snum_binding_types = 0;
		int sbig3 = 0;//for finding the biggest glue number in the computational tiles 
		try{
			
			
			soutput1 = new Formatter("snake_vertical_frame.txt");
			soutput2 = new Formatter("snake_horizontal_frame.txt");
			soutput3 = new Formatter("snake_seed.txt");
			sgh4 = new Formatter("snake_frame_broken.txt");
			sgh5 = new Formatter("snake.tiles");
			
			sinput1 = new Scanner(new File("glues2.txt"));
			sinput2 = new Scanner(new File("glues2.txt"));
			sinput3 = new Scanner(new File("snake_vertical_frame.txt"));
			sinput4 = new Scanner(new File("snake_horizontal_frame.txt"));
			sinput5 = new Scanner(new File("snake_seed.txt"));
			sinput6 = new Scanner(new File("glues.txt"));
			sinput7 = new Scanner(new File("snake_frame_broken.txt"));
			sinput8 = new Scanner(new File("snake_frame_broken.txt"));
			sinput9 = new Scanner(new File("snake_frame_broken.txt"));
			sinput10 = new Scanner(new File("glues.txt"));
			
		
		
		snakeButton.setLabel("m submitted");
		char[] shousem = snake_inputs.getText().toCharArray();
		int shouseml = snake_inputs.getText().length();
		int ssky=0;
		//int sm1 =4;
		//sm = 4;
		do
		{
			sm=sm*10+((int)shousem[ssky])-48;
			ssky++;
		}while(ssky<shouseml);
		
		
	/*	String sdk31 = ""; 
			sdk31 = ""+sm;
		snake.setText(sdk31 + newline);*/
			
		
			
			
		
			sk = sm/2;
			//System.out.printf("%d\n",k);
			
			while(sinput1.hasNext())//finding the biggest no. glue out of all the glues present
			{
				sx = sinput1.nextInt();
				if(sbig<sx)
					sbig=sx;
			}	
			//System.out.printf("%d\n",big);
				
			while(sinput2.hasNext())//seperating the frames into vertical,horizontal and seed files
				{
					sn=sinput2.nextInt();
					se=sinput2.nextInt();
					ss=sinput2.nextInt();
					sw=sinput2.nextInt();
					
					if(se==0 && ss!=0)
						soutput1.format("%d\t%d\t%d\t%d\n",sn,se,ss,sw);
					if(se!=0 && ss==0)
						soutput2.format("%d\t%d\t%d\t%d\n",sn,se,ss,sw);
					if(se==0 && ss==0)
						soutput3.format("%d\t%d\t%d\t%d\n",sn,se,ss,sw);
					
				
				}	
				soutput1.close();
				soutput2.close();
				soutput3.close();
			//seed is being written onto the snake_broken_frame.txt file	
			
			int tr1=sinput5.nextInt();
			int tr2 = sinput5.nextInt();
				int tr3=sinput5.nextInt();
			int tr4 = sinput5.nextInt();
			
			
			
				
			sgh4.format("%d\t%d\t%d\t%d\n",tr1,tr2,tr3,tr4);	
			
			
			
			
			//snake.setText(String.valueOf(tr1)+String.valueOf(tr2) + newline);
				
			//vertical frame tiles being broken for snake
			
			
			sa= sb= sc= sd= 0;
			int shr[] = new int[sm+1];//m+1
			int svr[] = new int[2*sm]; //2*m		
			while(sinput3.hasNext())
			{
				sa = sinput3.nextInt();
				sb = sinput3.nextInt();
				sc = sinput3.nextInt();
				sd = sinput3.nextInt();
				
				for(ssi = 0; ssi<= sm-1; ssi++) //i <= m-1
					svr[ssi] = sb;
				
				for(ssi = sm; ssi<= 2*sm - 1; ssi++) //i = n; i <= 2*n - 1
					svr[ssi] = sd;
				
					shr[0] = sc;
				
					shr[sm] = sa; //hr[n]
				
				for(ssi = 1; ssi <= sm-1; ssi++ ) //	i <=n-1		
					shr[ssi] = ++sbig;
				
				for(ssi = 0;ssi < sm; ssi++ ) //i<n
				{	
					sgh4.format("%d\t%d\t%d\t%d\n",shr[ssi+1], svr[ssi], shr[ssi], svr[ssi+sm]);
				}
			}
				
				//horizontal frame tiles being broken for snake
			
			
			sa= sb= sc= sd= 0;
			int svr1[] = new int[sm+1];//n+1
			int shr1[] = new int[2*sm]; //2*n		
			while(sinput4.hasNext())
			{
				sa = sinput4.nextInt();
				sb = sinput4.nextInt();
				sc = sinput4.nextInt();
				sd = sinput4.nextInt();
				
				for(ssi = 0; ssi<= sm-1; ssi++) //i <= n-1
					shr1[ssi] = sc;
				
				for(ssi = sm; ssi<= 2*sm - 1; ssi++) //i = n; i <= 2*n - 1
					shr1[ssi] = sa;
				
					svr1[0] = sb;
				
					svr1[sm] = sd; //vr1[n]
				
				for(ssi = 1; ssi <= sm-1; ssi++ ) //	i <=n-1		
					svr1[ssi] = ++sbig;
				
				for(ssi = 0;ssi < sm; ssi++ ) //i<n
				{
					sgh4.format("%d\t%d\t%d\t%d\n",shr1[ssi+sm], svr1[ssi], shr1[ssi], svr1[ssi+1]); //hr1[i+n]
				}
			}
			
			while(sinput10.hasNext())
			{	stemp = sinput10.nextInt();
				if(stemp > sbig3)
					sbig3 = stemp;	
			}
			for(ssi = sbig3 +1;ssi <= sbig; ssi++)
				sstrength[ssi] = 2;
			
			
			
			//for spliting the computational tiles
			ssi=1;
			sj=1;
			sbig1 = sbig;
		//	System.out.printf("big befor while %d",sbig);
			while(sinput6.hasNext())
			{
				//System.out.printf("\n value of big in beginning %d\n",sbig);						
				
				sbig1 = sbig;
				
				sn=sinput6.nextInt();
				se=sinput6.nextInt();
				ss=sinput6.nextInt();
				
				sw=sinput6.nextInt();
				
				for(sj=1;sj<=2*sk;sj++)// filling in the north side
				{
					for(ssi=1;ssi<=2*sk;ssi++)
					{
						if(sj==ssi+1 && ssi%2==0 && sk!=1)//for T(2i,2i+1) North condition
						{
							sglue[ssi][sj][0] = ++sbig;
							stemp = sglue[ssi][sj][0];
							sstrength[stemp] = 2;
						}
						else
						if(ssi%2==0 && sj==1 && sk!=1 && ssi != 2*sk)//for T(2i,1) North condition
						{
							sglue[ssi][sj][0] = 0;
							//temp = glue[i][j][0];
							//strength[temp] = 0;
						}
						else
						if(ssi==2*sk-2 && sj==2*sk && sk!=1)//for T(2k-2,2k) North condition
						{
							sglue[ssi][sj][0] = ++sbig;
							stemp = sglue[ssi][sj][0];
							sstrength[stemp] = 2;
						}
						else
						if(ssi==2*sk)//for T(2k,j) North condition
						{
							sglue[ssi][sj][0] = sn;
							stemp = sglue[ssi][sj][0];
							sstrength[stemp] = 1;
						}
						else//for a normal case
						{
							sglue[ssi][sj][0] = ++sbig;
							stemp = sglue[ssi][sj][0];
							sstrength[stemp] = 1;
						}
					}
				}//end of for
				
				//filling in the south side
				sbig = sbig1;
				//System.out.printf("\n%d\n",sbig);
				for(sj=1;sj<=2*sk;sj++)// filling in the south side
				{
					for(ssi=1;ssi<=2*sk;ssi++)
					{
						if(sj==ssi && ssi%2==1 && sk!=1 && ssi != 1)//for T(2i+1,2i+1) South condition
						{
							sglue[ssi][sj][2] = ++sbig;
							stemp = sglue[ssi][sj][2];
							sstrength[stemp] = 2;
						}
						else
						if(ssi%2==1 && sj==1 && sk!=1 && ssi != 1)//T(2i+1,1)
						{
							sglue[ssi][sj][2] = 0;
							//temp = glue[i][j][2];
							//strength[temp] = 0;
						}
						else
						if(ssi==2*sk-1 && sj==2*sk && sk!=1)//T(2k-1,2k)
						{
							sglue[ssi][sj][2] = ++sbig;
							stemp = sglue[ssi][sj][2];
							sstrength[stemp] = 2;
						}
						else
						if(ssi==1)
						{
							sglue[ssi][sj][2] = ss;
							stemp = sglue[ssi][sj][2];
							sstrength[stemp] = 1;
						}
						else
						{
							sglue[ssi][sj][2] = ++sbig;
							stemp = sglue[ssi][sj][2];
							sstrength[stemp] = 1;
						}
					}
				}//end of for
				
				//for filling the east side
				sbig2 = sbig;
				
				for(sj=1;sj<=2*sk;sj++)// filling in the east side
				{
					for(ssi=1;ssi<=2*sk;ssi++)
					{
						if(ssi==1 && sj%2==0 )//for T(1,2i) east condition
						{
							sglue[ssi][sj][1] = 0;
							//temp = glue[i][j][1];
							//strength[temp] = 0;
						}
						else
						if(ssi==sj && ssi%2==0 )//T(2i,2i)
						{
							sglue[ssi][sj][1] = ++sbig;
							stemp = sglue[ssi][sj][1];
							sstrength[stemp] = 2;
						}
						else
						if(ssi==(2*sk)-2 && sj==2*sk && sk != 1)//T(2k-2,2k)
						{
							sglue[ssi][sj][1] = 0;
							//temp = glue[i][j][1];
							//strength[temp] = 0;
						}
						else
						if(sj==1)
						{
							sglue[ssi][sj][1] = se;
							stemp = sglue[ssi][sj][1];
							sstrength[stemp] = 1;
						}
						else
						{
							sglue[ssi][sj][1] = ++sbig;
							stemp = sglue[ssi][sj][1];
							sstrength[stemp] = 1;
						}
					}
				}//end of for
				
				sbig = sbig2;
				
				for(sj=1;sj<=2*sk;sj++)// filling in the west side
				{
					for(ssi=1;ssi<=2*sk;ssi++)
					{
						if(ssi==1 && sj%2==1)//for T(1,2i-1) west condition
						{
							sglue[ssi][sj][3] = 0;
							//temp = glue[i][j][3];
							//strength[temp] = 0;
						}
						else
						if(ssi%2==0 && sj==ssi-1 )//for T(2i,2i-1)
						{
							sglue[ssi][sj][3] = ++sbig;
							stemp = sglue[ssi][sj][3];
							sstrength[stemp] = 2;
						}
						else
						if(ssi==2*sk-2 && sj==2*sk-1 && sk != 1)//T(2k-2,2k-1)
						{
							sglue[ssi][sj][3] = 0;
							//temp = glue[i][j][3];
							//strength[temp] = 0;
						}
						else
						if(sj==2*sk)
						{
							sglue[ssi][sj][3] = sw;
							stemp = sglue[ssi][sj][3];
							sstrength[stemp] = 1;
						}
						else
						{
							sglue[ssi][sj][3] = ++sbig;
							stemp = sglue[ssi][sj][3];
							sstrength[stemp] = 1;
						}
					}
				}//end of for
				//for writting all the glues after breaking in snake_frame.txt
				for(ssi=1;ssi<= 2 * sk; ssi++)
					for(sj=1; sj <= 2 * sk; sj++)
						sgh4.format("%d\t%d\t%d\t%d\n",sglue[ssi][sj][0],sglue[ssi][sj][1],sglue[ssi][sj][2],sglue[ssi][sj][3]);
									
			//System.out.printf("\n value of big %d\n",sbig);						
			}
			sgh4.close();
			//System.out.printf("\n value of big %d\n",sbig);						
			
			while(sinput7.hasNext())
			{	
				stemp = sinput7.nextInt();
				if(stemp > snum_binding_types)
					snum_binding_types = stemp;
			}
			while(sinput8.hasNext())
			{	
				stemp = sinput8.nextInt();
				stemp = sinput8.nextInt();
				stemp = sinput8.nextInt();
				stemp = sinput8.nextInt();
				snum_tile_types = snum_tile_types + 1;
			}
			
			
			//now starting to make .tiles file
			
		String naag="";	
		 sgh5.format("tile edges matches {{N E S W}*}\n");
		 sgh5.format("num tile types=%d\n",snum_tile_types);
		 sgh5.format("num binding types=%d\ntile edges={\n",snum_binding_types);
		
		String timeds = Tilegen11.now();
		
		naag = naag.concat("/*This .tiles file was generated using XTile 1.1 ").concat("at ").concat(timeds);
		naag = naag.concat("\n").concat("developed by Anshul Chaurasia, Sudhanshu Dwivedi and Prateek Jain, DA-IICT,INDIA");
		naag = naag.concat("\n").concat("detailed documentation and tool available at www.guptalab.org/xtile");
		naag = naag.concat("\n").concat("this .tiles file is input to Xgrow  developed by Eric Winfree, Caltech ").concat("*/");
		naag = naag.concat("\n");
		 
		 
		 
		 naag = naag.concat("tile edges matches {{N E S W}*}\n");
		 naag = naag.concat("num tile types=").concat(String.valueOf(snum_tile_types)).concat("\n");
		 naag = naag.concat("num binding types=").concat(String.valueOf(snum_binding_types)).concat("\n").concat("tile edges={\n");
		 
		 //int p,q,r,s;
		 while(sinput9.hasNext())
		 {
		 	sn=sinput9.nextInt();
		 	se=sinput9.nextInt();
		 	ss=sinput9.nextInt();
		 	sw=sinput9.nextInt();
		 	sgh5.format("{%d\t%d\t%d\t%d}\n",sn,se,ss,sw);
		 	naag = naag.concat("{").concat(String.valueOf(sn)).concat("\t").concat(String.valueOf(se)).concat("\t").concat(String.valueOf(ss)).concat("\t").concat(String.valueOf(sw)).concat("}\n");
		 }
		 sgh5.format("}\n");
		 sgh5.format("binding strengths=\n{");
		
		naag = naag.concat("}\n");
		 naag = naag.concat("binding strengths=\n{");
			
			for(ssi = 1; ssi <= snum_binding_types; ssi++)
			{
				sgh5.format("%d\t",sstrength[ssi]);
				
				naag = naag.concat(String.valueOf(sstrength[ssi])).concat("\t");
			}
			
			/*for(int d1=0; d1< glue_strength_2; d1++)
			{
				output1.format("2\t");
			}*/	 
			sgh5.format("}");
			naag = naag.concat("}");
		 sgh5.close();
		snake.setText(naag + newline);	
			
		}
		
	  catch ( SecurityException securityException )
      {
         System.err.println(
            "You do not have write access to this file." );
         System.exit( 1 );
      } // end catch
      catch ( FileNotFoundException filesNotFoundException )
      {
         System.err.println( "Error creating file." );
         System.exit( 1 );
      } // end catch
      catch ( FormatterClosedException formatterClosedException )
      {
            System.err.println( "Error writing to file." );
            return;
      } // end catch
      catch ( NoSuchElementException elementException )
      {
            System.err.println( "Invalid input. Please try again." );
            sinput6.nextLine(); // discard input so user can try again
      } // end catch
			
        	
        	
        	
        	
        repaint();	
        }	
        	
        	    	
            }	
            
            
          } 
          
          
         }
        
    public 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;
         
         }  
       
}
