-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem.java
59 lines (53 loc) · 2.17 KB
/
Problem.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
import java.util.ArrayList;
import java.util.Random;
public class Problem {
//Instance Variables
private int numPrems;
private ArrayList<Statement> premises = new ArrayList<>(); //list of premises
private Statement goal;
//Constructor
Problem(int prems, int nvars, int d) {
this.numPrems = prems;
//select variables to be used in statements
//list of variables used
char[] vars = new char[nvars];
Random r = new Random();
int n = r.nextInt(25 - nvars);
for(int i = 0; i < nvars; i++) { //pick maxNumVars sequential letters to be variables
//array of alphabet, V is not included so it doesn't get mixed with the OR symbol
char[] letters = "ABCDEFGHIJKLMNOPQRSTUWXYZ".toCharArray();
vars[i] = letters[n+i];
}
for(int i = 0; i < numPrems; i++) {
Statement s = new Statement(d, vars); //create specified number of premise statements
this.premises.add(s);
}
this.goal = new Statement(d, vars); //create goal statement
}
void printProblem() { //print premises and goal
for(int i = 0; i < numPrems; i++) {
premises.get(i).printStatement(); //each premise printed on different line
}
String g = "\u2234 " + goal.toString(); //"therefore" symbol added in front of goal
System.out.println(g);
}
public String toString() { //convert problem to string
StringBuilder ps = new StringBuilder();
for(int i = 0; i < numPrems; i++) {
ps.append(premises.get(i).toString());
ps.append("\n");
}
ps.append("\u2234 ").append(goal.toString());
return ps.toString();
}
String convertForm() { //convert form of problem to one usable by proplog.js for validity checking
StringBuilder s = new StringBuilder();
for (Statement premises1 : premises) {
s.append("(");
s.append(premises1.convertForm());
s.append(") & ");
}
s.append("(").append(goal.convertForm()).append(")");
return s.toString();
}
}