add user tracking on board.
This commit is contained in:
@@ -0,0 +1,12 @@
|
|||||||
|
#
|
||||||
|
# https://help.github.com/articles/dealing-with-line-endings/
|
||||||
|
#
|
||||||
|
# Linux start script should use lf
|
||||||
|
/gradlew text eol=lf
|
||||||
|
|
||||||
|
# These are Windows script files and should use crlf
|
||||||
|
*.bat text eol=crlf
|
||||||
|
|
||||||
|
# Binary files should be left untouched
|
||||||
|
*.jar binary
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
# Ignore Gradle project-specific cache directory
|
||||||
|
.gradle
|
||||||
|
|
||||||
|
# Ignore Gradle build output directory
|
||||||
|
build
|
||||||
|
|
||||||
|
# Ignore bin
|
||||||
|
bin
|
||||||
@@ -3,11 +3,18 @@
|
|||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
// Apply the java-library plugin for API and implementation separation.
|
// Apply the java-library plugin for API and implementation separation.
|
||||||
|
id 'java'
|
||||||
id 'application'
|
id 'application'
|
||||||
|
|
||||||
id("org.owasp.dependencycheck") version "12.2.0"
|
id("org.owasp.dependencycheck") version "12.2.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
java {
|
||||||
|
toolchain {
|
||||||
|
languageVersion = JavaLanguageVersion.of(25)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
group = 'edu.tictactoe'
|
group = 'edu.tictactoe'
|
||||||
version = '1.0'
|
version = '1.0'
|
||||||
|
|
||||||
|
|||||||
@@ -1,37 +1,40 @@
|
|||||||
package edu.tictactoe;
|
package edu.tictactoe;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.WindowAdapter;
|
import java.awt.event.WindowAdapter;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
|
|
||||||
import javax.swing.ImageIcon;
|
|
||||||
|
|
||||||
public final class App {
|
public final class App {
|
||||||
// main function
|
// main function
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
{
|
|
||||||
EventQueue.invokeLater(() -> {
|
EventQueue.invokeLater(() -> {
|
||||||
// 1. Create the Frame
|
// Create the Frame
|
||||||
Frame frame = new Frame("TicTacToe");
|
Frame frame = new Frame("TicTacToe");
|
||||||
|
App.Initialize(frame);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 2. Set the layout manager to GridLayout (3 rows, 2 columns)
|
final static void Initialize(Frame frame) {
|
||||||
|
// Set the layout manager to GridLayout (3 rows, 3 columns)
|
||||||
frame.setLayout(new GridLayout(3, 3));
|
frame.setLayout(new GridLayout(3, 3));
|
||||||
frame.setEnabled(false);
|
frame.setEnabled(false);
|
||||||
|
|
||||||
Board board = new Board(frame);
|
Board board = new Board(frame);
|
||||||
BufferedImage blankImage = BoardButton.getBlankImage();
|
// board Set butttons in grid
|
||||||
|
board.setup();
|
||||||
|
|
||||||
frame.add(new BoardButton("0", new ImageIcon(blankImage), board));
|
// Set Menubar for new game
|
||||||
frame.add(new BoardButton("1", new ImageIcon(blankImage), board));
|
MenuBar menuBar = new MenuBar();
|
||||||
frame.add(new BoardButton("2", new ImageIcon(blankImage), board));
|
Menu gameMenu = new Menu("Game");
|
||||||
frame.add(new BoardButton("3", new ImageIcon(blankImage), board));
|
gameMenu.setFont(frame.getFont());
|
||||||
frame.add(new BoardButton("4", new ImageIcon(blankImage), board));
|
MenuItem newGame = new MenuItem("New Game");
|
||||||
frame.add(new BoardButton("5", new ImageIcon(blankImage), board));
|
gameMenu.add(newGame);
|
||||||
frame.add(new BoardButton("6", new ImageIcon(blankImage), board));
|
menuBar.add(gameMenu);
|
||||||
frame.add(new BoardButton("7", new ImageIcon(blankImage), board));
|
frame.setMenuBar(menuBar);
|
||||||
frame.add(new BoardButton("8", new ImageIcon(blankImage), board));
|
|
||||||
|
|
||||||
// 4. Set frame properties
|
// Set frame properties
|
||||||
frame.setSize(480, 480); // Set an initial size
|
frame.setSize(480, 480); // Set an initial size
|
||||||
frame.pack();
|
frame.pack();
|
||||||
frame.setResizable(false);
|
frame.setResizable(false);
|
||||||
@@ -39,16 +42,19 @@ public final class App {
|
|||||||
frame.setEnabled(true);
|
frame.setEnabled(true);
|
||||||
frame.setVisible(true); // Make the frame visible
|
frame.setVisible(true); // Make the frame visible
|
||||||
|
|
||||||
|
// Add a window listener to handle the close button
|
||||||
// 5. Add a window listener to handle the close button
|
|
||||||
frame.addWindowListener(new WindowAdapter() {
|
frame.addWindowListener(new WindowAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void windowClosing(WindowEvent e) {
|
public void windowClosing(WindowEvent e) {
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
|
newGame.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
frame.removeAll();
|
||||||
|
App.Initialize(frame);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,19 +4,42 @@ import java.util.Random;
|
|||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
import javax.swing.ImageIcon;
|
||||||
|
|
||||||
import java.awt.Frame;
|
import java.awt.Frame;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
public final class Board {
|
public final class Board {
|
||||||
final int User = 1;
|
final int User = 1;
|
||||||
final int System = 0;
|
final int System = 0;
|
||||||
final Timer timer = new Timer();
|
final Timer timer = new Timer();
|
||||||
|
final BoardButton[] buttons = new BoardButton[9];
|
||||||
Frame frame = null;
|
Frame frame = null;
|
||||||
|
|
||||||
public Board(Frame frame) {
|
public Board(Frame frame) {
|
||||||
this.frame = frame;
|
this.frame = frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setup(){ // user inputs on tictactoe board
|
||||||
|
BufferedImage blankImage = BoardButton.getBlankImage();
|
||||||
|
buttons[0] = new BoardButton("0", new ImageIcon(blankImage), this);
|
||||||
|
buttons[1] = new BoardButton("1", new ImageIcon(blankImage), this);
|
||||||
|
buttons[2] = new BoardButton("2", new ImageIcon(blankImage), this);
|
||||||
|
buttons[3] = new BoardButton("3", new ImageIcon(blankImage), this);
|
||||||
|
buttons[4] = new BoardButton("4", new ImageIcon(blankImage), this);
|
||||||
|
buttons[5] = new BoardButton("5", new ImageIcon(blankImage), this);
|
||||||
|
buttons[6] = new BoardButton("6", new ImageIcon(blankImage), this);
|
||||||
|
buttons[7] = new BoardButton("7", new ImageIcon(blankImage), this);
|
||||||
|
buttons[8] = new BoardButton("8", new ImageIcon(blankImage), this);
|
||||||
|
for (BoardButton button : buttons) {
|
||||||
|
frame.add(button);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public BoardButton[] getButtons() {
|
||||||
|
return buttons;
|
||||||
|
}
|
||||||
|
|
||||||
public static int getRandomPlay() {
|
public static int getRandomPlay() {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
// Generate a random integer from 0 to 8.
|
// Generate a random integer from 0 to 8.
|
||||||
@@ -45,6 +68,8 @@ class SystemPlayTask extends TimerTask {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
System.out.println("Task executed at: " + new java.util.Date());
|
System.out.println("Task executed at: " + new java.util.Date());
|
||||||
|
int iplay = Board.getRandomPlay();
|
||||||
|
board.getButtons()[iplay].doClick();
|
||||||
board.setEnabledUIContainer(true);
|
board.setEnabledUIContainer(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ public class BoardButton extends JButton implements ActionListener {
|
|||||||
private static final BufferedImage blankImage = ResourceImageLoader.loadImageFromResource("/blank.png");
|
private static final BufferedImage blankImage = ResourceImageLoader.loadImageFromResource("/blank.png");
|
||||||
|
|
||||||
final Board board;
|
final Board board;
|
||||||
|
Boolean played = false;
|
||||||
|
|
||||||
public BoardButton(String label, ImageIcon imageIcon, Board board) {
|
public BoardButton(String label, ImageIcon imageIcon, Board board) {
|
||||||
super(label, imageIcon);
|
super(label, imageIcon);
|
||||||
@@ -37,7 +38,10 @@ public class BoardButton extends JButton implements ActionListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if(this.played) return;
|
||||||
|
|
||||||
this.setIcon(new ImageIcon(xImage));
|
this.setIcon(new ImageIcon(xImage));
|
||||||
|
this.played = true;
|
||||||
|
|
||||||
board.PlayEventUpdate();
|
board.PlayEventUpdate();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user