-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPossibilityTester.java
More file actions
126 lines (117 loc) · 4.12 KB
/
PossibilityTester.java
File metadata and controls
126 lines (117 loc) · 4.12 KB
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
import java.util.ArrayList;
import java.util.Arrays;
public class PossibilityTester {
private char[] operators = {'+', '-', '*', '/'};
public ArrayList<char[]> mixedOps;
private ArrayList<int []> mixedInts;
private ArrayList<int[]> allSolutionInts;
private ArrayList<char[]> allSolutionsOps;
// Pass int array to Permutations ; returns?
// Pass char array to Permutations ; returns?
// switch statements in a for loop iterating through the returned lists?
public PossibilityTester() {
mixedOps = operatorMixer(operators);
}
/*
* Tests if a given set of integers can possibly form 24.
* Programmed to ignore decimal arithmetic, unintuitive (for players).
*/
public boolean isPossible(int[] values) {
if (values == null) {
return false;
}
allSolutionInts = new ArrayList<int[]>();
allSolutionsOps = new ArrayList<char[]>();
mixedInts = new ArrayList<int[]>();
integerMixer(values, 0, mixedInts);
for (char[] chars : mixedOps) {
for (int[] nums : mixedInts) {
if (evaluate(chars, nums) == 24) {
allSolutionsOps.add(chars);
allSolutionInts.add(nums);
}
}
}
if (allSolutionsOps.isEmpty() || allSolutionInts.isEmpty()) {
return false;
}
return true;
}
public ArrayList<String> getSolutions(int[] values, int solutionNum) {
ArrayList<String> solutions = new ArrayList<String>();
if (!allSolutionInts.isEmpty() || isPossible(values)) {
for (int i = 0; i < solutionNum && i < allSolutionInts.size(); i += 1) {
int[] ints = allSolutionInts.get(i);
char[] ops = allSolutionsOps.get(i);
String solution = String.format("((( %d %c %d ) %c %d) %c %d )",
ints[0], ops[0], ints[1], ops[1],
ints[2], ops[2], ints[3]);
solutions.add(solution);
}
} else {
solutions.add("No Solution, Try another set of cards!");
}
return solutions;
}
private int evaluate(char[] operators, int[] numbers) {
int result = apply(operators[0], numbers[0], numbers[1]);
for (int i = 1; i < 3; i += 1) {
result = apply(operators[i], result, numbers[i+1]);
if (result == 28562) {
return 0;
}
}
return result;
}
private int apply(char operator, int num1, int num2) {
switch (operator) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
if ((num2 == 0) || (num1 % num2 != 0)) {
return 28562;
} else {
return num1 / num2;
}
default:
//TODO: Handle exceptions here
return 28562;
}
}
/* Permutes operators into arrays of length 3.
* Allows repeats and considers order.
*/
private ArrayList<char[]> operatorMixer(char[] ops) {
ArrayList<char[]> mixed = new ArrayList<char[]>();
for (char first : ops) {
for (char second : ops) {
for (char third : ops) {
char [] proxy = {first, second, third};
mixed.add(proxy);
}
}
}
return mixed;
}
private void integerMixer(int[] arr, int pos, ArrayList<int[]> list){
if (arr.length - pos == 1) {
list.add(arr.clone());
}
else {
for(int i = pos; i < arr.length; i++){
swapper(arr, pos, i);
integerMixer(arr, pos+1, list);
swapper(arr, pos, i);
}
}
}
private void swapper(int[] arr, int pos1, int pos2){
int h = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = h;
}
}