import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.*;

public class queenmaster
{
   public static void main(String args[]) throws IOException
   {
      int job_id = 1;
      int task_id = 100;

      int i, j, k, l;

      int main_pno;
      String main_pt = "p1";
      int gp_no;
      String gp_name;
      int pt_no;
      String  pt_name;

      int nl;

      JMPF def_obj = new JMPF(job_id, task_id);
      System.out.println("The hostname is " + def_obj.hostname);
      System.out.println("The host id is " + def_obj.host_id);

      gp_name = "gp1";
      gp_no = def_obj.CreatePortGroup(gp_name);

      main_pno = def_obj.CreatePort(main_pt);

      System.out.println("Enter the no. of queens : ");
      int n = System.in.read() - 48;
      nl = System.in.read();

      int no = 1;
      for (i = 1; i < n-1; i++)
         no = no + no * i;

      int status = def_obj.SyncsendGroup(gp_name, n);

      int countj, countk, countl;

      for (i = 1; i <= n; i++)
      {
       PrintStream ps = def_obj.CreatePS(def_obj.LocatePort(String.valueOf(i)));
         int i1 = i + 1;
         if (i1 > n)
            i1 = i1 % n;
         for (j = i1, countj = 1; countj <= n; j++, countj++)
         {
            int j1 = j + 1;
            if (j1 > n)
               j1 = j1 % n;
            if (j > n)
               j = j % n;
            if (j != i)
            {
               for (k = j1, countk = 1; countk <= n; k++, countk++)
               {
                  int k1 = k + 1;
                  if (k1 > n)
                     k1 = k1 % n;
                  if (k > n)
                     k = k % n;
                  if ((k != i) && (k != j)) 
                  {
                     for (l = k1, countl = 1; countl <= n; l++, countl++)
                     {
                        if (l > n)
                           l = l % n;
                        if ((l != i) && (l != j) && (l != k))
                        {
                           String str = i + " " + j + " " + k + " " + l;
                           System.out.println("String is " + str);
                           def_obj.Send(ps, str);
                        }
                     }
                  }
               }
            }
         }
      }

      Vector vector = new Vector(1,1);
      String str;
      for (i = 1; i <= n; i++)
      {
         for (j = 1; j <= no; j++)
         {
            str = def_obj.ReceiveString(main_pno);
            vector.addElement(str);
            System.out.println("The received string is : " + str);
         }
      }

      main_pt = "p2";
      main_pno = def_obj.CreatePort(main_pt);
      String gui_name = "gui";
      int gui_no = def_obj.LocatePort(gui_name, true);
      while (true)
      {
         String s = def_obj.ReceiveString(main_pno);
         System.out.println("The status of continuing is : " + s);
         if ("return".equals(s))
            continue;
         else if ("close".equals(s))
            break;
         int choice = def_obj.ReceiveInt(main_pno);
         System.out.println("The choice is ...." + choice);
         Vector vector2 = new Vector(1,1);
         int num;
         StringTokenizer st;
         int count = vector.size();
         System.out.println("The count is ... " + count);
         int inc = 0;

         for (i = 0; i < count; i++)
         {
            st = new StringTokenizer((String)vector.elementAt(inc));
            num = Integer.parseInt(st.nextToken());
            if (num == choice)
               vector2.addElement((String)vector.elementAt(inc));
            inc = inc + 1;
         }

         for (i = 0; i < vector2.size(); i++)
         {
            status = def_obj.Send(gui_no, (String)vector2.elementAt(i));
            System.out.println("Status of sending values is : " + status);
         }
         status = def_obj.Send(gui_no, "false");
      }
      def_obj.ClosePorts();
   }
}

