@matfah/

NumberCube FR 2009

Java

No description

fork
loading
Files
  • Main.java
  • CubeMethods.java
  • NumberCube.java

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.5131080741942791","path":"Main.java","file":{"path":"Main.java","content":{"asEncoding":{"base64":"cHVibGljIGNsYXNzIE1haW4gewoKICBzdGF0aWMgaW50IHNjb3JlID0gMDsKCi8vVGhpcyBjb2RlIGlzIGZvciB0ZXN0aW5nIHlvdXIgcHJvZ3JhbSAtIHlvdSBkb24ndCBuZWVkIHRvIGxvb2sgaGVyZSEKICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKQoJewoJICAvL2Z1biBsaXR0bGUgaGFjayB0byBnZXQgdGhlIHNjb3JlIHRvIHNob3cgdXAgYWZ0ZXIgU3lzdGVtLmV4aXQoMCkKCSAgUnVudGltZS5nZXRSdW50aW1lKCkuYWRkU2h1dGRvd25Ib29rKG5ldyBUaHJlYWQoKSB7CiAgICAgcHVibGljIHZvaWQgcnVuKCkgeyBTeXN0ZW0ub3V0LnByaW50bG4oIkZpbmFsIFNjb3JlOiAiICsgc2NvcmUgKyAiIC8gIiArIDE1KTsgfQogICAgfSk7CgkgIAogICAgamF2YS51dGlsLlJhbmRvbSBteVJhbmQgPSBoYWNrUmFuZG9tKCk7CiAgICAKICAgIGlmKHRlc3RDdWJlVG9zc2VzKDYsIDMsIG15UmFuZCkpIHNjb3JlKys7CiAgICBpZih0ZXN0Q3ViZVRvc3Nlcyg1LCAxMCwgbXlSYW5kKSkgc2NvcmUrKzsKICAgIGlmKHRlc3RDdWJlVG9zc2VzKDEsIDQsIG15UmFuZCkpIHNjb3JlKys7CiAgICBpZih0ZXN0Q3ViZVRvc3NlcygyMCwgOCwgbXlSYW5kKSkgc2NvcmUrKzsKICAgIAogICAgc2NvcmUgKz0gdGVzdExvbmdlc3RSdW4oKTsKICAgICAgICAKICAgICAgICAKCX0KICAgIAogIC8vaGVscGVyIGNvZGUgdG8gdGVzdCBpZiB0aGUgbG9uZ2VzdCBydW4gbWV0aG9kIHdvcmtzIHByb3Blcmx5CiAgcHJpdmF0ZSBzdGF0aWMgaW50IHRlc3RMb25nZXN0UnVuKCkgewogIAogICAgICBpbnQgc2NvcmUgPSAwOwogICAgICBpbnRbXVtdIGV4YW1wbGVzID0gIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgezEsIDF9LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB7MSwgMiwgMX0sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsxLCAxLCAyfSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgezEsIDIsIDJ9LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB7MX0sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsxLCAxLCAzLCAyLCAyfSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgezEsIDEsIDMsIDIsIDIsIDJ9LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB7MywgMiwgMiwgNCwgNCwgNCwgNCwgNSwgMSwgMSwgNiwgNiwgNiwgNiwgNiwgMX0sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHs3LCA3LCA3LCA3LCA2LCAzLCAzLCAzfSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgezEsIDIsIDIsIDIsIDMsIDIsIDQsIDQsIDQsIDQsIDQsIDV9LAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB7MSwgNSwgNSwgNCwgMywgMSwgMiwgMiwgMiwgMiwgNiwgMSwgMywgMywgNSwgNSwgNSwgNX0KICAgICAgICAgICAgICAgICAgICAgICAgICB9OwogICAgICBpbnRbXSByZXN1bHRzID0gewogICAgICAgICAgICAgICAgICAgICAgICAgIDAsCiAgICAgICAgICAgICAgICAgICAgICAgICAgLTEsCiAgICAgICAgICAgICAgICAgICAgICAgICAgMCwKICAgICAgICAgICAgICAgICAgICAgICAgICAxLAogICAgICAgICAgICAgICAgICAgICAgICAgIC0xLAogICAgICAgICAgICAgICAgICAgICAgICAgIDAsCiAgICAgICAgICAgICAgICAgICAgICAgICAgMywKICAgICAgICAgICAgICAgICAgICAgICAgICAxMCwKICAgICAgICAgICAgICAgICAgICAgICAgICAwLAogICAgICAgICAgICAgICAgICAgICAgICAgIDYsCiAgICAgICAgICAgICAgICAgICAgICAgICAgNgogICAgICAgICAgICAgICAgICAgICAgfTsKICAgICAgCiAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCByZXN1bHRzLmxlbmd0aDsgaSsrKSB7CiAgICAgICAgICBpbnQgYW5zd2VyID0gQ3ViZU1ldGhvZHMuZ2V0TG9uZ2VzdFJ1bihleGFtcGxlc1tpXSk7CiAgICAgICAgICBpZihhbnN3ZXIgPT0gcmVzdWx0c1tpXSkgewogICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiUGFzc2VkIGdldExvbmdlc3RSdW4oIiArIGFycmF5VG9TdHJpbmcoZXhhbXBsZXNbaV0pICsgIik6IHJlc3VsdCB3YXMgIiArIGFuc3dlcik7CiAgICAgICAgICAgICAgc2NvcmUrKzsKICAgICAgICAgIH0KICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiRkFJTEVEIGdldExvbmdlc3RSdW4oIiArIGFycmF5VG9TdHJpbmcoZXhhbXBsZXNbaV0pICsgIik6IHJlc3VsdCB3YXMgIiArIGFuc3dlciArICIsIHNob3VsZCBiZSAiICsgcmVzdWx0c1tpXSk7CiAgICAgICAgICAgICAgU3lzdGVtLmV4aXQoMCk7CiAgICAgICAgICB9CiAgICAgIH0KICAgICAgcmV0dXJuIHNjb3JlOwogIAogIH0KICAKICAvL2hlbHBlciBjb2RlIHRvIHRlc3QgaWYgdGhlIGN1YmUgdG9zc2VzIG1ldGhvZCB3b3JrcyBwcm9wZXJseQogIHByaXZhdGUgc3RhdGljIGJvb2xlYW4gdGVzdEN1YmVUb3NzZXMoa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107CiAgICAgIGludCBwb3MgPSAwOwogICAgICBmb3IoaW50IHg6IHJlc3VsdHMpIHsKICAgICAgICAgIGludCBuZXh0ID0gKGludCkobXlSYW5kLm5leHREb3VibGUoKSpjdWJlU2lkZXMpKzE7CiAgICAgICAgICBpZih4ICE9IG5leHQpCiAgICAgICAgICAgICAgZmFpbGVkID0gdHJ1ZTsKICAgICAgICAgIGFjdHVhbFtwb3NdID0gbmV4dDsKICAgICAgICAgIHBvcysrOwogICAgICB9CiAgICAgIAogICAgICBpZihmYWlsZWQpIHsKICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiRkFJTEVEIGdldEN1YmVUb3NzZXMoIiArIGN1YmVTaWRlcyArICIgc2lkZWQgY3ViZSwiICsgdG9zc2VzICsgIik6IHJlc3VsdCB3YXMgIiArIGFycmF5VG9TdHJpbmcocmVzdWx0cykgKyAiLCBzaG91bGQgYmUgIiArIGFycmF5VG9TdHJpbmcoYWN0dWFsKSk7CiAgICAgICAgICBTeXN0ZW0uZXhpdCgwKTsKICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgfQogICAgICBlbHNlIHsKICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiUGFzc2VkIGdldEN1YmVUb3NzZXMoIiArIGN1YmVTaWRlcyArICIgc2lkZWQgY3ViZSwiICsgdG9zc2VzICsgIik6IHJlc3VsdCB3YXMgIiArIGFycmF5VG9TdHJpbmcocmVzdWx0cykpOwogICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgIH0KICAgICAgCiAgfQogIAogICAgICAvL2hlbHBlciBtZXRob2QgZm9yIHRoZSB0ZXN0aW5nIHBhcnQgb2YgdGhlIHByb2dyYW0KICAvL3JldHVybnMgdGhlIHN0cmluZyByZXByZXNlbnRhdGlvbiBvZiB0aGUgYXJyYXkKICBwcml2YXRlIHN0YXRpYyBTdHJpbmcgYXJyYXlUb1N0cmluZyhpbnRbXSBhKSB7CiAgICAgIGlmKGEgPT0gbnVsbCkKICAgICAgICAgIHJldHVybiAibnVsbCI7CiAgCiAgICAgIFN0cmluZyBzdHIgPSAiIjsKICAgICAgZm9yKGludCB4OiBhKSB7CiAgICAgICAgICBzdHIgKz0geCArICIgIjsKICAgICAgfQogICAgICByZXR1cm4gc3RyOwogIH0KICAKICAvL015IG1pc2NoZXZpb3VzIGNvZGUgdGhhdCBjcmVhdGVzIGEgcmFuZG9tIG51bWJlciBnZW5lcmF0b3Igd2l0aCB0aGUgc2FtZSBzZWVkIGFzIE1hdGgucmFuZG9tKCkKICBwcml2YXRlIHN0YXRpYyBqYXZhLnV0aWwuUmFuZG9tIGhhY2tSYW5kb20oKSB7CiAgICAgIHRyeSB7CiAgICAgICAgICBNYXRoLnJhbmRvbSgpOwogICAgICAgICAgamF2YS5sYW5nLnJlZmxlY3QuRmllbGQgZmllbGQgPSBqYXZhLmxhbmcuTWF0aC5jbGFzcy5nZXREZWNsYXJlZENsYXNzZXMoKVswXS5nZXREZWNsYXJlZEZpZWxkKCJyYW5kb21OdW1iZXJHZW5lcmF0b3IiKTsKICAgICAgICAgIGZpZWxkLnNldEFjY2Vzc2libGUodHJ1ZSk7CiAgICAgICAgICAKICAgICAgICAgIGphdmEudXRpbC5SYW5kb20gciA9IChqYXZhLnV0aWwuUmFuZG9tKWZpZWxkLmdldChudWxsKTsKICAgICAgICAgIHJldHVybiByOwogICAgICB9CiAgICAgIGNhdGNoKEV4Y2VwdGlvbiBlKSB7CiAgICAgICAgICBlLnByaW50U3RhY2tUcmFjZSgpOwogICAgICB9CiAgICAgIAogICAgICByZXR1cm4gbnVsbDsKICB9Cn0="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
public class Main {

  static int score = 0;

//This code is for testing your program - you don't need to look here!
  public static void main(String[] args)
	{
	  //fun little hack to get the score to show up after System.exit(0)
	  Runtime.getRuntime().addShutdownHook(new Thread() {
     public void run() { System.out.println("Final Score: " + score + " / " + 15); }
    });
	  
    java.util.Random myRand = hackRandom();
    
    if(testCubeTosses(6, 3, myRand)) score++;
    if(testCubeTosses(5, 10, myRand)) score++;
    if(testCubeTosses(1, 4, myRand)) score++;
    if(testCubeTosses(20, 8, myRand)) score++;
    
    score += testLongestRun();
        
        
	}
    
  //helper code to test if the longest run method works properly
  private static int testLongestRun() {
  
      int score = 0;
      int[][] examples =  {
                              {1, 1},
                              {1, 2, 1},
                              {1, 1, 2},
                              {1, 2, 2},
                              {1},
                              {1, 1, 3, 2, 2},
                              {1, 1, 3, 2, 2, 2},
                              {3, 2, 2, 4, 4, 4, 4, 5, 1, 1, 6, 6, 6, 6, 6, 1},
                              {7, 7, 7, 7, 6, 3, 3, 3},
                              {1, 2, 2, 2, 3, 2, 4, 4, 4, 4, 4, 5},
                              {1, 5, 5, 4, 3, 1, 2, 2, 2, 2, 6, 1, 3, 3, 5, 5, 5, 5}
                          };
      int[] results = {
                          0,
                          -1,
                          0,
                          1,
                          -1,
                          0,
                          3,
                          10,
                          0,
                          6,
                          6
                      };
      
      for(int i = 0; i < results.length; i++) {
          int answer = CubeMethods.getLongestRun(examples[i]);
          if(answer == results[i]) {
              System.out.println("Passed getLongestRun(" + arrayToString(examples[i]) + "): result was " + answer);
              score++;
          }
          else {
              System.out.println("FAILED getLongestRun(" + arrayToString(examples[i]) + "): result was " + answer + ", should be " + results[i]);
              System.exit(0);
          }
      }
      return score;
  
  }
  
  //helper code to test if the cube tosses method works properly
  private static boolean testCubeTosses(int cubeSides, int tosses, java.util.Random rand) {
      long seed = System.currentTimeMillis();
      rand.setSeed(seed);
      java.util.Random myRand = new java.util.Random(seed);
  
      NumberCube cube = new NumberCube(cubeSides);
      int[] results = CubeMethods.getCubeTosses(cube, tosses);
      
      if(results == null) {
          System.out.println("FAILED getCubeTosses(" + cubeSides + " sided cube," + tosses + "): result was null");
          System.exit(0);
          return false;
      }
      else if(results.length != tosses) {
          System.out.println("FAILED getCubeTosses(" + cubeSides + " sided cube," + tosses + "): result length was " + results.length + ", should be " + tosses);
          System.exit(0);
          return false;
      }
      
      boolean failed = false;
      int[] actual = new int[tosses];
      int pos = 0;
      for(int x: results) {
          int next = (int)(myRand.nextDouble()*cubeSides)+1;
          if(x != next)
              failed = true;
          actual[pos] = next;
          pos++;
      }
      
      if(failed) {
          System.out.println("FAILED getCubeTosses(" + cubeSides + " sided cube," + tosses + "): result was " + arrayToString(results) + ", should be " + arrayToString(actual));
          System.exit(0);
          return false;
      }
      else {
          System.out.println("Passed getCubeTosses(" + cubeSides + " sided cube," + tosses + "): result was " + arrayToString(results));
          return true;
      }
      
  }
  
      //helper method for the testing part of the program
  //returns the string representation of the array
  private static String arrayToString(int[] a) {
      if(a == null)
          return "null";
  
      String str = "";
      for(int x: a) {
          str += x + " ";
      }
      return str;
  }
  
  //My mischevious code that creates a random number generator with the same seed as Math.random()
  private static java.util.Random hackRandom() {
      try {
          Math.random();
          java.lang.reflect.Field field = java.lang.Math.class.getDeclaredClasses()[0].getDeclaredField("randomNumberGenerator");
          field.setAccessible(true);
          
          java.util.Random r = (java.util.Random)field.get(null);
          return r;
      }
      catch(Exception e) {
          e.printStackTrace();
      }
      
      return null;
  }
}