-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.java
More file actions
138 lines (129 loc) · 3.17 KB
/
Clock.java
File metadata and controls
138 lines (129 loc) · 3.17 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
127
128
129
130
131
132
133
134
135
136
137
138
package puzzleSolver;
import java.util.ArrayList;
/**
*
*File:
* $Id: Clock.java,v 1.5 2013/05/01 11:46:15 cas5420 Exp $
*
*Revisions
* $Log: Clock.java,v $
* Revision 1.5 2013/05/01 11:46:15 cas5420
* Updated solver to generics
*
* Revision 1.4 2013/04/27 13:00:58 cas5420
* Started project
*
* Revision 1.6 2013/04/14 20:50:52 mgp9795
* Part 2 Adjustments
*
* Revision 1.5 2013/04/01 20:04:20 mgp9795
* *** empty log message ***
*
* Revision 1.4 2013/04/01 20:02:46 mgp9795
* *** empty log message ***
*
* Revision 1.3 2013/04/01 20:01:40 mgp9795
* *** empty log message ***
*
* Revision 1.2 2013/04/01 19:58:28 mgp9795
* Arg Length Check
*
* Revision 1.1 2013/04/01 19:41:43 mgp9795
* Initial Version
*
*/
/**
*
* @author Michael
* @author Chris Sleys
*
*/
public class Clock implements Puzzle<Integer> {
private int numHours; // the number of hours on the clock
private int startTime; // the starting time for the puzzle
private int goalTime; // the time we are attempting to reach
/**
* main method for program
*
* @param args
* - holds values for private fields
*/
public static void main(String[] args) {
ArrayList<Integer> solution;
if (args.length == 3) {
solution = new Solver<Integer>().solve(new Clock(Integer.parseInt(args[0]),
Integer.parseInt(args[1]), Integer.parseInt(args[2])));
if (solution != null) {
for (int i = 0; i < solution.size(); i++) {
System.out.println("Step " + i + ": "
+ solution.get(i));
}
} else {
System.out.println("Step 0: " + Integer.parseInt(args[1]));
}
} else {
System.out.println("Usage: java Clock hours start goal");
}
}
/**
* Constructor, sets initial field values
*
* @param numHours
* - the number of hours on the clock
* @param startTime
* - the starting time for the puzzle
* @param goalTime
* - the time we are attempting to reach
*/
public Clock(int numHours, int startTime, int goalTime) {
this.numHours = numHours;
this.startTime = startTime;
this.goalTime = goalTime;
}
/**
* Returns the goal configurating
*
* @return Integer representing goal time.
*/
public boolean isGoal(Integer config) {
if (this.goalTime == config) {
return true;
} else {
return false;
}
}
/**
* returns an ArrayList of neighbors to parameter
*
* @param config
* - current puzzle configuration
* @return ArrayList<ArrayList<Integer>> neighbors
*/
public ArrayList<Integer> getNeighbors(Integer config) {
ArrayList<Integer> neighbors = new ArrayList<Integer>();
Integer sub1, sub2;
if (config > 1 && config < numHours) {
sub1 = config - 1;
sub2 = config + 1;
} else {
if (config == 1) {
sub1 = numHours;
sub2 = config + 1;
} else {
sub1 = config - 1;
sub2 = 1;
}
}
neighbors.add(sub1);
neighbors.add(sub2);
return neighbors;
}
/**
* returns initial puzzle time
*
* @return - ArrayList<Integer> startTime
*/
public Integer getStart() {
return this.startTime;
}
}