Skip to content

Commit

Permalink
TTT main, display and listener
Browse files Browse the repository at this point in the history
  • Loading branch information
reututy committed Aug 31, 2017
1 parent 1ab206b commit 7085a86
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 1 deletion.
93 changes: 93 additions & 0 deletions src/test/java/il/ac/bgu/cs/bp/bpjs/TicTacToe/TTTDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package il.ac.bgu.cs.bp.bpjs.TicTacToe;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Point;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import il.ac.bgu.cs.bp.bpjs.bprogram.runtimeengine.BProgram;

/**
* Class that implements the display of the game
*/
public class TTTDisplay {
private JFrame window = new JFrame("Tic-Tac-Toe");
public JButton buttons[][] = new JButton[3][];
public JLabel message = new JLabel();

/**
* Constructor.
*/

public TTTDisplay(BProgram bp) {

// Create window
window.setSize(150, 150);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
window.setLocation(new Point(550,100));

// The board
JPanel board = new JPanel();
board.setLayout(new GridLayout(3, 3));
// The message label
message.setHorizontalAlignment(SwingConstants.CENTER);

// Create buttons
for (int i = 0; i < 3; i++) {
buttons[i] = new JButton[3];
for (int j = 0; j < 3; j++) {
buttons[i][j] = new TTTButton(i, j);
board.add(buttons[i][j]);
// buttons[i][j].addActionListener(this);
buttons[i][j].setEnabled(false);
}
}

// Add the board and the message component to the window
window.add(board, BorderLayout.CENTER);
window.add(message, BorderLayout.SOUTH);

// Make the window visible
//window.setVisible(true);
}

// /**
// * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
// */
// public void actionPerformed(ActionEvent a) {
// final TTTButton btt = ((TTTButton) a.getSource());
// BThread sc = new ClickHandler(btt.row,btt.col);
// bp.add(sc,20.0);
// sc.startBThread();
//
// }

/**
* A button that remembers its position on the board
*/
@SuppressWarnings("serial")
class TTTButton extends JButton {
int row;
int col;

/**
* Constructor.
*
* @param row
* The row of the button.
* @param col
* The column of the button.
*/
public TTTButton(int row, int col) {
super();
this.row = row;
this.col = col;
}
}
}
94 changes: 94 additions & 0 deletions src/test/java/il/ac/bgu/cs/bp/bpjs/TicTacToe/TTTListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package il.ac.bgu.cs.bp.bpjs.TicTacToe;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import il.ac.bgu.cs.bp.bpjs.bprogram.runtimeengine.BProgram;

/**
* Class that implements the Graphical User Interface for the game
*/
public class TTTListener implements ActionListener {
private JFrame window = new JFrame("Tic-Tac-Toe");
public JButton buttons[][] = new JButton[3][];
public JLabel message = new JLabel();

/**
* Constructor.
*/

public TTTListener(BProgram bp) {

// Create window
window.setSize(150, 150);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
window.setLocation(new Point(600,100));

// The board
JPanel board = new JPanel();
board.setLayout(new GridLayout(3, 3));
// The message label
message.setHorizontalAlignment(SwingConstants.CENTER);

// Create buttons
for (int i = 0; i < 3; i++) {
buttons[i] = new JButton[3];
for (int j = 0; j < 3; j++) {
buttons[i][j] = new TTTButton(i, j);
board.add(buttons[i][j]);
buttons[i][j].addActionListener(this);
}
}

// Add the board and the message component to the window
window.add(board, BorderLayout.CENTER);
window.add(message, BorderLayout.SOUTH);

// Make the window visible
window.setVisible(true);
}

/**
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent a) {
// final TTTButton btt = ((TTTButton) a.getSource());
// BThread sc = new ClickHandler(btt.row,btt.col);
// bp.add(sc,20.0);
// sc.startBThread();
}

/**
* A button that remembers its position on the board
*/
@SuppressWarnings("serial")
class TTTButton extends JButton {
int row;
int col;

/**
* Constructor.
*
* @param row
* The row of the button.
* @param col
* The column of the button.
*/
public TTTButton(int row, int col) {
super();
this.row = row;
this.col = col;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package il.ac.bgu.cs.bp.bpjs.TicTacToe;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.*;
import javax.swing.*;

import il.ac.bgu.cs.bp.bpjs.bprogram.runtimeengine.BProgram;
import il.ac.bgu.cs.bp.bpjs.bprogram.runtimeengine.BProgramRunner;
import il.ac.bgu.cs.bp.bpjs.bprogram.runtimeengine.SingleResourceBProgram;
import il.ac.bgu.cs.bp.bpjs.bprogram.runtimeengine.listeners.StreamLoggerListener;

class TicTacToeGameMain extends JFrame {

// GUI for interactively playing the game
public static TTTListener TTTlistener;

// Add GUI for watching the model-checking run.
public static TTTDisplay TTTdisplay;

public static void main(String[] args) throws InterruptedException {
// Create a program
final SingleResourceBProgram bprog = new SingleResourceBProgram("BPJSTicTacToe.js");
JFrame f = new TicTacToeGameMain();
//f.setVisible(true);

TTTdisplay = new TTTDisplay(bprog);
TTTlistener = new TTTListener(bprog);

BProgramRunner rnr = new BProgramRunner(bprog);
rnr.addListener( new StreamLoggerListener() );
rnr.start();

}

}

@SuppressWarnings("serial")
class TTTButton extends JButton {
int row;
int col;

/**
* Constructor.
*
* @param row
* The row of the button.
* @param col
* The column of the button.
*/
public TTTButton(int row, int col) {
super();
this.row = row;
this.col = col;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static java.util.function.Function.identity;
import java.util.stream.Collectors;

public class TicTacToeMain {
public class TicTacToeVerMain {

public static void main(String[] args) throws InterruptedException {
// Create a program
Expand Down

0 comments on commit 7085a86

Please sign in to comment.