Skip to content

Commit 92705b7

Browse files
authored
Add files via upload
1 parent f4051c7 commit 92705b7

File tree

10 files changed

+717
-0
lines changed

10 files changed

+717
-0
lines changed

tilePuzzle/Color.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package tilePuzzle;
2+
3+
/**
4+
*
5+
* Enum class to represent color constants
6+
*
7+
* @author angy xajil CS2013-05 CIN: 400304005
8+
*
9+
*/
10+
11+
public enum Color {
12+
/**
13+
* Red, orange, yellow, green, blue, purple, unknown
14+
*/
15+
R, O, Y, G, B, P, K
16+
}

tilePuzzle/Main.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package tilePuzzle;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* This class outputs a menu for the user to select which puzzle they would like
7+
* to solve and calls the appropriate method
8+
*
9+
* @author angy xajil CS2013-05; CIN: 400304005
10+
*
11+
*/
12+
13+
public class Main {
14+
15+
public static void main(String[] args) {
16+
// TODO Auto-generated method stub
17+
18+
/**
19+
* declaring a TileSet object
20+
*/
21+
TileSet ts = null;
22+
23+
System.out.println("Which puzzle would you like to solve?" + "\nA: tileset1-1.txt" + "\nB: tileset2-1.txt"
24+
+ "\nC: tileset3-1.txt");
25+
26+
Scanner scan = new Scanner(System.in);
27+
28+
/**
29+
* String read by the scanner
30+
*/
31+
String choice = scan.nextLine();
32+
33+
/**
34+
* declaring a new string option
35+
*/
36+
String option;
37+
38+
/**
39+
* Chosen input is converted to lower case to ensure choice can still be read
40+
* despite the case type (upper or lower)
41+
*/
42+
option = choice.toLowerCase();
43+
44+
/**
45+
* determines output based on chosen file
46+
*/
47+
switch (option) {
48+
49+
case "a":
50+
/**
51+
* instantiating the TileSet object with chosen file
52+
*/
53+
ts = TileSet.LoadTiles("src/txtfiles/tileset1-1.txt");
54+
/**
55+
* boolean true allows all solutions to be printed
56+
*/
57+
ts.Solve(true);
58+
break;
59+
case "b":
60+
ts = TileSet.LoadTiles("src/txtfiles/tileset2-1.txt");
61+
System.out.println("Please wait patiently while the number of solutions are found...");
62+
/**
63+
* boolean value false ensures solutions are not printed (too many!)
64+
*/
65+
ts.Solve(false);
66+
break;
67+
case "c":
68+
ts = TileSet.LoadTiles("src/txtfiles/tileset3-1.txt");
69+
System.out.println("Working...");
70+
ts.Solve(true);
71+
break;
72+
default:
73+
System.out.println("Something went wrong, please try again!");
74+
System.exit(0);
75+
}
76+
77+
scan.close();
78+
79+
}
80+
81+
}

tilePuzzle/Side.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package tilePuzzle;
2+
3+
/**
4+
* Enum class to represent position constants
5+
*
6+
* @author angy xajil CS2013-05 CIN: 400304005
7+
*
8+
*/
9+
public enum Side {
10+
/**
11+
* side a, side b, side c, side d, side f
12+
*/
13+
SA, SB, SC, SD, SE, SF
14+
}

tilePuzzle/Tile.java

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
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

Comments
 (0)