1+ package tilePuzzle ;
2+
3+ /**
4+ * This class represents an individual tile and contains the methods necessary
5+ * to assign tile information from the txt file, assign colors, rotate, and
6+ * match the tile data.
7+ *
8+ * @author angy xajil CS2013-05 CIN: 400304005
9+ *
10+ */
11+
12+ public class Tile {
13+
14+ /*
15+ * array to hold all data from the tile sides;
16+ */
17+ private Color [] sideData ;
18+ /**
19+ * integer to hold position number
20+ */
21+ private int pos ;
22+ /**
23+ * integer to hold the tile number
24+ */
25+ private int num ;
26+
27+ /*
28+ * Default constructor sets the sideData array to 6 because our tiles have 6
29+ * sides; setting each side of the tile as color k and setting at position 0
30+ * with no rotations;
31+ */
32+ public Tile () {
33+ sideData = new Color [6 ];
34+ for (int i = 0 ; i < 6 ; i ++) {
35+ sideData [i ] = Color .K ;
36+ }
37+ pos = 0 ;
38+ }
39+
40+ /*
41+ * Copy constructor that takes in a Tile t as a parameter to add to our solution
42+ * copies all side data, tile number, and position number (:
43+ */
44+ public Tile (Tile t ) {
45+ sideData = new Color [6 ];
46+ for (int i = 0 ; i < 6 ; i ++) {
47+ sideData [i ] = t .sideData [i ];
48+ }
49+ pos = t .pos ;
50+ num = t .num ;
51+ }
52+
53+ /**
54+ * Method that takes a String init and an integer v as parameters and assigns
55+ * colors to the array
56+ *
57+ * @param init string from txt file
58+ * @param v is the tile number
59+ */
60+ public void assign (String init , int v ) {
61+ /**
62+ * previously initialized "num" now equals our parameter v which is the tile
63+ * number
64+ */
65+ num = v ;
66+ /**
67+ * String init is now a substring from index of ":" + 2 (where the letters start
68+ * to the end of the string)
69+ */
70+ init = init .substring (init .indexOf (':' ) + 2 , init .length ());
71+ /**
72+ * all commas in that substring are removed
73+ */
74+ init = init .replaceAll ("," , "" );
75+ /*
76+ * reading through the 6 colors and putting them into the sideData array
77+ */
78+ for (int i = 0 ; i < 6 ; i ++) {
79+ switch (init .charAt (i )) {
80+ case 'R' :
81+ sideData [i ] = Color .R ;
82+ break ;
83+ case 'O' :
84+ sideData [i ] = Color .O ;
85+ break ;
86+ case 'Y' :
87+ sideData [i ] = Color .Y ;
88+ break ;
89+ case 'G' :
90+ sideData [i ] = Color .G ;
91+ break ;
92+ case 'B' :
93+ sideData [i ] = Color .B ;
94+ break ;
95+ case 'P' :
96+ sideData [i ] = Color .P ;
97+ break ;
98+ /**
99+ * if color cannot be changed from K, error in file reading or formatting
100+ */
101+ default :
102+ sideData [i ] = Color .K ;
103+ System .out .println ("Error initializing tile colors." );
104+ break ;
105+ }
106+ }
107+
108+ /**
109+ * no position since it has not been placed yet
110+ */
111+ pos = 0 ;
112+ }
113+
114+ /*
115+ * Method that shifts the array left by 1, which rotates our tile
116+ */
117+ void rotate () {
118+ Color LastColor = sideData [5 ];
119+ for (int i = 5 ; i > 0 ; i --) {
120+ sideData [i ] = sideData [i - 1 ];
121+ }
122+ sideData [0 ] = LastColor ;
123+ }
124+
125+ /**
126+ * Method that returns rotation count an integer, updates
127+ *
128+ * @return integer rotation count
129+ */
130+
131+ /**
132+ *
133+ * Method containing a switch case that assigns sideData indexes to sides a - f
134+ * i.e(index 0 is side a, index 1 is side b, ...)
135+ *
136+ * @param s is a side
137+ * @return color at specified index
138+ */
139+ public Color color (Side s ) {
140+ switch (s ) {
141+ case SA :
142+ return sideData [0 ];
143+ case SB :
144+ return sideData [1 ];
145+ case SC :
146+ return sideData [2 ];
147+ case SD :
148+ return sideData [3 ];
149+ case SE :
150+ return sideData [4 ];
151+ case SF :
152+ return sideData [5 ];
153+ default :
154+ return Color .K ;
155+ }
156+ }
157+
158+ /**
159+ * Method that takes 2 tiles objects as parameters (the previously placed tile
160+ * and the center tile) and matches the appropriate sides
161+ *
162+ * Or, if previous tile is in position x, match and place position x + 1
163+ *
164+ * @param prev is the previously placed tile
165+ * @param center is the center tile (position 1)
166+ * @return a boolean value
167+ */
168+ public boolean Match (Tile prev , Tile center ) {
169+ /**
170+ * position of the tile previously placed is set as the condition for the switch
171+ * statement
172+ */
173+ switch (prev .Pos ()) {
174+ /*
175+ * Match position 2 tile side D to previous position tile side A
176+ */
177+ case 1 :
178+ return color (Side .SD ) == center .color (Side .SA );
179+ /**
180+ * Match position 3 tile side E with center position tile side B and side F with
181+ * previous tile side C
182+ */
183+ case 2 :
184+ return color (Side .SE ) == center .color (Side .SB ) && color (Side .SF ) == prev .color (Side .SC );
185+ case 3 :
186+ return color (Side .SF ) == center .color (Side .SC ) && color (Side .SA ) == prev .color (Side .SD );
187+ case 4 :
188+ return color (Side .SA ) == center .color (Side .SD ) && color (Side .SB ) == prev .color (Side .SE );
189+ case 5 :
190+ return color (Side .SB ) == center .color (Side .SE ) && color (Side .SC ) == prev .color (Side .SF );
191+ case 6 :
192+ return color (Side .SC ) == center .color (Side .SF ) && color (Side .SD ) == prev .color (Side .SA );
193+ /**
194+ * in case we are attempting to place an 8th tile
195+ */
196+ case 7 :
197+ System .out .println ("ERROR: Attempted to place tile after last tile." );
198+ return false ;
199+ default :
200+ System .out .println ("ERROR: Attempted to place tile at invalid position " + (prev .Pos () + 1 ) + "." );
201+ }
202+ return false ;
203+ }
204+
205+ /*
206+ * Setter for position value
207+ */
208+ public void Place (int p ) {
209+ pos = p ;
210+ }
211+
212+ /**
213+ * Getter for position value
214+ *
215+ * @return the position of the tile
216+ */
217+ public int Pos () {
218+ return pos ;
219+ }
220+
221+ /**
222+ * Method to remove tiles from the board
223+ */
224+ public void Remove () {
225+ pos = 0 ;
226+ }
227+
228+ /*
229+ * Implements the toString method that return the formatted tile object
230+ */
231+ public String toString () {
232+
233+ String outString = "Position " + pos + ": Tile #" + num + ":" ;
234+
235+ for (int i = 0 ; i < 6 ; i ++) {
236+ outString += "\t " + sideData [i ];
237+ }
238+
239+ outString += "\n " ;
240+
241+ return outString ;
242+ }
243+ }
0 commit comments