
/**
 * Title:        <p>
 * Description:  <p>
 * Copyright:    Copyright (c) <p>
 * Company:      <p>
 * @author
 * @version 1.0
 */

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


public class NameGeneratingApplet extends java.applet.Applet
{
  String coolname = "Push the Button";
  NameGenerator ng;
  TextField tf;

  public void init()
  {
    ng = new NameGenerator();
    setLayout(new BorderLayout());
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    setLayout(gridbag);
    constraints.fill = GridBagConstraints.NONE;

    tf = new TextField(coolname, 25);
    tf.setEditable(false);
    buildConstraints(constraints, 0, 0, 1, 1, 90, 90);
    gridbag.setConstraints(tf, constraints);
    add(tf);

    Button again = new Button("Generate New Name");
    buildConstraints(constraints, 0, 1, 2, 1, 10, 10);
    gridbag.setConstraints(again, constraints);
    add(again);

  }

  public void start()
  {

  }

  public boolean action(Event evt, Object arg)
  {

      coolname = ng.GenerateName();
      tf.setText(coolname);
      return true;
  }

  void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy)
  {
    gbc.gridx = gx;
    gbc.gridy = gy;
    gbc.gridwidth = gw;
    gbc.gridheight = gh;
    gbc.weightx = wx;
    gbc.weighty = wy;
  }


}

class NameGenerator
{

  int num_firstwords;
  String[] firstwords;
  int num_secondwords;
  String[] secondwords;
  int num_thirdwords;
  String[] thirdwords;
  URL where;
  URLConnection conn = null;
  Input in = null;
  boolean connected = true;

  public NameGenerator()
  {

    String url = "http://www.afn.org/~afn07474/GameNamesBig.in";
    try
    {
      where = new URL(url);
    }
    catch(Exception e){connected = false;}

    try
    {
      conn = where.openConnection();
      conn.connect();
      in = new Input(new BufferedInputStream(conn.getInputStream()));
    }
    catch(Exception e){connected = false;}

    /* Input format:

          for each set of words
            first int = number of words
            (first int) strings = words
          end
    */

    if (connected)
    {
      num_firstwords = in.readInt();
      firstwords = new String[num_firstwords];
      for (int i = 0; i < num_firstwords; i++)
      {
         firstwords[i] = in.readLine();
      }
      num_secondwords = in.readInt();
      secondwords = new String[num_secondwords];
      for (int i = 0; i < num_secondwords; i++)
      {
         secondwords[i] = in.readLine();
      }
      num_thirdwords = in.readInt();
      thirdwords = new String[num_thirdwords];
      for (int i = 0; i < num_thirdwords; i++)
      {
         thirdwords[i] = in.readLine();
      }
    }
}

  public String GenerateName()
  {
    if (connected)
    {
      int first_num = (int)Math.floor(num_firstwords*Math.random());
      String first = firstwords[first_num];
      int second_num = (int)Math.floor(num_secondwords*Math.random());
      while(first.charAt(0) == '*' && first_num == second_num)
      {
        second_num = (int)Math.floor(num_secondwords*Math.random());
      }
      String second = secondwords[second_num];
      int third_num = (int)Math.floor(num_thirdwords*Math.random());
      while((first.charAt(0) == '*' && first_num == third_num)
           || (second.charAt(0) == '*' && second_num == third_num))
      {
        third_num = (int)Math.floor(num_thirdwords*Math.random());
      }
      if (first.charAt(0) == '*') first = first.substring(1, first.length());
      if (second.charAt(0) == '*') second = second.substring(1, second.length());
      String third = thirdwords[third_num];
      if (third.charAt(0) == '*') third = third.substring(1, third.length());
      return ( first + " " + second + " " + third);
    }
    else return "not connected";
  }
}


class Input
   {
      BufferedInputStream bis;

      Input(BufferedInputStream my)
      {
        bis = my;
      }


      Input(String file)
      {
         try{
            bis = new BufferedInputStream(new FileInputStream(file));
            }
            catch(Exception e){}
      }

      Input()
      {
         bis = new BufferedInputStream(System.in);
      }

      public String readString()
      {
         char read_in = ' ';
         String ret = "";
      /* Skip white space */

         do
         {
            try{
               read_in = (char) bis.read();
            }
               catch(Exception e){
               }
         }
         while (Character.isWhitespace(read_in));

         while (!Character.isWhitespace(read_in) && Character.isDefined(read_in))
         {
            ret = ret + read_in;
            try{
               read_in = (char) bis.read();
            }
               catch(Exception e){
               }
         }
         return ret;
      }
      public String readLine()
      {
         char read_in = ' ';
         String ret = "";
      /* Skip white space */

         do
         {
            try{
               read_in = (char) bis.read();
            }
               catch(Exception e){
               }
         }
         while (Character.isWhitespace(read_in) && read_in != ' ');

         while (!(Character.isWhitespace(read_in) && read_in != ' ')
               && Character.isDefined(read_in))
         {
            ret = ret + read_in;
            try{
               read_in = (char) bis.read();
            }
               catch(Exception e){
               }
         }
         return ret;
      }


      public int readInt()
      {
         char read_in = ' ';
         String ret = "";

         do
         {
            try{
               read_in = (char) bis.read();
            }
               catch(Exception e){
               }
         }
         while (Character.isWhitespace(read_in));

         while (!Character.isWhitespace(read_in) && Character.isDefined(read_in)
               && Character.isDigit(read_in))
         {
            ret = ret + read_in;
            try{
               read_in = (char) bis.read();
            }
               catch(Exception e){
               }
         }
         return Integer.parseInt(ret);
      }

      public double readDouble()
      {
         char read_in = ' ';
         String ret = "";

         do
         {
            try{
               read_in = (char) bis.read();
            }
               catch(Exception e){
               }
         }
         while (Character.isWhitespace(read_in));

         while (!Character.isWhitespace(read_in) && Character.isDefined(read_in)
               && (Character.isDigit(read_in) || read_in == '.'))
         {
            ret = ret + read_in;
            try{
               read_in = (char) bis.read();
            }
               catch(Exception e){
               }
         }
         return Double.parseDouble(ret);
      }
   }

