Skip to content

Commit

Permalink
TTT click events
Browse files Browse the repository at this point in the history
  • Loading branch information
geraw committed Sep 3, 2017
1 parent 1793a12 commit a8520c4
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public Set<BEvent> selectableEvents(Set<BSyncStatement> statements, List<BEvent>
.filter( stmt -> stmt!=null )
.map(BSyncStatement::getBlock )
.filter(r -> r != EventSets.none )
.collect( Collectors.toSet() ) );
.collect( toSet() ) );

Set<BEvent> requested = statements.stream()
.filter( stmt -> stmt!=null )
.flatMap( stmt -> stmt.getRequest().stream() )
.collect( Collectors.toSet() );
.collect( toSet() );

// Let's see what internal events are requested and not blocked (if any).
try {
Expand Down
34 changes: 31 additions & 3 deletions src/test/java/il/ac/bgu/cs/bp/bpjs/TicTacToe/TTTDisplayGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,33 @@
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import il.ac.bgu.cs.bp.bpjs.TicTacToe.events.Click;
import il.ac.bgu.cs.bp.bpjs.TicTacToe.events.Move;
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.listeners.BProgramListenerAdapter;
import il.ac.bgu.cs.bp.bpjs.events.BEvent;

/**
* Class that implements the Graphical User Interface for the game
*/
public class TTTDisplayGame implements ActionListener {
private BProgram bp;
private BProgramRunner rnr;

private JFrame window = new JFrame("Tic-Tac-Toe");
public JButton buttons[][] = new JButton[3][];
public JLabel message = new JLabel();

/**
* Constructor.
* @param rnr
*/

public TTTDisplayGame(BProgram BP) {
public TTTDisplayGame(BProgram bp, BProgramRunner rnr) {

bp = BP;
this.bp = bp;
this.rnr = rnr;

// Create window
window.setSize(150, 150);
Expand Down Expand Up @@ -59,18 +68,37 @@ public TTTDisplayGame(BProgram BP) {

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


rnr.addListener( new BProgramListenerAdapter() {

@Override
public void eventSelected(BProgram bp, BEvent theEvent) {
try {
Move mv = (Move)theEvent;
buttons[mv.row][mv.col].setText(mv.displayString());
} catch( ClassCastException cce) {
}


}

});

}

/**
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent a) {
final TTTButton btt = ((TTTButton) a.getSource());
buttons[btt.row][btt.col].setText("X");
//buttons[btt.row][btt.col].setText("X");

// BThread sc = new ClickHandler(btt.row,btt.col);
// bp.add(sc,20.0);
// sc.startBThread();

bp.enqueueExternalEvent(new Click(btt.row,btt.col));
}

/**
Expand Down
62 changes: 38 additions & 24 deletions src/test/java/il/ac/bgu/cs/bp/bpjs/TicTacToe/TicTacToeGameMain.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
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 javax.swing.JButton;
import javax.swing.JFrame;

import org.mozilla.javascript.Scriptable;

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 TTTDisplayGame TTTdisplayGame;

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);

TTTdisplayGame = new TTTDisplayGame(bprog);

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

// bprog.add(new UpdatePlayingGUI(), 0.1);
// bprog.add(new UserMove(), 0.5);

}

}
public static boolean isModelChecking() {
return false;
}

public static void main(String[] args) throws InterruptedException {
// Create a program
BProgram bprog = new SingleResourceBProgram("BPJSTicTacToe.js") {
protected void setupProgramScope(Scriptable scope) {
putInGlobalScope("isModelChecking", false);
super.setupProgramScope(scope);
}
};


bprog.setDaemonMode(true);
JFrame f = new TicTacToeGameMain();
// f.setVisible(true);

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

TTTdisplayGame = new TTTDisplayGame(bprog, rnr);

rnr.start();

System.out.println("end of run");

// bprog.add(new UpdatePlayingGUI(), 0.1);
// bprog.add(new UserMove(), 0.5);

}

}

@SuppressWarnings("serial")
class TTTButton extends JButton {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ public class Click extends BEvent {
* Column of the pressed button
*/
public Click(int row, int col) {
super();
super("Click(" + row + "," + col + ")");
this.row = row;
this.col = col;
this.named("Click(" + row + "," + col + ")");
// this.name = "Click(" + row + "," + col + ")";
// this.setName("Click(" + row + "," + col + ")");
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/il/ac/bgu/cs/bp/bpjs/TicTacToe/events/Move.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class Move extends BEvent {
public int row;
public int col;

public Move(int row, int col) {
super();
public Move(int row, int col, String type) {
super(type + "(" + row + "," + col + ")");
this.row = row;
this.col = col;
}
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/il/ac/bgu/cs/bp/bpjs/TicTacToe/events/O.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ public class O extends Move {
* Constructor.
*/
public O(int row, int col) {
super(row, col);
this.named("O(" + row + "," + col + ")");
//this.setName("O(" + row + "," + col + ")");
super(row, col, "O");
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/il/ac/bgu/cs/bp/bpjs/TicTacToe/events/X.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ public class X extends Move {
* Constructor.
*/
public X(int row, int col) {
super(row, col);
this.named("X(" + row + "," + col + ")");
//this.setName("X(" + row + "," + col + ")");
super(row, col, "X");
}

/**
Expand Down
Loading

0 comments on commit a8520c4

Please sign in to comment.