JAVA You are given seven hexagon tiles Each tile has 6 color
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
Solution
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();
}
});
}
final static Color COLOURBACK = Color.YELLOW;
final static Color COLOURCELL = Color.ORANGE;
final static Color COLOURGRID = Color.BLUE;
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; //board size.
final static int HEXSIZE = 60; //hex size in pixels
final static int BORDERS = 15;
final static int SCRSIZE = HEXSIZE * (BSIZE + 1) + BORDERS*3; //screen size (vertical dimension).
int[][] board = new int[BSIZE][BSIZE];
void initGame(){
hexmech.setXYasVertex(false); //RECOMMENDED: leave this as FALSE.
hexmech.setHeight(HEXSIZE); //Either setHeight or setSize must be run to initialize the hex
hexmech.setBorders(BORDERS);
for (int i=0;i<BSIZE;i++) {
for (int j=0;j<BSIZE;j++) {
board[i][j]=EMPTY;
}
}
//set up board here
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++) {
hexmech.fillHex(i,j,board[i][j],g2);
}
}
}
class MyMouseListener extends MouseAdapter { 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;
board[p.x][p.y] = (int)\'X\';
repaint();
}
} //end of MyMouseListener class
} // end of DrawingPanel class
}



