Example #1
0
  static void reqChangeProperty(Client c) throws IOException {
    int foo;
    byte mode;
    int prpty;
    byte frmt;
    int typ;
    int n;
    IO io = c.client;

    mode = (byte) c.data;
    if ((mode != PropModeReplace) && (mode != PropModeAppend) && (mode != PropModePrepend)) {
      // System.err.println("error!!");
      c.errorValue = mode;
      c.errorReason = 2; // BadValue;
    }

    n = c.length;
    ;
    foo = io.readInt();
    Window w = c.lookupWindow(foo);
    if (c.errorReason == 0 && w == null) {
      c.errorValue = foo;
      c.errorReason = 3; // BadWindow;
    }
    prpty = io.readInt();
    typ = io.readInt();
    frmt = (byte) io.readByte();
    if (c.errorReason == 0 && (frmt != 8) && (frmt != 16) && (frmt != 32)) {
      c.errorValue = frmt;
      c.errorReason = 3; // BadWindow;
    }
    io.readPad(3);
    foo = io.readInt();
    int totalSize = foo * (frmt / 8);
    byte[] bar = null;
    if (totalSize > 0) {
      bar = new byte[totalSize];
      io.readByte(bar, 0, totalSize);
      if (c.swap) {
        switch (frmt) {
          case 16:
            swapS(bar, 0, totalSize);
            break;
          case 32:
            swapL(bar, 0, totalSize);
            break;
          default:
        }
      }
      io.readPad((-totalSize) & 3);
    }
    c.length = 0;
    if (c.errorReason != 0) {
      return;
    }
    changeWindowProperty(c, w, prpty, typ, frmt, mode, foo, bar, true);
  }
Example #2
0
  static void reqSetSelectionOwner(Client c) throws IOException {
    int foo;
    int selection;
    IO io = c.client;
    foo = io.readInt();
    c.length -= 2;
    Window w = null;
    if (foo != 0) {
      w = c.lookupWindow(foo);
      if (w == null) {
        c.errorValue = foo;
        c.errorReason = 3; // BadWindow
        return;
      }
    }

    selection = io.readInt();
    foo = io.readInt();
    c.length -= 2;
    int time = 0;
    time = (int) System.currentTimeMillis();
    time = foo; // ??

    if (Atom.valid(selection)) {
      int i = 0;
      Selection s = getSelection(selection);
      if (s != null) {
        if (s.client != null && (w == null || (s.client != c))) {
          if (s.client != null) {
            c.cevent.mkSelectionClear(time, s.wid, s.selection);
            s.client.sendEvent(c.cevent, 1, Event.NoEventMask, Event.NoEventMask, null);
          }
        }
        s.window = w;
        s.wid = (w != null ? w.id : 0);
        s.lastTimeChanged = time;
        s.client = (w != null ? c : null);
      } else {
        // System.out.println("add");
        addSelection(selection, time, w, c);
      }
      return;
    } else {
      c.errorValue = selection;
      c.errorReason = 5;
      return;
    }
  }
Example #3
0
  static void reqListProperties(Client c) throws IOException {
    int foo, n;
    IO io = c.client;

    foo = io.readInt();
    Window w = c.lookupWindow(foo);
    c.length -= 2;
    if (w == null) {
      c.errorValue = foo;
      c.errorReason = 3; // BadWindow;
      return;
    }
    synchronized (io) {
      io.writeByte(1);
      Property p = w.getProperty();
      int i = 0;
      while (p != null) {
        i++;
        p = p.next;
      }

      io.writePad(1);
      io.writeShort(c.seq);
      io.writeInt(i);
      io.writeShort(i);
      io.writePad(22);

      p = w.getProperty();
      while (p != null) {
        io.writeInt(p.propertyName);
        p = p.next;
      }
      io.flush();
    }
  }
Example #4
0
  static void reqGetSelectionOwner(Client c) throws IOException {
    int foo;
    int selection;
    IO io = c.client;
    selection = io.readInt();

    c.length -= 2;

    if (!Atom.valid(selection)) {
      c.errorValue = selection;
      c.errorReason = 5; // BadAtom
      return;
    }

    synchronized (io) {
      io.writeByte(1);
      Selection s = getSelection(selection);
      io.writePad(1);
      io.writeShort(c.seq);
      io.writeInt(0);
      if (s != null) {
        io.writeInt(s.wid);
      } else {
        io.writeInt(0);
      }
      io.writePad(20);
      io.flush();
    }
  }
Example #5
0
 static void reqDeleteProperty(Client c) throws IOException {
   int foo, propty;
   IO io = c.client;
   foo = io.readInt();
   Window w = c.lookupWindow(foo);
   if (w == null) {
     c.errorValue = foo;
     c.errorReason = 3; // BadWindow;
   }
   propty = foo = io.readInt();
   c.length -= 3;
   if (c.errorReason != 0) {
     return;
   }
   if (w.parent != null) {
     Property.delProperty(c, w, propty, 0);
   }
 }
 public static void main(String[] args) throws Exception {
   for (int t = 1, ts = IO.readInt(InfiniteHouseOfPancakes.in); t <= ts; t++) {
     InfiniteHouseOfPancakes.out.print("Case #");
     InfiniteHouseOfPancakes.out.print(t);
     InfiniteHouseOfPancakes.out.print(": ");
     new InfiniteHouseOfPancakes().solve();
   }
   InfiniteHouseOfPancakes.out.flush();
 }
Example #7
0
  static void reqConvertSelection(Client c) throws IOException {
    int foo;
    int selection, requestor, target, property, time;
    boolean paramsOkay = true;
    IO io = c.client;
    requestor = io.readInt();
    Window w = c.lookupWindow(requestor);
    if (w == null) {
      c.errorValue = requestor;
      c.errorReason = 3; // BadWindow;
    }
    selection = io.readInt();
    paramsOkay = Atom.valid(selection);
    target = io.readInt();
    paramsOkay &= Atom.valid(target);
    property = io.readInt();
    if (property != 0) {
      paramsOkay &= Atom.valid(property);
    }
    time = io.readInt();
    c.length -= 6;
    if (c.errorReason != 0) {
      return;
    }

    if (paramsOkay) {
      Selection s = getSelection(selection);
      if (s != null && s.client != null) {
        c.cevent.mkSelectionRequest(time, s.wid, requestor, selection, target, property);
        if (s.client.sendEvent(c.cevent, 1, Event.NoEventMask, Event.NoEventMask, null) != 0)
          return;
      }
      c.cevent.mkSelectionNotify(time, requestor, selection, target, 0);
      c.sendEvent(c.cevent, 1, Event.NoEventMask, Event.NoEventMask, null);

      return;
    } else {
      // System.out.println("error!!");
      c.errorValue = property;
      c.errorReason = 5; // BadAtom
      return;
    }
  }
 public int read() {
   IO io = new IO();
   int value;
   boolean ok;
   do {
     value = io.readInt(title);
     ok = limits.includes(value);
     if (!ok) {
       io.writeln("ERROR!!! La opci�n debe ser entre 1 y 9" + limits + " incluidos");
     }
   } while (!ok);
   return value;
 }
Example #9
0
 public int read() {
   IO io = new IO();
   int value;
   boolean ok;
   do {
     value = io.readInt(title);
     ok = limits.includes(value);
     if (!ok) {
       io.writeln("El valor debe estar entre " + limits);
     }
   } while (!ok);
   return value;
 }
Example #10
0
 public static void main(String[] args) {
   System.out.println("please enter size of grid.");
   int n = IO.readInt();
   if (n < 0) {
     IO.reportBadInput();
     return;
   }
   char[][] tissue = new char[n][n];
   System.out.println("Please enter threshold value in percent form (between 0 -100)");
   int threshold = IO.readInt();
   System.out.println("Please enter number of max rounds.");
   int maxRounds = IO.readInt();
   System.out.println(
       "Please enter number of times the board should be printed int x rounds. (Fresquencey of printing. i splled frequency wrong.");
   int frequency = IO.readInt();
   System.out.println("Please enter percent blank");
   int percentBlank = IO.readInt();
   System.out.println("Pease enter percentX");
   int percentX = IO.readInt();
   if (percentBlank < 0
       || percentBlank > 100
       || percentX < 0
       || percentX > 100
       || threshold < 0
       || threshold > 100
       || n < 0
       || maxRounds <= 0
       || frequency < 0) {
     IO.reportBadInput();
     return;
   }
   assignCellTypes(tissue, percentBlank, percentX);
   CellSimGUI hi = new CellSimGUI(n, 1);
   System.out.println("Initial board is: ");
   printTissue(tissue, hi, n, threshold);
   System.out.println("");
   int x = 0;
   int i = 0;
   int a = 0;
   int max = 1;
   int rounds = 0;
   double percentUnsatisfied = 0;
   int counter = 0;
   double satisfied = 0;
   while (boardSatisfied(tissue, threshold) == false) {
     counter = moveAllUnsatisfied(tissue, threshold) + counter;
     x++;
     if (x % frequency == 0) {
       printTissue(tissue, hi, n, threshold);
       System.out.println("");
       if (boardSatisfied(tissue, threshold) == false) {
         System.out.println("Board is not satisfied");
       } else System.out.println("Board is satisfied.");
     }
     if (max == maxRounds) {
       System.out.println("Board cannot be satisfied within max rounds givent.");
       for (i = 0; i < tissue.length; i++) {
         for (a = 0; a < tissue.length; a++) {
           if (isSatisfied(tissue, i, a, threshold) == false) percentUnsatisfied++;
           else satisfied++;
         }
       }
       percentUnsatisfied = (satisfied / (percentUnsatisfied + satisfied));
       System.out.println(
           "This is the percent of satisfied cells : " + (percentUnsatisfied * 100) + "%");
       System.out.println("THis is the number of movements that occured: " + counter);
       return;
     }
     max++;
     rounds++;
   }
   System.out.println("This took " + rounds + " number of rounds");
   System.out.println("THis is the number of movements that occured: " + counter);
   System.out.println("Baord is satisfied. Final board is:");
   printTissue(tissue, hi, n, threshold);
 }
Example #11
0
  static void reqGetProperty(Client c) throws IOException {
    int foo;
    int dlt;
    int prprty;
    int typ;
    int ffst, lngth;
    IO io = c.client;
    dlt = c.data;

    foo = io.readInt();
    Window w = c.lookupWindow(foo);
    if (w == null) {
      c.errorValue = foo;
      c.errorReason = 3; // BadWindow;
    }
    prprty = io.readInt();
    typ = io.readInt();
    ffst = io.readInt();
    lngth = io.readInt();
    c.length -= 6;
    if (c.errorReason != 0) {
      return;
    }
    int frmt;
    int seq;
    int ba;
    Property prop;
    prop = Property.getProperty(w, prprty, typ);

    synchronized (io) {
      io.writeByte(1);
      if (prop == null) {
        io.writeByte(0);
        io.writeShort(c.seq);
        io.writeInt(0);
        io.writeInt(0);
        io.writeInt(0);
        io.writeInt(0);
        io.writePad(12);
        io.flush();
        return;
      }

      if ((typ != prop.type) && typ != 0) {
        io.writeByte(prop.format);
        io.writeShort(c.seq);
        io.writeInt(0);
        io.writeInt(prop.type);
        io.writeInt(0);
        io.writeInt(0);
        io.writePad(12);
        io.flush();
        return;
      }

      int n = (prop.format / 8) * prop.size;

      int ind = ffst * 4;
      if (n < ind) {
        // System.err.println("procGetProperty: ind-n="+new Integer((ind-n)));
      }

      int len = ((n - ind) < lngth * 4) ? n - ind : lngth * 4;
      if ((lngth * 4) < 0) len = n - ind;
      ba = n - (ind + len);
      if (dlt != 0 && ba == 0) {
        c.cevent.mkPropertyNotify(w.id, prprty, (int) System.currentTimeMillis(), 1);
        w.sendEvent(c.cevent, 1, null);
      }

      io.writeByte(prop.format);
      io.writeShort(c.seq);
      io.writeInt((len + 3) / 4);
      io.writeInt(prop.type);
      io.writeInt(ba);
      if ((prop.format / 8) == 0) io.writeInt(0);
      else io.writeInt(len / (prop.format / 8));

      io.writePad(12);

      if (len > 0) {
        if (c.swap && (prop.format == 16 || prop.format == 32)) {
          byte[] b = new byte[len];
          System.arraycopy(prop.data, ind, b, 0, len);
          switch (prop.format) {
            case 16:
              swapS(b, 0, len);
              break;
            case 32:
              swapL(b, 0, len);
              break;
            default:
          }
          io.writeByte(b, 0, len);
        } else {
          io.writeByte(prop.data, ind, len);
        }
        if (((-len) & 3) > 0) {
          io.writePad((-len) & 3);
        }
      }
      io.flush();
    }

    if (dlt != 0 && ba == 0) {
      Property.delProperty(c, w, prprty, typ);
    }
  }
Example #12
0
  static void reqRotateProperties(Client c) throws IOException {
    int foo, propty;
    IO io = c.client;

    foo = io.readInt();
    Window w = c.lookupWindow(foo);
    c.length -= 2;
    if (w == null) {
      c.errorValue = foo;
      c.errorReason = 3; // BadWindow;
      return;
    }
    int n = (short) io.readShort();
    int delta = (short) io.readShort();

    c.length--;

    if (n == 0) {
      return;
    }

    int[] atoms = new int[n];
    Property[] props = new Property[n];
    Property p;
    int i = 0;
    while (n != 0) {
      atoms[i] = io.readInt();
      c.length--;
      if (!Atom.valid(atoms[i])) {
        c.errorValue = atoms[i];
        c.errorReason = 5; // BadAtom
        return;
      }
      p = w.getProperty();
      while (p != null) {
        if (p.propertyName == atoms[i]) {
          props[i] = p;
          break;
        }
        p = p.next;
      }
      if (p == null) {
        c.errorReason = 8; // BadMatch
        return;
      }
      i++;
      n--;
    }
    for (int j = 0; j < atoms.length; j++) {
      for (int k = j + 1; k < atoms.length; k++) {
        if (atoms[j] == atoms[k]) {
          c.errorReason = 8; // BadMatch
          return;
        }
      }
    }
    if (((delta < 0 ? -1 * delta : delta) % atoms.length) != 0) {
      while (delta < 0) {
        delta += atoms.length;
      }
      for (i = 0; i < atoms.length; i++) {
        c.cevent.mkPropertyNotify(
            w.id,
            props[i].propertyName,
            (int) System.currentTimeMillis(),
            // Property.PropertyNewValue
            0);
        w.sendEvent(c.cevent, 1, null);
        props[i].propertyName = atoms[(i + delta) % atoms.length];
      }
    }
  }
Example #13
0
  /** @param args */
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    boolean play = true;
    String pname;
    cat c;
    System.out.println("What is your name?");
    String name = IO.readString();
    player p = new player(name);
    store shop = new store();

    while (play) {

      System.out.println("What kind of pet do you want?");
      System.out.println("Menu:");
      System.out.println("1. Cat");
      System.out.println("2. Horse");
      System.out.println("3. Dog");

      int species = IO.readInt();
      if (species == 1) {
        System.out.println("You have a new cat!");
        System.out.println("What is your pet's name?");
        pname = IO.readString();
        c = new cat(pname);

      } else if (species == 2) {
        c = new cat("fluffy");
        pet test = new pet(2, "fluffy");
      } else {
        System.out.println("Sorry, only cats are available right now. You have a new cat.");
        System.out.println("What is your pet's name?");
        pname = IO.readString();
        c = new cat(pname);
      }

      while (c.dead == false) {
        // menu
        int choice = -1;
        while (choice < 0 || choice > 8) {
          System.out.println("------");
          System.out.println("Menu:");
          System.out.println("------");
          System.out.println("1. Feed");
          System.out.println("2. Pet");
          System.out.println("3. Bath");
          System.out.println("4. Play");
          System.out.println("5. See Stats");
          System.out.println("6. See Inventory");
          System.out.println("7. Store");

          choice = IO.readInt();
          if (choice == 1) {
            p.feed(c);
          }
          if (choice == 2) {
            p.pet(c);
          }
          if (choice == 3) {
            p.bath(c);
          }
          if (choice == 4) {
            p.giveToy(c);
          }
          if (choice == 5) {
            c.get_stats();
          }
          if (choice == 6) {
            p.printInventory();
          }
          if (choice == 7) {
            shop.accessStore(p, c);
          } else {
            System.out.println("Please choose an option from the menu.");
          }
        }

        random_events event = new random_events(c, p);
      }

      System.out.println("Do you want another pet?");
      play = IO.readBoolean();
    }
  }
Example #14
0
  public static void main(String[] args) {

    System.out.println("\n" + "Hey there, how are you doing?");
    System.out.println("\n" + "Do you want to tell me your grades? (Up to 5)");
    String start = IO.readString();
    if (start.length() < 1 || start.charAt(0) != 'y' && start.charAt(0) != 'Y') {
      System.out.println("\n" + "Okay then. Talk to you another time.");
      return;
    }

    double[] grades = new double[] {0, 0, 0, 0, 0};
    double[] credits = new double[] {0, 0, 0, 0, 0};

    int counter = 1;
    while (counter < 6) {
      System.out.print("\n" + "Tell me grade " + counter + ":  ");
      while (true) {

        start = IO.readString();

        if (start.equals("A")) {
          grades[counter - 1] = 4.0;
          break;
        }
        if (start.equals("B+")) {
          grades[counter - 1] = 3.5;
          break;
        }
        if (start.equals("B")) {
          grades[counter - 1] = 3.0;
          break;
        }
        if (start.equals("C+")) {
          grades[counter - 1] = 2.5;
          break;
        }
        if (start.equals("C")) {
          grades[counter - 1] = 2.0;
          break;
        }
        if (start.equals("D")) {
          grades[counter - 1] = 1.0;
          break;
        }
        if (start.equals("F")) {
          grades[counter - 1] = 0.0;
          break;
        }
        System.out.print(
            "\n" + "Sorry that is not valid input. Please make sure you use capital letters.");
      }
      System.out.print("\n" + "And how many credits was that grade worth?  ");
      credits[counter - 1] = IO.readInt();

      System.out.println("\n" + "\n" + "Do you have any more grades to tell me about?");
      start = IO.readString();
      if (start.length() < 1 || start.charAt(0) != 'y' || start.charAt(0) != 'Y') break;
      counter++;
      System.out.println("\n" + "Here is the value of counter: " + counter);
    }

    double gpa = 0.0;
    double credit_sum = 0.0;
    double grade_sum = 0.0;

    for (counter = 0; counter < 5; counter++) {
      credit_sum += credits[counter];
      grade_sum += (grades[counter] * credits[counter]);
    }
    gpa = grade_sum / credit_sum;
    System.out.println("\n" + "Thank you for that information. Now here is your GPA:  " + gpa);
    return;
  }
Example #15
0
  // Connects to server, starts a NetThread, then waits for messages.
  public static void main(String[] args) {

    int port = -1;
    String host = null;

    if (args.length >= 3) {

      name = args[0];
      host = args[1];
      try {
        port = Integer.parseInt(args[2]);
      } catch (Exception e) {
        port = -1;
      }
    }

    if (port == (-1)) {

      io.p("What server would you like to connect to? ");
      host = io.readLine();

      io.p("What port would you like to connect on? ");
      port = io.readInt();

      if ((port < 1) || (port > 65535)) {
        io.pl("Invalid port number.");
        System.exit(1);
      }
    }

    io.pl("Connecting to " + host + ":" + port + "...");

    Socket s = null;

    try {
      s = new Socket(host, port);
    } catch (Exception e) {
      io.pl("Couldn't connect to that server.");
      UIWindow.alert(null, "Couldn't connect to that server.");
      System.exit(1);
    }

    server = new NetThread(s, al, -1);

    io.pl("Connected to server.");

    Message nameMessage = new Message(Message.SET_NAME, name, null);
    server.sendMessage(nameMessage);

    while (true) {

      synchronized (al) {
        if (haveMessages()) {

          Message m = null;

          m = al.get(0);
          al.remove(0);

          handleMessage(m);

        } else {

          try {
            al.wait();
          } catch (Exception e) {
            io.pl("An exception occurred while trying to wait for messages from the server.");
            UIWindow.alert(null, "AI Client error occurred.");
            System.exit(1);
          }
        }
      }
    }
  }