@matfah/

# NumberCube FR 2009

## No description

Files
• Main.java
• CubeMethods.java
• NumberCube.java

# This Plugin Crashed!

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)
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++) {
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;
}
}