コード例 #1
0
 public void payToPlayer(Player payee, Long amount) {
   // if calling giveMoney will be anything but not 0 then all belongings go to payee
   Long owed = this.giveMoney(amount);
   payee.getMoney(amount - owed);
   if (owed > 0) {
     StdOut.println(this.name + " owes to " + payee.name + " " + Bank.moneyEuro(owed));
     if (this == Game.getCurrentPlayer()) {
       // the moment player gave up it will stop recursing
       if (this.playing) {
         // allows player to sell something and then calls itself,
         // so it won't leave till it's paid ot player gave up
         this.sellProperty();
         this.payToPlayer(payee, owed);
       }
     } else {
       for (BoardTile tile : BoardSearch.all().filterByOwner(this).getTiles()) {
         tile.changeOwner(payee);
       }
       // give payee money you got from selling of upgrades
       StdOut.println(
           payee + " got " + Bank.moneyEuro(this.cash) + " from " + this.name + "'s upgrades.");
       this.giveMoney(this.cash);
       this.cash = 0L;
     }
   }
 }
コード例 #2
0
  public static void main(String[] args) {

    // key = word, value = set of files containing that word
    ST<String, SET<File>> st = new ST<String, SET<File>>();

    // create inverted index of all files
    StdOut.println("Indexing files");
    for (String filename : args) {
      StdOut.println("  " + filename);
      File file = new File(filename);
      In in = new In(file);
      while (!in.isEmpty()) {
        String word = in.readString();
        if (!st.contains(word)) st.put(word, new SET<File>());
        SET<File> set = st.get(word);
        set.add(file);
      }
    }

    // read queries from standard input, one per line
    while (!StdIn.isEmpty()) {
      String query = StdIn.readString();
      if (st.contains(query)) {
        SET<File> set = st.get(query);
        for (File file : set) {
          StdOut.println("  " + file.getName());
        }
      }
    }
  }
コード例 #3
0
  // test client, optional
  public static void main(String[] args) {
    StdOut.println("start testing..");

    int N = 20;
    Percolation p = new Percolation(N);

    int openCounter = 0;
    while (true) {

      int i = (int) (Math.random() * N + 1);
      int j = (int) (Math.random() * N + 1);

      if (!p.isOpen(i - 1, j - 1)) {
        openCounter++;
        StdOut.println("open: " + i + ", " + j);

        p.open(i, j);

        if (p.percolates()) {
          break;
        }
      }
    }

    StdOut.println("open counter: " + openCounter);
    double threshold = openCounter / (double) (N * N);
    StdOut.println(threshold);
  }
コード例 #4
0
ファイル: GabowSCC.java プロジェクト: slideclick/algrithmS
  /** Unit tests the <tt>GabowSCC</tt> data type. */
  public static void main(String[] args) {
    In in = new In(args[0]);
    Digraph G = new Digraph(in);
    GabowSCC scc = new GabowSCC(G);

    // number of connected components
    int M = scc.count();
    StdOut.println(M + " components");

    // compute list of vertices in each strong component
    Queue<Integer>[] components = (Queue<Integer>[]) new Queue[M];
    for (int i = 0; i < M; i++) {
      components[i] = new Queue<Integer>();
    }
    for (int v = 0; v < G.V(); v++) {
      components[scc.id(v)].enqueue(v);
    }

    // print results
    for (int i = 0; i < M; i++) {
      for (int v : components[i]) {
        StdOut.print(v + " ");
      }
      StdOut.println();
    }
  }
コード例 #5
0
  // decrease this players cash by amount
  // return if you owe some resting standing (you didn't pay fully)
  public Long giveMoney(Long amount) {
    if (this.cash > amount) {
      this.cash -= amount;
      StdOut.println(
          this.name
              + " paid "
              + Bank.moneyEuro(amount)
              + " (current cash "
              + Bank.moneyEuro(this.cash)
              + ")");
      return 0L;
    } else {
      Long owed = amount - this.cash;

      StdOut.println(
          this.name
              + " paid "
              + Bank.moneyEuro(this.cash)
              + " (owed "
              + Bank.moneyEuro(owed)
              + ")");
      this.cash = 0L;
      return owed;
    }
  }
コード例 #6
0
 private void search() {
   Point[] aux = new Point[size];
   for (int i = 0; i < size; i++) aux[i] = p[i];
   for (int i = 0; i < size; i++) {
     Arrays.sort(aux);
     Arrays.sort(aux, p[i].SLOPE_ORDER);
     int start = 1;
     int end = 1;
     for (int j = 2; j < size + 1; j++) {
       if (j != size && p[i].slopeTo(aux[j]) == p[i].slopeTo(aux[j - 1])) {
         end++;
       } else {
         if (end > start + 1) {
           int k;
           for (k = start; k <= end; k++) {
             if (p[i].compareTo(aux[k]) >= 0) {
               break;
             }
           }
           if (k == end + 1) {
             p[i].drawTo(aux[end]);
             StdOut.print(p[i].toString());
             for (int m = start; m <= end; m++) {
               StdOut.print(" -> " + aux[m].toString());
             }
             StdOut.println();
           }
         }
         start = j;
         end = j;
       }
     }
   }
 }
コード例 #7
0
ファイル: DegreesOfSeparation.java プロジェクト: imace/zava
  /**
   * Reads in a social network from a file, and then repeatedly reads in individuals from standard
   * input and prints out their degrees of separation. Takes three command-line arguments: the name
   * of a file, a delimiter, and the name of the distinguished individual. Each line in the file
   * contains the name of a vertex, followed by a list of the names of the vertices adjacent to that
   * vertex, separated by the delimiter.
   */
  public static void main(String[] args) {
    String filename = args[0];
    String delimiter = args[1];
    String source = args[2];

    // StdOut.println("Source: " + source);

    SymbolGraph sg = new SymbolGraph(filename, delimiter);
    Graph G = sg.G();
    if (!sg.contains(source)) {
      StdOut.println(source + " not in database.");
      return;
    }

    int s = sg.index(source);
    BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

    while (!StdIn.isEmpty()) {
      String sink = StdIn.readLine();
      if (sg.contains(sink)) {
        int t = sg.index(sink);
        if (bfs.hasPathTo(t)) {
          for (int v : bfs.pathTo(t)) {
            StdOut.println("   " + sg.name(v));
          }
        } else {
          StdOut.println("Not connected");
        }
      } else {
        StdOut.println("   Not in database.");
      }
    }
  }
コード例 #8
0
  public static void main(String[] args) {
    String filename = args[0];
    String delimiter = args[1];
    String source = args[2];

    StdOut.println("Zrodlo: " + source);

    SymbolGraph sg = new SymbolGraph(filename, delimiter);
    Graph G = sg.G();
    if (!sg.contains(source)) {
      StdOut.println(source + " nie wystepuje w bazie danych.");
      return;
    }

    int s = sg.index(source);
    BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

    while (!StdIn.isEmpty()) {
      String sink = StdIn.readLine();
      if (sg.contains(sink)) {
        int t = sg.index(sink);
        if (bfs.hasPathTo(t)) {
          for (int v : bfs.pathTo(t)) {
            StdOut.println("   " + sg.name(v));
          }
        } else {
          StdOut.println("Brak polaczenia");
        }
      } else {
        StdOut.println("   Nie wystepuje w bazie danych.");
      }
    }
  }
コード例 #9
0
  public static void main(String[] args) {

    // create initial board from file
    In in = new In(args[0]);
    int N = in.readInt();
    int[][] tiles = new int[N][N];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        tiles[i][j] = in.readInt();
      }

    Board initial = new Board(tiles);
    /*
    System.out.println(initial.isSolvable());
    System.out.println(initial.toString());
    Iterable<Board> newbrdstack = new Stack<Board>();
    newbrdstack = initial.neighbors();
    System.out.println(newbrdstack.toString());
     */

    // check if puzzle is solvable; if so, solve it and output solution
    if (initial.isSolvable()) {
      Solver solver = new Solver(initial);
      StdOut.println("Minimum number of moves = " + solver.moves());
      for (Board board : solver.solution()) StdOut.println(board);
    }

    // if not, report unsolvable
    else {
      StdOut.println("Unsolvable puzzle");
    }
  }
コード例 #10
0
ファイル: StdStats.java プロジェクト: asccharania/learning
 /** Test client. Convert command-line arguments to array of doubles and call various methods. */
 public static void main(String[] args) {
   final double[] a = StdArrayIO.readDouble1D();
   StdOut.printf("       min %7.3f\n", min(a));
   StdOut.printf("      mean %7.3f\n", mean(a));
   StdOut.printf("       max %7.3f\n", max(a));
   StdOut.printf("   std dev %7.3f\n", stddev(a));
 }
コード例 #11
0
ファイル: TopologicalX.java プロジェクト: akiuesltd/src_root
  /** Unit tests the <tt>com.akieus.algos.coursera.lib.TopologicalX</tt> data type. */
  public static void main(String[] args) {

    // create random DAG with V vertices and E edges; then add F random edges
    int V = Integer.parseInt(args[0]);
    int E = Integer.parseInt(args[1]);
    int F = Integer.parseInt(args[2]);
    Digraph G = DigraphGenerator.dag(V, E);

    // add F extra edges
    for (int i = 0; i < F; i++) {
      int v = StdRandom.uniform(V);
      int w = StdRandom.uniform(V);
      G.addEdge(v, w);
    }

    StdOut.println(G);

    // find a directed cycle
    TopologicalX topological = new TopologicalX(G);
    if (!topological.hasOrder()) {
      StdOut.println("Not a DAG");
    }

    // or give topologial sort
    else {
      StdOut.print("com.akieus.algos.coursera.lib.Topological order: ");
      for (int v : topological.order()) {
        StdOut.print(v + " ");
      }
      StdOut.println();
    }
  }
コード例 #12
0
ファイル: Interval2D.java プロジェクト: akiuesltd/src_root
  /** Unit tests the <tt>com.akieus.algos.coursera.lib.Interval2D</tt> data type. */
  public static void main(String[] args) {
    double xlo = Double.parseDouble(args[0]);
    double xhi = Double.parseDouble(args[1]);
    double ylo = Double.parseDouble(args[2]);
    double yhi = Double.parseDouble(args[3]);
    int T = Integer.parseInt(args[4]);

    Interval1D xinterval = new Interval1D(xlo, xhi);
    Interval1D yinterval = new Interval1D(ylo, yhi);
    Interval2D box = new Interval2D(xinterval, yinterval);
    box.draw();

    Counter counter = new Counter("hits");
    for (int t = 0; t < T; t++) {
      double x = StdRandom.uniform(0.0, 1.0);
      double y = StdRandom.uniform(0.0, 1.0);
      Point2D p = new Point2D(x, y);

      if (box.contains(p)) counter.increment();
      else p.draw();
    }

    StdOut.println(counter);
    StdOut.printf("box area = %.2f\n", box.area());
  }
コード例 #13
0
  private int displayDebugMenu() {
    ArrayList<String> items =
        new ArrayList<String>(
            Arrays.asList(
                "Exit debugging",
                ((Game.getDebugDice()) ? "Disable" : "Enable") + " dice debugging",
                "Jump to tile #",
                "Execute current tile action",
                "Set your cash",
                "Set jail time",
                "Pick community card",
                "Pick chance card",
                "Show deck sizes",
                "Switch to other player",
                "Display board",
                "Access available upgrades"));

    StdOut.println(
        "\nWARNING !!! Be careful, you could break the game.\n Some of these functions do not do any sanity checks!!!");
    StdOut.println(
        "And really if some of these functions is used in some situations, it will make the game misbehave.");
    return UI.displayMenuNGetChoice(
        "Debug options (" + Bank.moneyEuro(Game.getCurrentPlayer().getCash()) + " cash)",
        items,
        true,
        true);
  }
コード例 #14
0
ファイル: StdOut.java プロジェクト: rakelwb/School
  /** Simple unit test for <tt>StdOut</tt>. */
  public static void main(String[] args) {

    // write to stdout
    StdOut.println("Test");
    StdOut.println(17);
    StdOut.println(true);
    StdOut.printf("%.6f\n", 1.0 / 7.0);
  }
コード例 #15
0
 // test client
 public static void main(String[] args) {
   double z = Double.parseDouble(args[0]);
   double mu = Double.parseDouble(args[1]);
   double sigma = Double.parseDouble(args[2]);
   StdOut.println(Phi(z, mu, sigma));
   double y = Phi(z);
   StdOut.println(PhiInverse(y));
 }
コード例 #16
0
ファイル: StdArrayIO.java プロジェクト: 9r3y/c47
 /** Print an array of ints to standard output. */
 public static void print(int[] a) {
   int N = a.length;
   StdOut.println(N);
   for (int i = 0; i < N; i++) {
     StdOut.printf("%9d ", a[i]);
   }
   StdOut.println();
 }
コード例 #17
0
  public void printQueue() {

    for (int i = 1; i < N + 1; i++) {
      StdOut.println(queue[i].toString());
    }

    StdOut.println();
  }
コード例 #18
0
ファイル: RedandBlackBST.java プロジェクト: PinkyK/new
 /**
  * ************************************************************************* Check integrity of
  * red-black tree data structure.
  * *************************************************************************
  */
 private boolean check() {
   if (!isBST()) StdOut.println("Not in symmetric order");
   if (!isSizeConsistent()) StdOut.println("Subtree counts not consistent");
   if (!isRankConsistent()) StdOut.println("Ranks not consistent");
   if (!is23()) StdOut.println("Not a 2-3 tree");
   if (!isBalanced()) StdOut.println("Not balanced");
   return isBST() && isSizeConsistent() && isRankConsistent() && is23() && isBalanced();
 }
コード例 #19
0
ファイル: Ordered.java プロジェクト: qiaoyuguo/practice
  public static void main(String[] args) {
    int a = Integer.parseInt(args[0]);
    int b = Integer.parseInt(args[1]);
    int c = Integer.parseInt(args[2]);

    if ((a <= b && b <= c) || (a >= b && b >= c)) StdOut.println("true");
    else StdOut.println("false");
  }
コード例 #20
0
 public static void main(String[] args) { // test client (described below)
   int N = Integer.parseInt(args[0]);
   int T = Integer.parseInt(args[1]);
   PercolationStats test = new PercolationStats(N, T);
   StdOut.println("mean                    = " + test.mean());
   StdOut.println("stddev                  = " + test.stddev());
   StdOut.println("95% confidence interval = " + test.confidenceLo() + ", " + test.confidenceHi());
 }
コード例 #21
0
ファイル: FFT.java プロジェクト: dpkolev/audio_ai
 // display an array of Complex numbers to standard output
 public static void show(Complex[] x, String title) {
   StdOut.println(title);
   StdOut.println("-------------------");
   for (int i = 0; i < x.length; i++) {
     StdOut.println(x[i]);
   }
   StdOut.println();
 }
コード例 #22
0
ファイル: Digraph.java プロジェクト: demas/code-snippets
  /** Test client. */
  public static void main(String[] args) {
    In in = new In(args[0]);
    Digraph G = new Digraph(in);
    StdOut.println(G);

    StdOut.println();
    for (int v = 0; v < G.V(); v++) for (int w : G.adj(v)) StdOut.println(v + "->" + w);
  }
コード例 #23
0
ファイル: Deque.java プロジェクト: smsahu/playground-notes
  public static void main(String[] args) {
    Deque<Integer> ints = new Deque<Integer>();

    assert ints.size() == 0;
    assert ints.isEmpty();

    ints.addFirst(1);
    assert ints.size() == 1;
    assert !ints.isEmpty();

    int val = ints.removeFirst();
    assert val == 1;
    assert ints.size() == 0;
    assert ints.isEmpty();

    ints.addFirst(2);
    assert ints.size() == 1;
    assert !ints.isEmpty();

    val = ints.removeLast();
    assert val == 2;
    assert ints.size() == 0;
    assert ints.isEmpty();

    ints.addFirst(3);
    ints.addFirst(2);
    ints.addFirst(1);
    ints.addLast(4);
    ints.addLast(5);
    ints.addLast(6);

    assert ints.size() == 6;
    assert !ints.isEmpty();

    for (int value : ints) {
      StdOut.print(value + " ");
    }

    StdOut.println();

    while (!ints.isEmpty()) {
      StdOut.println(ints.removeLast());
    }

    assert ints.size() == 0;
    assert ints.isEmpty();

    for (int i = 0; i < 10; ++i) {
      ints.addFirst(i);
    }

    for (int i = 0; i < 10; ++i) {
      assert ints.size() == 10 - i;
      ints.removeLast();
    }

    assert ints.size() == 0;
  }
コード例 #24
0
ファイル: DoublingRatio.java プロジェクト: oredi/algorithms
 public static void main(String[] args) {
   double prev = timeTrial(125);
   for (int N = 250; true; N += N) {
     double time = timeTrial(N);
     StdOut.printf("%7d %5.1f", N, time);
     StdOut.printf("%5.1f\n", time / prev);
     prev = time;
   }
 }
コード例 #25
0
ファイル: RedandBlackBST.java プロジェクト: PinkyK/new
 /** Unit tests the <tt>RedBlackBST</tt> data type. */
 public static void main(String[] args) {
   RedBlackBST<String, Integer> st = new RedBlackBST<String, Integer>();
   for (int i = 0; !StdIn.isEmpty(); i++) {
     String key = StdIn.readString();
     st.put(key, i);
   }
   for (String s : st.keys()) StdOut.println(s + " " + st.get(s));
   StdOut.println();
 }
コード例 #26
0
ファイル: Stack.java プロジェクト: nachogames/MyCoding
 /** Unit tests the <tt>Stack</tt> data type. */
 public static void main(String[] args) {
   Stack<String> s = new Stack<String>();
   while (!StdIn.isEmpty()) {
     String item = StdIn.readString();
     if (!item.equals("-")) s.push(item);
     else if (!s.isEmpty()) StdOut.print(s.pop() + " ");
   }
   StdOut.println("(" + s.size() + " left on stack)");
 }
コード例 #27
0
ファイル: Queue.java プロジェクト: ablochha/Resume
 /** Unit tests the <tt>Queue</tt> data type. */
 public static void main(String[] args) {
   Queue<String> q = new Queue<String>();
   while (!StdIn.isEmpty()) {
     String item = StdIn.readString();
     if (!item.equals("-")) q.enqueue(item);
     else if (!q.isEmpty()) StdOut.print(q.dequeue() + " ");
   }
   StdOut.println("(" + q.size() + " left on queue)");
 }
コード例 #28
0
 public static void main(String[] args) {
   In in = new In(args[0]);
   EdgeWeightedGraph G = new EdgeWeightedGraph(in);
   KruskalMST mst = new KruskalMST(G);
   for (Edge e : mst.edges()) {
     StdOut.println(e);
   }
   StdOut.printf("%.5f\n", mst.weight());
 }
コード例 #29
0
ファイル: StdArrayIO.java プロジェクト: 9r3y/c47
 /** Print an array of booleans to standard output. */
 public static void print(boolean[] a) {
   int N = a.length;
   StdOut.println(N);
   for (int i = 0; i < N; i++) {
     if (a[i]) StdOut.print("1 ");
     else StdOut.print("0 ");
   }
   StdOut.println();
 }
コード例 #30
0
ファイル: UnionFind.java プロジェクト: imphat/Algorithms
 private static void printArray(int[] array) {
   if (array != null) {
     for (int i = 0; i < array.length; i++) {
       StdOut.print(array[i] + " ");
     }
     StdOut.println();
   } else {
     StdOut.println("Array not available");
   }
 }