/* * 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. */ package tictactoe; import java.awt.Graphics; /** * * @author levenick */ public class TTTFrame extends javax.swing.JFrame { Board theBoard; public void paint(Graphics g) { super.paint(g); theBoard.paint(g); } public TTTFrame() { initComponents(); setTitle("T I C T A C T O U G H !"); setSize(300, 300); theBoard = new Board(); setVisible(true); display(); } @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { moveButton = new javax.swing.JButton(); resetButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { formMousePressed(evt); } }); moveButton.setText("Move"); moveButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { moveButtonActionPerformed(evt); } }); resetButton.setText("Reset"); resetButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { resetButtonActionPerformed(evt); } }); 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(1242, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(resetButton) .addComponent(moveButton)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(21, 21, 21) .addComponent(moveButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(resetButton) .addContainerGap(690, Short.MAX_VALUE)) ); pack(); }// //GEN-END:initComponents private void moveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveButtonActionPerformed test(); }//GEN-LAST:event_moveButtonActionPerformed private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed theBoard.reset(); }//GEN-LAST:event_resetButtonActionPerformed private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed int x = evt.getX(); int y = evt.getY(); System.out.println("x = " + x); System.out.println("y = " + y); theBoard.handleClick(x, y); repaint(); display(); if (!theBoard.gameOver()) { theBoard.makeMachinePlay(); } }//GEN-LAST:event_formMousePressed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton moveButton; private javax.swing.JButton resetButton; // End of variables declaration//GEN-END:variables private void test() { if (theBoard.gameOver()) { display(); return; } theBoard.playAtRandom(); display(); repaint(); } private void display() { System.out.println("theBoard = \n" + theBoard); if (theBoard.xWin()) { report("X wins!"); } else if (theBoard.oWin()) { report("Win for O!"); } else if (theBoard.catsGame()) { report("cat's game..."); } } private void report(String s) { System.out.println(s); setTitle(s); } }