Question: BoggleUi.java Add member variables Primitive data type int for the players score Write an inner class to create an ActionListener that is registered to the
BoggleUi.java Add member variables Primitive data type int for the players score Write an inner class to create an ActionListener that is registered to the JButton with text Submit Word; it shouldValidate if the word can be used based on the dictionary text file provided if it is not included in the dictionary notify the user the word was not valid and do not add it to the JTextPane if it is included in the dictionary, update the JTextPane by adding the word from the JLabel representing the Current Word Clear the JLabel representing the Current Word Write an inner class to create an ActionListener that is registered to the 16 JButtons that represent the dice on the board; when the JButton is clicked it should Update the JLabel representing the current word with the letter on the die and concatenate it to existing text Update the UI so only the available letters are enabled, all other letters are disabled based on the rules of Boggle; reference the PDF document for the rules of the game. Update the inner class that created the ActionListener for the javax.swing.Timer event handler; it should do the following: Stop the timer Randomly determines how many of the players word the computer found as well Randomly select which words of the players were found by the computer Strike through the words in the JTextPane Update the JLabel representing the players score for their final game score package boggle; import core.Board; import inputOutput.ReadDataFile; import java.util.ArrayList; import javax.swing.JOptionPane; import userInterface.BoggleUi; public class Boggle { // // Array list to store data value of each die private static ArrayList boggleData = new ArrayList(); // // // Array list to store data of the dictionary file // private static ArrayList dictionaryData = new ArrayList(); // name of the Boggle data file using relative pathing private static String dataFileName = new String("../data/BoggleData.txt"); // name of the dictionary file using relative pathing private static String dictionayFileName = new String("../data/Dictionary.txt"); /** * @param args the command line arguments */ public static void main(String[] args) { System.out.println("Welcome to Boggle!"); JOptionPane.showMessageDialog(null, "Let's Play Boggle!"); // read in the dice data file ReadDataFile data = new ReadDataFile(dataFileName); data.populateData(); //read in the dictionary data file ReadDataFile dictionary = new ReadDataFile(dictionayFileName); dictionary.populateData(); // create instance of Board passing the boggleData Board board = new Board(data.getData(), dictionary.getData()); board.populateDice(); System.out.println("There are " + dictionary.getData().size() + " entries in the dictionary"); board.shakeDice(); board.displayGameData(); boggleData = board.getGameDice(); BoggleUi ui = new BoggleUi(board); } } ------------------------Board.java---------------------------------- package core; import java.util.ArrayList; import java.util.Random; public class Board implements IBoard { // stores the letter data from the data file private ArrayList boggleData; // stores the dictionary data private ArrayList dictionaryData; // collection of 16 dice private ArrayList boggleDice; // collection of 16 dice with dice and letters randomized private ArrayList gameDice; // keep track of which die has been used public Board(ArrayList diceData, ArrayList dictionary) { boggleData = diceData; dictionaryData = dictionary; boggleDice = new ArrayList(); } @Override public void shakeDice() { gameDice = new ArrayList(); ArrayList used = new ArrayList(); // randomize the dice // get random letter for each die while(used.size() < NUMBER_OF_DICE) { // randomly select a die 0 - 15 int index = getRandomDie(); if (!used.contains(index)) { Die die = boggleDice.get(index); gameDice.add(die.rollDie()); used.add(new Integer(index)); // System.out.println("used die " + index); } } } private int getRandomDie() { // randomly select a die from the 16 dice Random random = new Random(); int value = random.nextInt(NUMBER_OF_DICE); return value; } @Override public void populateDice() { Die die; int counter = 0; // loop 16 times for each die for(int dice = 0; dice < NUMBER_OF_DICE; dice++) { // create an instance of Die die = new Die(); // add 6 items of the array list to populate the die sides for (int side = 0; side < die.NUMBER_OF_SIDES; side++) { die.addLetter(boggleData.get(counter)); counter++; } // Temporary for Assignment 1 System.out.print("Die " + dice + ": "); die.displayLetters(); System.out.println(); boggleDice.add(die); } } /** * @return the gameDice */ public ArrayList getGameDice() { return gameDice; } public void displayGameData() { int counter = 0; // create an instance of class String, locally called value // loop through the contents of container names letters for(String value : gameDice) { System.out.print(value + " "); counter++; if(counter % 4 == 0) System.out.println(); } } } ----------------------------------------Die.java----------------------------------- package core; import java.util.ArrayList; import java.util.Random; c class Die implements IDie { // create the array for the letters private ArrayList letters = new ArrayList(); private String letter; @Override public void displayLetters() { // create an instance of class String, locally called value // loop through the contents of container names letters for(String value : letters) { System.out.print(value + " "); } } @Override public void addLetter(String letter) { letters.add(letter); } @Override public String rollDie() { Random random = new Random(); int value = random.nextInt(NUMBER_OF_SIDES); letter = letters.get(value); return letter; } } ---------------------IBoard.java---------------------------------------------- package core; public interface IBoard { // each board has 16 dice in a 4 x 4 layout public static final int NUMBER_OF_DICE = 16; public static final int GRID = 4; // this method will invoke the rollDie method for each of the 16 dice in the game public void shakeDice(); // this method will add the data to the 16 dice public void populateDice(); } -----------------------idie.java------------------------------------ public interface IDie { // this is a constant public static final int NUMBER_OF_SIDES = 6; // this method will display the letters of the six sides of the die public void displayLetters(); // this method will add a letter to the die public void addLetter(String letter); // this method will return the current letter of the die public String rollDie(); } ----------------------------Data.txt------------------------------------------ D R L X E I C P O H S A N H N L Z R W T O O T A I O S S E T N W E G H E B O O J A B U I E N E S P S A F K F I U N H M Qu Y R D V E L V E H W H R I O T M U C T Y E L T R S T I T Y D A G A E E N -----------------------ireaddatafile.java--------------------------------------------- public interface IReadDataFile { // method to read a data file and populate an ArrayList public void populateData(); } ---------------------- readdatafile.java-------------------------- public class ReadDataFile implements IReadDataFile { // instance variables private Scanner inputFile; private String dataFileName; private ArrayList data; public ReadDataFile(String fileName) { dataFileName = fileName; data = new ArrayList(); } @Override public void populateData() { try { URL url = getClass().getResource(dataFileName); File file = new File(url.toURI()); inputFile = new Scanner(file); while(inputFile.hasNext()) { data.add(inputFile.next()); } } catch(IOException | URISyntaxException ex) { System.out.println(ex.toString()); ex.printStackTrace(); } finally { if(inputFile != null) inputFile.close(); } } /** * @return the data */ public ArrayList getData() { return data; } } -------------------------boggleUi.java------------------------------------- package userInterface; import core.Board; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.* public class BoggleUi { private JFrame frame; private JMenuBar menuBar; private JMenu game; private JMenuItem exit; private JMenuItem newGame; // Boggle board private JPanel bogglePanel; private JButton[][] diceButtons; // Enter found words private JPanel wordsPanel; private JScrollPane scrollPane; private JTextPane wordsArea; // time label private JLabel timeLabel; private JButton shakeDice; // Enter current word private JPanel currentPanel; private JLabel currentLabel; private JButton currentSubmit; // player's score private JLabel scoreLabel; // class Board reference object private Board board; // ResetGameListener private ResetGameListener reset; //Timer private Timer timer; private int minutes = 3; private int seconds = 0; public BoggleUi(Board inBoard) { board = inBoard; reset = new ResetGameListener(); initComponents(); } private void initComponents() { // Initialize the JFrame frame = new JFrame("Boggle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(660, 500); // Initialize the JMenuBar and add to the JFrame createMenu(); // Initialize the JPane for the current word setupCurrentPanel(); // Initialize the JPanel for the word entry setupWordPanel(); // Initialize the JPanel for the Boggle dice setupBogglePanel(); // initialize the Timer setupTimer(); // Add everything to the JFrame frame.setJMenuBar(menuBar); frame.add(bogglePanel, BorderLayout.WEST); frame.add(wordsPanel, BorderLayout.CENTER); frame.add(currentPanel, BorderLayout.SOUTH); frame.setVisible(true); } private void createMenu() { menuBar = new JMenuBar(); game = new JMenu("Boggle"); game.setMnemonic('B'); newGame = new JMenuItem("New Game"); newGame.setMnemonic('N'); newGame.addActionListener(reset); exit = new JMenuItem("Exit"); exit.setMnemonic('E'); exit.addActionListener(new ExitListener()); game.add(newGame); game.add(exit); menuBar.add(game); } private void setupCurrentPanel() { currentPanel = new JPanel(); currentPanel.setBorder(BorderFactory.createTitledBorder("Current Word")); currentLabel = new JLabel(); currentLabel.setBorder(BorderFactory.createTitledBorder("Current Word")); currentLabel.setMinimumSize(new Dimension(300, 50)); currentLabel.setPreferredSize(new Dimension(300,50)); currentLabel.setHorizontalAlignment(SwingConstants.LEFT); currentSubmit = new JButton("Submit Word"); currentSubmit.setMinimumSize(new Dimension(200, 100)); currentSubmit.setPreferredSize(new Dimension(200, 50)); scoreLabel = new JLabel(); scoreLabel.setBorder(BorderFactory.createTitledBorder("Score")); scoreLabel.setMinimumSize(new Dimension(100, 50)); scoreLabel.setPreferredSize(new Dimension(100,50)); currentPanel.add(currentLabel); currentPanel.add(currentSubmit); currentPanel.add(scoreLabel); } private void setupWordPanel() { wordsPanel = new JPanel(); wordsPanel.setLayout(new BoxLayout(wordsPanel, BoxLayout.Y_AXIS)); wordsPanel.setBorder(BorderFactory.createTitledBorder("Enter Words Found")); wordsArea = new JTextPane(); scrollPane = new JScrollPane(wordsArea); scrollPane.setPreferredSize(new Dimension(180, 330)); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); timeLabel = new JLabel("3:00"); timeLabel.setHorizontalAlignment(SwingConstants.CENTER); timeLabel.setFont(new Font("Serif", Font.PLAIN, 48)); timeLabel.setPreferredSize(new Dimension(240, 100)); timeLabel.setMinimumSize(new Dimension(240, 100)); timeLabel.setMaximumSize(new Dimension(240, 100)); timeLabel.setBorder(BorderFactory.createTitledBorder("Time Left")); shakeDice = new JButton("Shake Dice"); shakeDice.setPreferredSize(new Dimension(240, 100)); shakeDice.setMinimumSize(new Dimension(240, 100)); shakeDice.setMaximumSize(new Dimension(240, 100)); shakeDice.addActionListener(reset); wordsPanel.add(scrollPane); wordsPanel.add(timeLabel); wordsPanel.add(shakeDice); } private void setupBogglePanel() { // counter for the ArrayList of the 16 letters int counter = 0; // get new letters for the game board.shakeDice(); // set up the board for the UI bogglePanel = new JPanel(); bogglePanel.setLayout(new GridLayout(4, 4)); bogglePanel.setMinimumSize(new Dimension(400, 400)); bogglePanel.setPreferredSize(new Dimension(400, 400)); bogglePanel.setBorder(BorderFactory.createTitledBorder("Boggle Board")); diceButtons = new JButton[Board.GRID][Board.GRID]; for(int row = 0; row < Board.GRID; row++) for(int col = 0; col < Board.GRID; col++) { diceButtons[row][col] = new JButton(); diceButtons[row][col].setText(board.getGameDice().get(counter)); bogglePanel.add(diceButtons[row][col]); counter++; } } private void setupTimer() { timer = new Timer(1000, new TimerListener()); timer.start(); } private void changeDice() { // counter for the ArrayList of the 16 letters int counter = 0; // get new letters for the game board.shakeDice(); for(int row = 0; row < Board.GRID; row++) for(int col = 0; col < Board.GRID; col++) { diceButtons[row][col].setText(board.getGameDice().get(counter)); counter++; } } // inner classes private class ExitListener implements ActionListener { @Override public void actionPerformed(ActionEvent ae) { int response = JOptionPane.showConfirmDialog(null, "Confirm to exit Boggle?", "Exit?", JOptionPane.YES_NO_OPTION); if (response == JOptionPane.YES_OPTION) System.exit(0); } } private class TimerListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if(seconds == 0 && minutes == 0) { timer.stop(); } else { if(seconds == 0) { seconds = 59; minutes--; } else { seconds--; } } if(seconds < 10) { String strSeconds = "0" + String.valueOf(seconds); timeLabel.setText(String.valueOf(minutes) + ":" + strSeconds); } else { timeLabel.setText(String.valueOf(minutes) + ":" + String.valueOf(seconds)); } } } private class ResetGameListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { //this code resets the bogglePanel with new letters changeDice(); //resets text areas to be ready for a new game wordsArea.setText(""); //Words found panel is cleared scoreLabel.setText("0"); //score is reset currentLabel.setText(""); //currentLabel is reset timeLabel.setText("3:00"); //timer is 'reset' // when updating the UI these methods // performs a layout of the container bogglePanel.revalidate(); bogglePanel.repaint(); //restarts timer timer.stop(); minutes = 3; seconds = 0; timer.start(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
