/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author davereed
 */
public class WumpusGUI extends javax.swing.JFrame {
    private final static String fileName = "caves.txt";
    private CaveMaze maze;
    
    /**
     * Creates new form WumpusGUI
     */
    public WumpusGUI() {
        initComponents();
        this.startGame();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        outputArea = new javax.swing.JTextArea();
        titleLabel = new javax.swing.JLabel();
        moveLabel = new javax.swing.JLabel();
        moveButton = new javax.swing.JButton();
        tossButton = new javax.swing.JButton();
        repeatButton = new javax.swing.JButton();
        caveNumField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        outputArea.setColumns(20);
        outputArea.setRows(5);
        jScrollPane1.setViewportView(outputArea);

        titleLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        titleLabel.setText("Hunt the Wumpus");

        moveLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        moveLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        moveLabel.setText("cave");

        moveButton.setText("move -->");
        moveButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                moveButtonMouseClicked(evt);
            }
        });

        tossButton.setText("<-- toss");
        tossButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tossButtonMouseClicked(evt);
            }
        });

        repeatButton.setText("Play Again");
        repeatButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                repeatButtonMouseClicked(evt);
            }
        });

        caveNumField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        caveNumField.setText("1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 451, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(moveButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(moveLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                                    .addComponent(caveNumField))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tossButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(repeatButton)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(moveButton)
                            .addComponent(tossButton)
                            .addComponent(caveNumField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(repeatButton)
                            .addComponent(moveLabel))
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void moveButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_moveButtonMouseClicked
        if (this.maze.stillAble() && this.maze.stillWumpi()) {
            int caveNum = Integer.parseInt(this.caveNumField.getText());
            this.outputArea.setText(this.maze.move(caveNum));
            this.update();
        }      
    }//GEN-LAST:event_moveButtonMouseClicked

    private void tossButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tossButtonMouseClicked
        if (this.maze.stillAble() && this.maze.stillWumpi()) {
            int caveNum = Integer.parseInt(this.caveNumField.getText());
            this.outputArea.setText(this.maze.toss(caveNum));
            this.update();
        }
    }//GEN-LAST:event_tossButtonMouseClicked

    private void repeatButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_repeatButtonMouseClicked
    this.startGame();
    }//GEN-LAST:event_repeatButtonMouseClicked

    private void startGame() {
        try {
            this.maze = new CaveMaze(WumpusGUI.fileName);
            this.outputArea.setText("HUNT THE WUMPUS:  "
                    + "Your mission is to explore the maze of caves\n"
                    + "and capture all of the wumpi (without getting yourself mauled).\n"
                    + "To move to an adjacent cave or toss a stun grenade, enter the \n"
                    + "tunnel number in the field and click the move or toss button.");
            this.update();
        } catch (java.io.FileNotFoundException e) {
            this.outputArea.setText("Unable to find " + WumpusGUI.fileName + " data file.");
        }
    }

    private void update() {
        if (this.maze.stillAble() && this.maze.stillWumpi()) {
            this.outputArea.append("\n\n" + this.maze.showLocation());
        } else {
            this.outputArea.append("\n\nGAME OVER");
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(WumpusGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(WumpusGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(WumpusGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(WumpusGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new WumpusGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField caveNumField;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton moveButton;
    private javax.swing.JLabel moveLabel;
    private javax.swing.JTextArea outputArea;
    private javax.swing.JButton repeatButton;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JButton tossButton;
    // End of variables declaration//GEN-END:variables
}
