package example9_2_BlocksMania; import javax.swing.*; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * Class that manages the GUI for Barnyard Simon. *

Barnyard Simon is a game similar to the electronic Simon memory game * marketed by the Milton Bradley Company. *

The game has four colored buttons (red, green, blue, and yellow). The * buttons are activated in a randomly chosen sequence. The player must then * mimic the sequence in order to continue playing the game. The sequence begins * with just one button and increases in length by one each time the player * successfully mimics the sequence. *

Traditionally, the activation of each button results in the button being * lit and a tone (unique for each button) being played for a short amount of time. * Barnyard Simon differs in that the sounds associated with each button are * barnyard animal sounds. * @author FIX * @version 2010-01-10 * @created 15-Jan-2014 6:47:12 PM */ public class BlocksMania extends JFrame { public static void main(String[] args){ new BlocksMania(); } /** * This class listens solely for game play buttons to be pressed. Once pressed, * this class determines which button was pressed and then advances game play * appropriately. * @author FIX * @version 2010-01-10 * @created 15-Jan-2014 6:47:12 PM */ public class HandleGameButton implements ActionListener { /** * Handles an action event caused by one of the four colored buttons in Barnyard * Simon. *

The method first determines if the correct button was pressed. *

If not, the game has ended and the method must ensure that the high score * is updated (if necessary) and resets the label on the start button and enables * it. *

If the correct button is pressed, then the button is activated and the * current count of how many buttons in the sequence the player has pressed * correctly must be incremented. If the player has pressed all of the buttons in * the sequence correctly, the method must ensure that the sequence with one * additional entry be played. * * @param event */ @Override public void actionPerformed(ActionEvent event){ } } /** * This class listens solely to the start button component of the GUI. * @author FIX * @version 2010-01-10 * @created 15-Jan-2014 6:47:12 PM */ public class StartGame implements ActionListener { /** * Initializes and starts a game of Barnyard Simon. *

This method disables the start button and sets the text to "Playing." It * also creates a new sequence and calls playSequence to initiate game play. * * @param event */ @Override public void actionPerformed(ActionEvent event){ } } /** * GUI Component used to initiate a new game. */ private JButton btnStart; /** * The buttons used for game play. */ private Tile buttons[]; /** * The player's current location in the sequence. */ private int countCorrect; /** * Height of the window */ private static final int HEIGHT = 220; /** * Highest score obtained during this running of the program. */ private int highScore = 0; /** * A random number generator used to select the sequence in which the buttons are * activated. */ private Random rGen; /** * Text area used to report the score and high score. */ private JTextArea scoreReport; /** * The sequence in which the buttons are activated. */ private List sequence; /** * Auto-generated serial version ID */ private static final long serialVersionUID = -8930955432889780211L; /** * Width of the window */ private static final int WIDTH = 180; // Only used to identify code hard-coded to the four-button version. // Don't use elsewhere. // TODO: Better way to enforce/document? private static final int NUM_BUTTONS = 4; /** * Constructor. Creates game buttons, initializes attributes and the GUI layout. */ public BlocksMania(){ rGen = new Random(); sequence = new ArrayList(); setTitle("Barnyard Simon (yoder)"); setSize(WIDTH, HEIGHT); setLayout(new BorderLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btnStart = new JButton("Start!"); btnStart.addActionListener(new StartGame()); add(btnStart, BorderLayout.NORTH); add(createPlayButtonPanel(), BorderLayout.CENTER); scoreReport = new JTextArea(); scoreReport.setEnabled(false); scoreReport.setDisabledTextColor(Color.BLACK); // TODO: How to use text color instead? add(scoreReport, BorderLayout.SOUTH); setScoreReport(true); readLevel("level.txt"); setVisible(true); } private JPanel createPlayButtonPanel() { JPanel panel = new JPanel(); assert(NUM_BUTTONS == 4); // Following code works only for 4 buttons panel.setLayout(new GridLayout(5, 5)); buttons = new Tile[5*5]; // TODO: use named constants. assert(NUM_BUTTONS == 4); ActionListener gameButtonListener = new HandleGameButton(); for(int i=0; i0) { // TODO: This takes the first column as a tile instead of a row index. // TODO: Or crashes if we leave the +1 in. for(int indCol = 0; indCol