public boolean hasNext() {
   if (current.getNext() == null) {
     return false;
   } else {
     return true;
   }
 }
 public T next() {
   if (hasNext()) {
     current = current.getNext();
     return current.getData();
   } else {
     throw new NoSuchElementException();
   }
 }
Beispiel #3
0
  private static void serverCall() throws Exception {
    String sentence = "move";
    String line;

    while (!gameStopped) {
      System.out.print("");
    }
    playing = false;
    try {
      setDiscussion();
    } catch (Exception ex) {
      System.out.println("Failed to discuss with clients");
    }

    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
    try {
      InetAddress inetAddress = InetAddress.getByName(serverInfo.getNext().getIpAddr());
      Socket clientSocket = new Socket(inetAddress, serverInfo.getNext().getPort());
      DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
      BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
      sendInfo(out);
      line = in.readLine();
      while (!game.getUnbinding()) ;
      Naming.unbind("rmi://" + serverInfo.getIpAddr() + ":1099/I_InfoGame");
      out.writeBytes("Unbinding finished\n");
      line = in.readLine();
      clientSocket.close();
    } catch (RemoteException e) {
      System.out.println("Failed get info from game");
      System.exit(1);
    } catch (Exception ex) {
      System.out.println("Failed to socket with waiting server");
    }
    discussion.shutDown();
    discussion = null;
    game = null;
    System.gc();
    serverWaiting();
  }
Beispiel #4
0
 public static void serverInit() {
   String serverAddress = "rmi://" + serverInfo.getIpAddr() + ":1099/I_InfoGame";
   try {
     game = new InfoGame();
     server = new Server();
     game.setServer(server);
     game.setServerAddress(serverInfo.getIpAddr());
     game.setNextAddress(serverInfo.getNext().getIpAddr());
     game.setNbPlayerExpected(numPlayer);
     Naming.rebind(serverAddress, game);
     socketPortClient = serverInfo.getPortClient();
     // System.out.println("End initialisation");
   } catch (RemoteException e) {
     System.out.println("Hubo una excepcion creando la instancia del objeto distribuido");
   } catch (MalformedURLException e) {
     System.out.println("URL mal formada al tratar de publicar el objeto");
   }
 }
Beispiel #5
0
  public static void main(String[] args) {
    Node n1;
    n1 = new Node("Tommy");
    System.out.println(n1);
    Node n2 = new Node("Sammy");
    System.out.println(n2);
    n1.setNext(n2);
    System.out.println(n1.getNext());
    n2.setNext(new Node("Clyde"));
    System.out.println(n1.getNext().getNext());
    System.out.println(n2.getNext());
    /* example of removing a node
    n1.setNext(n2.getNext());
    // or n1.setNext(n1.getNext().getNext());
    System.out.println("After removing second node");
    System.out.println(n1);
    System.out.println(n1.getNext());
    System.out.println(n1.getNext().getNext());
    */

    Node last = n1.getNext().getNext();
    last.setNext(n1); // creates a loop by connecting last to first

    System.out.println("Looping through the list");
    System.out.println(n1.getNext());
    System.out.println(n1.getNext().getNext());
    System.out.println(n1.getNext().getNext().getNext());
    System.out.println(n1.getNext().getNext().getNext().getNext());

    System.out.println("Testing toString method");
    MyLinkedList m1 = new MyLinkedList();
    m1.add("A");
    m1.add("B");
    m1.add("C");
    System.out.println(m1);

    System.out.println("Testing add(int i, String s)");
    m1.add(1, "D");
    System.out.println(m1);
  }
Beispiel #6
0
  public static void main(String[] args) throws IOException {
    // list of possible colors
    String[] colorNames = {" ", "Red", "Yellow", "Blue", "Green"};
    // colors represented as integers
    int[] colorList = {0, 1, 2, 3, 4};

    // read in file
    if (args.length == 0) System.out.println("No file specified.");
    else {
      FileReader theFile;
      BufferedReader inFile;
      String oneLine;
      try {
        theFile = new FileReader(args[0]);
        inFile = new BufferedReader(theFile);
        // while not end of input
        while ((oneLine = inFile.readLine()) != null) {
          // build a linked list
          String numNodes[] = oneLine.split(" ");
          System.out.println("Number of nodes: " + numNodes[0]);
          int numOfNodes = Integer.parseInt(numNodes[0]);
          LinkedList v = new LinkedList();
          // add the number of vertex nodes indicated in the input file, all to one linked list
          for (int i = 0; i < numOfNodes; i++) {
            Node y = new Node();
            v.append(y);
          }
          // create adjacency table
          int[][] a = new int[numOfNodes][numOfNodes];
          // Read in the adjacency table
          for (int r = 0; r < numOfNodes; r++) {
            // read in each line of the input table
            oneLine = inFile.readLine();
            String[] line = oneLine.split(" ");
            for (int c = 0; c < numOfNodes; c++) {
              a[r][c] = Integer.parseInt(line[c]);
            }
          }
          // print adjacency table
          printArray(a, numOfNodes);

          // now try to assign colors and check adjacency table as you move along,
          // backtracking when necessary
          boolean valid;
          int colorCount = 0;
          int x;
          int n = 0; // n represents which node I am currently in
          Node p = v.first.getNext();
          while (p.getNext() != null) {
            colorCount++;
            x = colorList[colorCount];
            p.setColor(x);
            // check if current selected color would be valid for this node
            valid = v.checkAdjacency(x, n, a, numOfNodes);
            if (valid == false) {
              if (colorCount < 4 && colorCount > 0) {
                continue; // will try next color when re-enter while loop
              } else { // backtrack to previous node
                if (n > 0) {
                  v.printProgressThusFar(n);
                  p.setColor(0);
                  p = p.getPrevious();
                  n--;
                  int z = p.getColor();
                  while (z
                      == 4) { // if previous node is at last color, then go to previous one, etc.
                    p.setColor(0);
                    if (n > 0) {
                      p = p.getPrevious();
                      n--;
                    } else break;
                    z = p.getColor();
                  } // when you reach a previous node whose color you can increment, go back through
                    // the while loop again
                  colorCount = z;
                }
              }
            } else { // if(good==true) then just keep going with filling in colors
              p = p.getNext();
              n++;
              colorCount = 0; // reset color back to red for next node
            }
          } // while (p.getNext()!=null)
          // print the solution that has been found
          v.printList();
        } // end while loop
      } // end try loop
      catch (Exception e) {
        e.printStackTrace();
        System.out.println("An error has occurred!");
      }
    } // end else
  } // end main