-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrisonBreakLab.java
171 lines (144 loc) · 3.77 KB
/
PrisonBreakLab.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
public class PrisonBreakLab {
private int[] boxes;
public PrisonBreakLab(int[] boxes) {
super();
this.boxes = boxes;
}
public PrisonBreakLab() {
boxes = new int[100];
reset();
}
public int howManyPrisoners() {
return boxes.length;
}
public void shuffle() {
for(int i=0; i<boxes.length; i++) {
int randomSpot = (int)(Math.random() * boxes.length-1);
int temp = boxes[i];
boxes[i] = boxes[randomSpot];
boxes[randomSpot] = temp;
}
}
public void reset() {
for (int i=0; i<boxes.length; i++) {
boxes[i] = i;
}
}
public boolean simulateSmart(){
double successfulPathFindsCounter = 0;
for (int i=0; i<howManyPrisoners(); i++){
if(followSmartPath(i, false)){
successfulPathFindsCounter++;
}
}
return successfulPathFindsCounter/howManyPrisoners() == 1;
}
public double simulate(int trails, boolean whichPath){
double successfulPathFindsCounter = 0;
if(whichPath){
for(int i=0; i<trails; i++){
if(simulateSmart()){
successfulPathFindsCounter++;
}
shuffle();
}
}
else{
for(int i=0; i<trails; i++){
if(simulateRandom()){
successfulPathFindsCounter++;
}
shuffle();
}
}
return (successfulPathFindsCounter/trails) * 100;
}
public boolean followSmartPath(int start, boolean show) {
int temp = start;
if(show) {
System.out.print("| Box : " + temp + "|" + " ---> "+ boxes[temp] + " ===> ");
}
for(int i=0; i<boxes.length/2; i++) {
if(boxes[temp] == start) {
if(show) {
System.out.print("| Box : " + temp+ " | "+ "---> "+ boxes[temp] + " ===> ");
System.out.println("true");
}
return true;
}
else{
temp = boxes[temp];
}
if(show) {
System.out.print("| Box : " + temp + " | "+" ---> "+ boxes[temp] + " ===> ");
}
}
if(show) {
System.out.println("false");
}
return false;
}
public boolean simulateRandom(){
double successfulPathFindsCounter = 0;
for(int i=0; i<howManyPrisoners(); i++){
if(followRandomPath(i, false)){
successfulPathFindsCounter++;
}
}
return successfulPathFindsCounter/howManyPrisoners() == 1;
}
public boolean followRandomPath(int start, boolean show) {
int randomSpot;
boolean[] spotsVisted = new boolean[howManyPrisoners()];
for(int i=0; i<howManyPrisoners()/2; i++){
randomSpot = (int)(Math.random()*howManyPrisoners());
while(spotsVisted[randomSpot]){
randomSpot = (int)(Math.random()*howManyPrisoners());
}
if(boxes[randomSpot] == start){
if(show) {
System.out.print("| Box : " + randomSpot + " | "+ "---> "+ boxes[randomSpot] + " ===> ");
System.out.println("true");
}
return true;
}
else{
spotsVisted[randomSpot] = true;
if(show) {
System.out.print("| Box : " + randomSpot + " | " +" ---> "+ boxes[randomSpot] + " ===> ");
}
}
}
if(show) {
System.out.println("false");
}
return false;
}
public String toString() {
String output = "";
int groups;
if(boxes.length > 10){
groups = boxes.length/10;
}else{
groups = boxes.length/2;
}
for (int j=0; j<groups; j++){
for (int i=j; i<boxes.length; i += groups) {
if(boxes[i]<10 && boxes.length > 10){
output += "| Box " + i + ": " + boxes[i] + " | ";
}
else{
output += "| Box " + i + ": " + boxes[i] + " | ";
}
}
output += "\n";
}
return output;
}
public int[] getBoxes() {
return boxes;
}
public void setBoxes(int[] boxes) {
this.boxes = boxes;
}
}