/** * * @author Rob Holman */ public class GameApp extends javax.swing.JFrame implements ActionListener { /** Creates new form GameApp */ //Instance Variables: TicTacToe myBoard = new TicTacToe(); boolean p; private JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9; private JTextField fld1; public GameApp() { initComponents(); this.setSize(new Dimension(180,200)); btn1 = new JButton("-"); btn2 = new JButton("-"); btn3 = new JButton("-"); btn4 = new JButton("-"); btn5 = new JButton("-"); btn6 = new JButton("-"); btn7 = new JButton("-"); btn8 = new JButton("-"); btn9 = new JButton("-"); fld1 = new JTextField("Good Luck!");
// Add buttons to the JFrame:
this.getContentPane().add(btn1); this.getContentPane().add(btn2); this.getContentPane().add(btn3); this.getContentPane().add(btn4); this.getContentPane().add(btn5); this.getContentPane().add(btn6); this.getContentPane().add(btn7); this.getContentPane().add(btn8); this.getContentPane().add(btn9); this.getContentPane().add(fld1); // Register controls with event listener (main) btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); btn4.addActionListener(this); btn5.addActionListener(this); btn6.addActionListener(this); btn7.addActionListener(this); btn8.addActionListener(this); btn9.addActionListener(this); //Instantiate a new game board as an object myBoard.setBoard(); }
public void actionPerformed(ActionEvent e) { // Game Variables JButton pressed = (JButton)e.getSource(); p = myBoard.getPlayer();
// Handle control clicks if (pressed.equals(btn1)) myBoard.makeMove(0, 0); if (pressed.equals(btn2)) myBoard.makeMove(1, 0); if (pressed.equals(btn3)) myBoard.makeMove(2, 0); if (pressed.equals(btn4)) myBoard.makeMove(0, 1); if (pressed.equals(btn5)) myBoard.makeMove(1, 1); if (pressed.equals(btn6)) myBoard.makeMove(2, 1); if (pressed.equals(btn7)) myBoard.makeMove(0, 2); if (pressed.equals(btn8)) myBoard.makeMove(1, 2); if (pressed.equals(btn9)) myBoard.makeMove(2, 2); if (p == true){ pressed.setText("O"); } else if (p == false){ pressed.setText("X"); } pressed.setEnabled(false);
if (myBoard.gameStatus()==1) { fld1.setText("Player 'X' has won!!"); }
if (myBoard.gameStatus()==2) { fld1.setText("Player 'O' has won!!"); }
if (myBoard.gameStatus()==3) { fld1.setText("Game ended in a draw."); }
} /** This method is called from within the constructor to * initialize the form. */ // private void initComponents() {
/** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new GameApp().setVisible(true); } }); } // Variables declaration - do not modify // End of variables declaration
/* CSC 240 Spring 2009
* GameApp.java
* Assignment 2 - Tic Tac Toe
* Created on March 18, 2009, 4:16 PM
*/
//Import Libs Here:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Rob Holman
*/
public class GameApp extends javax.swing.JFrame implements ActionListener {
/** Creates new form GameApp */
//Instance Variables:
TicTacToe myBoard = new TicTacToe();
boolean p;
private JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
private JTextField fld1;
public GameApp() {
initComponents();
this.setSize(new Dimension(180,200));
btn1 = new JButton("-");
btn2 = new JButton("-");
btn3 = new JButton("-");
btn4 = new JButton("-");
btn5 = new JButton("-");
btn6 = new JButton("-");
btn7 = new JButton("-");
btn8 = new JButton("-");
btn9 = new JButton("-");
fld1 = new JTextField("Good Luck!");
// Add buttons to the JFrame:
this.getContentPane().add(btn1);
this.getContentPane().add(btn2);
this.getContentPane().add(btn3);
this.getContentPane().add(btn4);
this.getContentPane().add(btn5);
this.getContentPane().add(btn6);
this.getContentPane().add(btn7);
this.getContentPane().add(btn8);
this.getContentPane().add(btn9);
this.getContentPane().add(fld1);
// Register controls with event listener (main)
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
//Instantiate a new game board as an object
myBoard.setBoard();
}
public void actionPerformed(ActionEvent e) {
// Game Variables
JButton pressed = (JButton)e.getSource();
p = myBoard.getPlayer();
// Handle control clicks
if (pressed.equals(btn1))
myBoard.makeMove(0, 0);
if (pressed.equals(btn2))
myBoard.makeMove(1, 0);
if (pressed.equals(btn3))
myBoard.makeMove(2, 0);
if (pressed.equals(btn4))
myBoard.makeMove(0, 1);
if (pressed.equals(btn5))
myBoard.makeMove(1, 1);
if (pressed.equals(btn6))
myBoard.makeMove(2, 1);
if (pressed.equals(btn7))
myBoard.makeMove(0, 2);
if (pressed.equals(btn8))
myBoard.makeMove(1, 2);
if (pressed.equals(btn9))
myBoard.makeMove(2, 2);
if (p == true){
pressed.setText("O");
}
else if (p == false){
pressed.setText("X");
}
pressed.setEnabled(false);
if (myBoard.gameStatus()==1)
{
fld1.setText("Player 'X' has won!!");
}
if (myBoard.gameStatus()==2)
{
fld1.setText("Player 'O' has won!!");
}
if (myBoard.gameStatus()==3)
{
fld1.setText("Game ended in a draw.");
}
}
/** This method is called from within the constructor to
* initialize the form.
*/
//
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("TicTac");
setResizable(false);
getContentPane().setLayout(new java.awt.FlowLayout());
pack();
}//
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GameApp().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
} //end of Class
/*
Reply
Leave a comment