JAVA (( please follow the all the hits, and please do not copy and pastte from i
ID: 3786336 • Letter: J
Question
JAVA (( please follow the all the hits, and please do not copy and pastte from internet answers ))
You are given seven hexagon tiles. Each tile has 6 colored segments.
Colored segments can be any color from the following, Red, Blue, Yellow, Green, Orange, Purple. Colors may also repeat on the same hexagon tile. The tiles themselves are also labeled 1 - 7.
You need to place the 7 hexagon tiles on a board such that the first hexagon is placed in the center, and the remaining 6 are placed going around the first. The trick is that adjacent segments of the hexagons must have the same color in order for the placement to be valid. Your program must use recursion to find a possible solution.
The program must read in a description of seven hexagons from a text file. Use a FileChooser the allow to user to select the input file. Each line of the file contains the hexagon tile number, followed by the colors of each segment. The colors are listed in clockwise order.
For example, the following would be an example of the user input for each hexagon in the above solution:
1 PPBGOY
2 GGGGGG
3 PPPPPP
4 ROYGBP
5 PROYGB
6 GOGYGB
7 BGPPOR
for example file read this
adn the solution would be
Solutions:
Solution Number: 1
Position 1: BRPRYO
Position 2: PGBBGB
Position 3: YOYGRB
Position 4: GPGROP
Position 5: ROGYBG
Position 6: RYGOBR
Position 7: PGORPY
Solution Number: 2
Position 1: OBRPRY
Position 2: YPGORP
Position 3: BPGBBG
Position 4: BYOYGR
Position 5: PGPGRO
Position 6: GROGYB
Position 7: RRYGOB
Solution Number: 3
Position 1: YOBRPR
Position 2: BRRYGO
Position 3: PYPGOR
Position 4: GBPGBB
Position 5: RBYOYG
Position 6: OPGPGR
Position 7: BGROGY
Solution Number: 4
Position 1: RYOBRP
Position 2: YBGROG
Position 3: OBRRYG
Position 4: RPYPGO
Position 5: BGBPGB
Position 6: GRBYOY
Position 7: ROPGPG
Solution Number: 5
Position 1: PRYOBR
Position 2: GROPGP
Position 3: GYBGRO
Position 4: GOBRRY
Position 5: ORPYPG
Position 6: BBGBPG
Position 7: YGRBYO
Solution Number: 6
Position 1: RPRYOB
Position 2: OYGRBY
Position 3: PGROPG
Position 4: OGYBGR
Position 5: YGOBRR
Position 6: GORPYP
Position 7: GBBGBP
Explanation / Answer
In this problem,
start with first placing central tile.
try placing a next tile as it is or with rotation.
Call placetiles() recursively for the next positions
If a solution is reached, copy it to the Solutions array, reset the various counters and continue recursion until last tile is in the centre and for 6th tile outside 6 rotations have been tried as well.
To solve this problems we need to define some classes.
Let us list down the the data structures / classes needed
Class Tiles
Array of 7 Hexagons
Class Hexagon
Array of 6 Segments
Each Hexagon has 6 segments and value stored in the segment is a character that represents the color
Rotate()
When you rotate a hexagon, the color positions change
Objects can be created
GivenTiles
Tiles
Read the input fole and for processing save it in this array.
Solutions
Array of ordered Tiles
When TryPlacing reaches count of 6 for outer configuration, a valid solution is complete, and can be placed in Solutions array.
Centre
Hexagon
Tile that is in the centre currently.
TryPlacing
Tiles
This is work in progress while tiles are being placed to check for a solution
CurHexagon
Hexagon
The Hexagon that is being placed currently
CurRotation
Integer
Any hexagon can be rotated 5 times, after 6th rotation you get the same color combinations
CurSegment
Character
Indicates segment no.
CurPosition
Integer
In the solution array which position(Tile) is being filled up currently. Starts with 1 and goes up to 6
FullyPlaced
Boolean
When all 7 tiles have been placed in the centre and rest all positions for placing other tiles are completed this will be false
CentreConfig
Integer
Takes values 1 to 7 as there are 7 possibilities for centre
OuterConfig
Integer
PROCESSING
1. Let us check that input file is a valid file:
-the 1st character is a number, then a space and then each character is a valid color code.
Read the input file in GivenTiles.
2. Let us start Solution with
CentreConfig=0
OuterConfig = 1
Centre=GivenTile[CentreConfig]
CurHexagon=GivenTile[OuterConfig]
PlaceTiles(Centre, CurHexagon, CurPosition,CurRotation)
//CentreColortobeMatched is a character
CentreColortobeMatched=Centre[CurPosition]
HexogonSegmenttoCheck=CurHexagon[CurSegment]
If (CentreColortobeMatched =HexogonSegmenttoCheck)
TryPlacing[CurPosition]=CurHexagon //Placed
If CurPosition = 6
Solution[n] <---TryPlacing //Solution Completed
//initialize counters for next solution to find all possible solutions
CurPosition=1 //for next solution
else
CurPosition=CurPosition+ 1
else rotate()
If CurRotation = 6 {
OuterConfig = OuterConfig +1
CurHexagon=GivenTile[OuterConfig]
}
If (Centre = 6 and OuterConfig = 6 and CurRotation =6)
//Keep track of Centre tile with an integer, there are 7 different centres possible.
FullyPlaced = True
Whille FullyPlaced = False
PlaceTiles(Centre, CurHexagon, CurPosition,CurRotation) //Recursion
Here let's work in the top-down approach.
First let us have the analysis of the solution and pseudocode in place.
Then let us go in the modular way:
1. Only read the given file and display the output.
2. Work on the first working solution only, so everytime a recursiove call is made, display the values that are sent to placeTiles and also the TryPlacing array of solution that is being tried.
Let us check these values in debug mode with each call or probably better to run it completely with the values being displayed on the screen.
3. In a step-by-step approach the solution needs to be taken to all the details in a top-down way.
Class Tiles
Array of 7 Hexagons
This is a set of 7 hexagons.Class Hexagon
Array of 6 Segments
Each Hexagon has 6 segments and value stored in the segment is a character that represents the color
Rotate()
When you rotate a hexagon, the color positions change
Objects can be created
GivenTiles
Tiles
Read the input fole and for processing save it in this array.
Solutions
Array of ordered Tiles
When TryPlacing reaches count of 6 for outer configuration, a valid solution is complete, and can be placed in Solutions array.
Centre
Hexagon
Tile that is in the centre currently.
TryPlacing
Tiles
This is work in progress while tiles are being placed to check for a solution
CurHexagon
Hexagon
The Hexagon that is being placed currently
CurRotation
Integer
Any hexagon can be rotated 5 times, after 6th rotation you get the same color combinations
CurSegment
Character
Indicates segment no.
CurPosition
Integer
In the solution array which position(Tile) is being filled up currently. Starts with 1 and goes up to 6
FullyPlaced
Boolean
When all 7 tiles have been placed in the centre and rest all positions for placing other tiles are completed this will be false
CentreConfig
Integer
Takes values 1 to 7 as there are 7 possibilities for centre
OuterConfig
Integer
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.