import java.util.*;
import java.awt.*;
import java.io.*;
import java.net.*;

class QuizPair {
  int myMaxSize=500;

  public QuizPair(Point p, URL url) {
    mySize=p;
    int i,j;
    len=mySize.x*mySize.y;
    found=0;
//    loadURL("file:/d:/Jeff's Files/java/conc/jeff.txt");
    loadURL(url.toString());

    matched=new boolean[len];
    index=new int[len];
    int numPairs=len/2;

    // create necessary numbers
//    myFileLen=len*2;
    boolean[] chosen=new boolean[myFileLen/2];

    for(i=0;i<myFileLen/2;i++) {
//      myFData[i*2]=new PString(Integer.toString(i));
//      myFData[i*2+1]=new PString(Integer.toString(i));
      chosen[i]=false;
    }

    for(i=0;i<len;i++) {
      matched[i]=false;
    }

    // randomly select pairs from those available
    int[] cPairs=new int[numPairs];
    Random r=new Random();
    for(i=0;i<numPairs;i++) {
      j=Math.abs(r.nextInt())%(myFileLen/2);
      while(chosen[j]) {
        j++;
        if(j>=numPairs)
          j=0;
      }
      chosen[j]=true;
      cPairs[i]=j;
    }

    // get chosen data
    myData=new PObject[len];
    for(i=0;i<numPairs;i++) {   
      myData[i*2]=myFData[cPairs[i]*2];
      myData[i*2+1]=myFData[cPairs[i]*2+1];
    }

    // randomly place chosen data
    chosen=new boolean[len];
    for(i=0;i<len;i++) {
      chosen[i]=false;
    }
    for(i=0;i<len;i++) {
      j=Math.abs(r.nextInt())%len;
      while(chosen[j]) {
        j++;
        if(j>=len)
          j=0;
      }
      chosen[j]=true;
      index[i]=j;
    }

    // handle odd number of entries
    if(len%2==1) {
      extra=len-1;
      myData[extra]=new PString("Freebie!!!");
    }
    else
      extra=len;

for(i=0;i<len;i++) {
  System.out.println(i+":"+myData[i]+":"+index[i]);
}
  }

  public void get(Point p, Point mousePoint) {
    myData[index[mySize.x*p.y+p.x]].show(mousePoint);
  }

  public boolean check(Point p1, Point p2) {
    int i1=mySize.x*p1.y+p1.x;
    int i2=mySize.x*p2.y+p2.x;

    int j1=index[i1];
    int j2=index[i2];

    // has it already been matched?
    if(matched[i1] || matched[i2])
      return false;

    // handle the freebie
    if(j1==extra) {
      matched[i1]=true;
      found++;
      return true;
    }
    else if(j2==extra) {
      matched[i2]=true;
      found++;
      return true;
    }

    // check for a match
    if(j1>j2) {
      int j3=j1;
      j1=j2;
      j2=j3;
    }

    if(j1%2==0 && j1+1==j2) {
      matched[i1]=true;
      matched[i2]=true;
      found+=2;
      return true;
    }
    else
      return false;
  }

  public boolean isMatched(Point p) {
    return matched[mySize.x*p.y+p.x];
  }

  public boolean isDone() {
    return found==len;
  }

  public PObject getObject(String s) {
    String token, value;
    int first,mid,last;

    first=s.indexOf('<');
    mid=s.indexOf(':');
    last=s.indexOf('>');

    if(first<mid && mid<last && first>-1) {
      token=s.substring(first+1,mid);
      value=s.substring(mid+1,last);

      if(token.equals("SND"))
        s="Sound:"+value;
      else if(token.equals("BMP"))
        s="Picture:"+value;
    }

    return new PString(s);
  };

  private void loadURL(String sURL) {
    try {
    URL u=new URL(sURL);
//    URL u=new URL("http://www.sybase.com");
      
      BufferedReader br=new BufferedReader(new InputStreamReader(u.openStream()));
      String s;
      PObject p;
      myFData=new PObject[myMaxSize];
      myFileLen=0;

      while((s=br.readLine())!=null) {
        myFData[myFileLen]=getObject(s);
        myFileLen++;

        if((s=br.readLine())!=null) {
          myFData[myFileLen]=getObject(s);
          myFileLen++;
        }

        if((s=br.readLine())!=null) {
        }
      }
      br.close();
    }
    catch(Exception e) {
      System.out.println("Sorry!");
      System.out.println(e);
    }
  }

  PObject[] myData;
  int[] index;
  boolean [] matched;
  int extra;
  int len;
  int found;
  Point mySize;
  int myFileLen;
  PObject[] myFData;
}
