Exemplo n.º 1
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);
 }
Exemplo n.º 2
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);
          }
        }
      }
    }
  }