Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA You are given seven hexagon tiles. Each tile has 6 colored segments. Colore

ID: 3785439 • Letter: J

Question

JAVA

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

Explanation / Answer

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class hexgame

{

private hexgame() {

                        initGame();

                        createAndShowGUI();

            }

            public static void main(String[] args)

            {

                        SwingUtilities.invokeLater(new Runnable() {

                                                public void run() {

                                                new hexgame();

                                                }

                                                });

            }

            //constants and global variables

            final static Color COLOURBACK = Color.WHITE;

            final static Color COLOURCELL = Color.ORANGE;       

            final static Color COLOURGRID = Color.BLACK;          

            final static Color COLOURONE = new Color(255,255,255,200);

            final static Color COLOURONETXT = Color.BLUE;

            final static Color COLOURTWO = new Color(0,0,0,200);

            final static Color COLOURTWOTXT = new Color(255,100,255);

            final static int EMPTY = 0;

            final static int BSIZE = 12;

            final static int HEXSIZE = 60;          

            final static int BORDERS = 15;

            final static int SCRSIZE = HEXSIZE * (BSIZE + 1) + BORDERS*3;

            int[][] board = new int[BSIZE][BSIZE];

            void initGame(){

                        hexmech.setXYasVertex(false);

                        hexmech.setHeight(HEXSIZE);

                        hexmech.setBorders(BORDERS);

                        for (int i=0;i<BSIZE;i++) {

                                    for (int j=0;j<BSIZE;j++) {

                                                board[i][j]=EMPTY;

                                    }

                        }

                       

                        board[3][3] = (int)'A';

                        board[4][3] = (int)'Q';

                        board[4][4] = -(int)'B';

            }

            private void createAndShowGUI()

            {

                        DrawingPanel panel = new DrawingPanel();

                       

                        JFrame frame = new JFrame("Hex Testing 4");

                        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

                        Container content = frame.getContentPane();

                        content.add(panel);

                        frame.setSize( (int)(SCRSIZE/1.23), SCRSIZE);

                        frame.setResizable(false);

                        frame.setLocationRelativeTo( null );

                        frame.setVisible(true);

            }

            class DrawingPanel extends JPanel

            {                     

                        public DrawingPanel()

                        {         

                                    setBackground(COLOURBACK);

                                    MyMouseListener ml = new MyMouseListener();           

                                    addMouseListener(ml);

                        }

                        public void paintComponent(Graphics g)

                        {

                                    Graphics2D g2 = (Graphics2D)g;

                                    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                                    g.setFont(new Font("TimesRoman", Font.PLAIN, 20));

                                    super.paintComponent(g2);

                                   

                                    for (int i=0;i<BSIZE;i++) {

                                                for (int j=0;j<BSIZE;j++) {

                                                            hexmech.drawHex(i,j,g2);

                                                }

                                    }

                                   

                                    for (int i=0;i<BSIZE;i++) {

                                                for (int j=0;j<BSIZE;j++) {                                                   

                                                            //if (board[i][j] < 0) hexmech.fillHex(i,j,COLOURONE,-board[i][j],g2);

                                                            //if (board[i][j] > 0) hexmech.fillHex(i,j,COLOURTWO, board[i][j],g2);

                                                            hexmech.fillHex(i,j,board[i][j],g2);

                                                }

                                    }

                                    //g.setColor(Color.RED);

                                    //g.drawLine(mPt.x,mPt.y, mPt.x,mPt.y);

                        }

                        class MyMouseListener extends MouseAdapter        {          //inner class inside DrawingPanel

                                    public void mouseClicked(MouseEvent e) {

                                                int x = e.getX();

                                                int y = e.getY();

                                                //mPt.x = x;

                                                //mPt.y = y;

                                                Point p = new Point( hexmech.pxtoHex(e.getX(),e.getY()) );

                                                if (p.x < 0 || p.y < 0 || p.x >= BSIZE || p.y >= BSIZE) return;

                                               

                                                /* for (int i=0;i<BSIZE;i++) {

                                                            for (int j=0;j<BSIZE;j++) {

                                                                        board[i][j]=EMPTY;

                                                            }

                                                } */

                       

                                                board[p.x][p.y] = (int)'X';

                                                repaint();

                                    }                     

                        }

            }

}

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class hexgame

{

private hexgame() {

                        initGame();

                        createAndShowGUI();

            }

            public static void main(String[] args)

            {

                        SwingUtilities.invokeLater(new Runnable() {

                                                public void run() {

                                                new hexgame();

                                                }

                                                });

            }

            //constants and global variables

            final static Color COLOURBACK = Color.WHITE;

            final static Color COLOURCELL = Color.ORANGE;       

            final static Color COLOURGRID = Color.BLACK;          

            final static Color COLOURONE = new Color(255,255,255,200);

            final static Color COLOURONETXT = Color.BLUE;

            final static Color COLOURTWO = new Color(0,0,0,200);

            final static Color COLOURTWOTXT = new Color(255,100,255);

            final static int EMPTY = 0;

            final static int BSIZE = 12;

            final static int HEXSIZE = 60;          

            final static int BORDERS = 15;

            final static int SCRSIZE = HEXSIZE * (BSIZE + 1) + BORDERS*3;

            int[][] board = new int[BSIZE][BSIZE];

            void initGame(){

                        hexmech.setXYasVertex(false);

                        hexmech.setHeight(HEXSIZE);

                        hexmech.setBorders(BORDERS);

                        for (int i=0;i<BSIZE;i++) {

                                    for (int j=0;j<BSIZE;j++) {

                                                board[i][j]=EMPTY;

                                    }

                        }

                       

                        board[3][3] = (int)'A';

                        board[4][3] = (int)'Q';

                        board[4][4] = -(int)'B';

            }

            private void createAndShowGUI()

            {

                        DrawingPanel panel = new DrawingPanel();

                       

                        JFrame frame = new JFrame("Hex Testing 4");

                        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

                        Container content = frame.getContentPane();

                        content.add(panel);

                        frame.setSize( (int)(SCRSIZE/1.23), SCRSIZE);

                        frame.setResizable(false);

                        frame.setLocationRelativeTo( null );

                        frame.setVisible(true);

            }

            class DrawingPanel extends JPanel

            {                     

                        public DrawingPanel()

                        {         

                                    setBackground(COLOURBACK);

                                    MyMouseListener ml = new MyMouseListener();           

                                    addMouseListener(ml);

                        }

                        public void paintComponent(Graphics g)

                        {

                                    Graphics2D g2 = (Graphics2D)g;

                                    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                                    g.setFont(new Font("TimesRoman", Font.PLAIN, 20));

                                    super.paintComponent(g2);

                                   

                                    for (int i=0;i<BSIZE;i++) {

                                                for (int j=0;j<BSIZE;j++) {

                                                            hexmech.drawHex(i,j,g2);

                                                }

                                    }

                                   

                                    for (int i=0;i<BSIZE;i++) {

                                                for (int j=0;j<BSIZE;j++) {                                                   

                                                            //if (board[i][j] < 0) hexmech.fillHex(i,j,COLOURONE,-board[i][j],g2);

                                                            //if (board[i][j] > 0) hexmech.fillHex(i,j,COLOURTWO, board[i][j],g2);

                                                            hexmech.fillHex(i,j,board[i][j],g2);

                                                }

                                    }

                                    //g.setColor(Color.RED);

                                    //g.drawLine(mPt.x,mPt.y, mPt.x,mPt.y);

                        }

                        class MyMouseListener extends MouseAdapter        {          //inner class inside DrawingPanel

                                    public void mouseClicked(MouseEvent e) {

                                                int x = e.getX();

                                                int y = e.getY();

                                                //mPt.x = x;

                                                //mPt.y = y;

                                                Point p = new Point( hexmech.pxtoHex(e.getX(),e.getY()) );

                                                if (p.x < 0 || p.y < 0 || p.x >= BSIZE || p.y >= BSIZE) return;

                                               

                                                /* for (int i=0;i<BSIZE;i++) {

                                                            for (int j=0;j<BSIZE;j++) {

                                                                        board[i][j]=EMPTY;

                                                            }

                                                } */

                       

                                                board[p.x][p.y] = (int)'X';

                                                repaint();

                                    }                     

                        }

            }

}