JAVA please follow the all the hits and please do not copy

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

Solution

File: HexagonMain.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JFileChooser;

public class HexagonMain {
   static Hexagon[] places = new Hexagon[7];
   static Set<String> solution = new HashSet<String>(); // Set to store solutions. Set will avoid duplicate solutions

   public static void main(String[] args) {
       Hexagon[] hexagons = new Hexagon[7];
       JFileChooser jFileChooser = new JFileChooser();
       jFileChooser.showOpenDialog(null); //prompts user to select file
       File file = jFileChooser.getSelectedFile();
       if (file != null) {
           try {
               FileReader fr = new FileReader(file);
               BufferedReader br = new BufferedReader(fr);
               String input = null;
               int index = 0;
               while ((input = br.readLine()) != null && index < 7) { //Read from file
                   String[] values = input.split(\" \");
                   hexagons[index++] = new Hexagon(values[1]); //Construct hexagon objects from input data
               }
               br.close();
           } catch (FileNotFoundException e) {
               e.printStackTrace();
           } catch (IOException e) {
               e.printStackTrace();
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
       arrange(hexagons, 0); // call method to place the hexagons
       if (solution.isEmpty()) {
           System.out.println(\"No Solutions found\");
       } else {
           int i = 0;
           for(String str:solution){
               System.out.println(\"Solution Number\" + ++i + \"\ \" + str);
           }
       }

   }

   //method to arrange hexagons, inputs are hexagons and first position to place the hexagon
   public static void arrange(Hexagon[] hexagons, int start) {
       if (start < places.length) {
           for (int i = 0; i < hexagons.length; i++) {
               if (hexagons[i].position == -1) { //find out unused hexagons
                   places[start] = hexagons[i]; //place hexagon in position
                   hexagons[i].position = start; // save the position in hexagon object also
                   if (checkValid()) { //Checks if placement is valid
                       arrange(hexagons, start + 1); //proceeds to place next hexagon
                   }
                   for (int j = 0; j < 5; j++) {
                       hexagons[i].rotate(); //Rotate the current hexagon
                       if (checkValid()) {
                           arrange(hexagons, start + 1);
                       }
                   }
                   hexagons[i].rotate(); //Reset back to initial position
                   places[start] = null; //Remove hexagon from position
                   hexagons[i].position = -1;
               }
           }
       } else {
           if (checkValid()) { //Check to see if final result is valid
               StringBuilder str = new StringBuilder();
               for (Hexagon hex : places) {
                   str.append(hex.toString() + \"\ \");
               }
               solution.add(str.toString()); // Save solution to a set
           }
       }
   }

   //Method to check if any every placement is valid
   public static boolean checkValid() {

       for (int i = 0; i <= 5; i++) {
           if (places[i + 1] != null
                   && places[0].section[i] != places[i + 1].section[(i + 3) % 6]) //comparison based on center hexagon
               return false;
           if (places[i + 1] != null
                   && places[((i + 1) % 6) + 1] != null
                   && places[i + 1].section[(i + 2) % 6] != places[((i + 1) % 6) + 1].section[(i + 5) % 6]) { //comparison of outer hexagons with each other
               return false;
           }
       }
       return true;
   }

}

File: Hexagon.java


public class Hexagon {
  
   char[] section;
   int position = -1;
  
   public Hexagon(String colorString){ //constructor
       section = colorString.toCharArray();
   }
  
   //Method to rotate hexagon
   public void rotate(){
       char temp = section[0];
       for(int i=1;i<section.length;i++){
           section[i-1] = section[i];
       }
       section[section.length -1] = temp;
   }

   @Override
   public String toString() {
       return \"Position \" + (position + 1) + \": \" + new String(section);
   }
  

}

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 se
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 se
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 se
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 se

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site