1 | /* |
2 | * Copyright (c) 2005 The PseudoQ Project. |
3 | * |
4 | * This file is part of PseudoQ. |
5 | * |
6 | * PseudoQ is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU Lesser General Public License as published by |
8 | * the Free Software Foundation; either version 2.1 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * PseudoQ is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public License |
17 | * along with PseudoQ; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19 | */ |
20 | |
21 | package net.sourceforge.pseudoq.solver; |
22 | |
23 | import net.sourceforge.pseudoq.model.Coordinate; |
24 | |
25 | /** |
26 | * Structure representing a single step in the solution of a puzzle. Contains |
27 | * the coordinates of the cell solved, the value placed in the cell, and an |
28 | * optional explanation of the strategy leading to this deduction. |
29 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
30 | */ |
31 | public class SolutionStep { |
32 | /** |
33 | * Creates a new instance of SolutionStep. |
34 | * @param coordinate Coordinates of solved cell. |
35 | * @param value Value which was placed. |
36 | * @param explanation Description of the logic behind the placement. |
37 | */ |
38 | public SolutionStep(Coordinate coordinate, int value, String explanation) { |
39 | this.coordinate = coordinate; |
40 | this.value = value; |
41 | this.explanation = explanation; |
42 | } |
43 | |
44 | /** |
45 | * Creates a new instance of SolutionStep. |
46 | * @param coordinate Coordinates of solved cell. |
47 | * @param value Value which was placed. |
48 | */ |
49 | public SolutionStep(Coordinate coordinate, int value) { |
50 | this(coordinate, value, null); |
51 | } |
52 | |
53 | /** |
54 | * Holds value of property coordinate. |
55 | */ |
56 | private Coordinate coordinate; |
57 | |
58 | /** |
59 | * Getter for property coordinate. |
60 | * @return Value of property coordinate. |
61 | */ |
62 | public Coordinate getCoordinate() { |
63 | return this.coordinate; |
64 | } |
65 | |
66 | /** |
67 | * Setter for property coordinate. |
68 | * @param coordinate New value of property coordinate. |
69 | */ |
70 | public void setCoordinate(Coordinate coordinate) { |
71 | this.coordinate = coordinate; |
72 | } |
73 | |
74 | /** |
75 | * Holds value of property value. |
76 | */ |
77 | private int value; |
78 | |
79 | /** |
80 | * Getter for property value. |
81 | * @return Value of property value. |
82 | */ |
83 | public int getValue() { |
84 | return this.value; |
85 | } |
86 | |
87 | /** |
88 | * Setter for property value. |
89 | * @param value New value of property value. |
90 | */ |
91 | public void setValue(int value) { |
92 | this.value = value; |
93 | } |
94 | |
95 | /** |
96 | * Holds value of property explanation. |
97 | */ |
98 | private String explanation; |
99 | |
100 | /** |
101 | * Getter for property explanation. |
102 | * @return Value of property explanation. |
103 | */ |
104 | public String getExplanation() { |
105 | return this.explanation; |
106 | } |
107 | |
108 | /** |
109 | * Setter for property explanation. |
110 | * @param explanation New value of property explanation. |
111 | */ |
112 | public void setExplanation(String explanation) { |
113 | this.explanation = explanation; |
114 | } |
115 | |
116 | public String toString() { |
117 | return "Cell " + coordinate + " = " + value + |
118 | ((explanation != null) ? (" due to " + explanation) : ""); |
119 | } |
120 | |
121 | public boolean equals(Object obj) { |
122 | boolean retValue = false; |
123 | |
124 | if ((obj instanceof SolutionStep) && (obj != null)) { |
125 | SolutionStep other = (SolutionStep) obj; |
126 | retValue = (other.coordinate.equals(this.coordinate) && |
127 | (other.value == this.value) && |
128 | ((other.explanation == null) ? (this.explanation == null) : |
129 | other.explanation.equals(this.explanation))); |
130 | } |
131 | |
132 | return retValue; |
133 | } |
134 | |
135 | public int hashCode() { |
136 | int retValue = 0; |
137 | int factor = (Coordinate.MAX_INDEX + 1) * (Coordinate.MAX_INDEX + 1); |
138 | |
139 | retValue = coordinate.hashCode() + (factor * value); |
140 | if (explanation != null) { |
141 | retValue += (factor * 50 * explanation.hashCode()); |
142 | } |
143 | |
144 | return retValue; |
145 | } |
146 | |
147 | } |