From 2f20f32a5f16b4a4708a601b7326efafeb395f11 Mon Sep 17 00:00:00 2001 From: Sherwin Price Date: Thu, 5 Feb 2026 15:03:28 -0500 Subject: [PATCH] add user tracking on board. --- .gitattributes | 12 +++ .gitignore | 8 ++ lib/build.gradle | 7 ++ lib/src/main/java/edu/tictactoe/App.java | 84 ++++++++++--------- lib/src/main/java/edu/tictactoe/Board.java | 29 ++++++- .../main/java/edu/tictactoe/BoardButton.java | 6 +- 6 files changed, 104 insertions(+), 42 deletions(-) create mode 100644 .gitattributes create mode 100644 .gitignore diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..f91f646 --- /dev/null +++ b/.gitattributes @@ -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 + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..68f70f3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Ignore Gradle project-specific cache directory +.gradle + +# Ignore Gradle build output directory +build + +# Ignore bin +bin diff --git a/lib/build.gradle b/lib/build.gradle index 1b4701a..eb5e303 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -3,11 +3,18 @@ plugins { // Apply the java-library plugin for API and implementation separation. + id 'java' id 'application' id("org.owasp.dependencycheck") version "12.2.0" } +java { + toolchain { + languageVersion = JavaLanguageVersion.of(25) + } +} + group = 'edu.tictactoe' version = '1.0' diff --git a/lib/src/main/java/edu/tictactoe/App.java b/lib/src/main/java/edu/tictactoe/App.java index e94deea..65bb6b5 100644 --- a/lib/src/main/java/edu/tictactoe/App.java +++ b/lib/src/main/java/edu/tictactoe/App.java @@ -1,54 +1,60 @@ package edu.tictactoe; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.awt.image.BufferedImage; - -import javax.swing.ImageIcon; public final class App { // main function public static void main(String[] args) throws Exception { - { - EventQueue.invokeLater(() -> { - // 1. Create the Frame - Frame frame = new Frame("TicTacToe"); + EventQueue.invokeLater(() -> { + // Create the Frame + Frame frame = new Frame("TicTacToe"); + App.Initialize(frame); + }); + } - // 2. Set the layout manager to GridLayout (3 rows, 2 columns) - frame.setLayout(new GridLayout(3, 3)); - frame.setEnabled(false); - Board board = new Board(frame); - BufferedImage blankImage = BoardButton.getBlankImage(); - - frame.add(new BoardButton("0", new ImageIcon(blankImage), board)); - frame.add(new BoardButton("1", new ImageIcon(blankImage), board)); - frame.add(new BoardButton("2", new ImageIcon(blankImage), board)); - frame.add(new BoardButton("3", new ImageIcon(blankImage), board)); - frame.add(new BoardButton("4", new ImageIcon(blankImage), board)); - frame.add(new BoardButton("5", new ImageIcon(blankImage), board)); - frame.add(new BoardButton("6", new ImageIcon(blankImage), board)); - frame.add(new BoardButton("7", new ImageIcon(blankImage), board)); - frame.add(new BoardButton("8", new ImageIcon(blankImage), board)); + final static void Initialize(Frame frame) { + // Set the layout manager to GridLayout (3 rows, 3 columns) + frame.setLayout(new GridLayout(3, 3)); + frame.setEnabled(false); - // 4. Set frame properties - frame.setSize(480, 480); // Set an initial size - frame.pack(); - frame.setResizable(false); - frame.setLocationRelativeTo(null); // Center - frame.setEnabled(true); - frame.setVisible(true); // Make the frame visible - + Board board = new Board(frame); + // board Set butttons in grid + board.setup(); - // 5. Add a window listener to handle the close button - frame.addWindowListener(new WindowAdapter() { - @Override - public void windowClosing(WindowEvent e) { - System.exit(0); - } - }); - }); + // Set Menubar for new game + MenuBar menuBar = new MenuBar(); + Menu gameMenu = new Menu("Game"); + gameMenu.setFont(frame.getFont()); + MenuItem newGame = new MenuItem("New Game"); + gameMenu.add(newGame); + menuBar.add(gameMenu); + frame.setMenuBar(menuBar); - } + // Set frame properties + frame.setSize(480, 480); // Set an initial size + frame.pack(); + frame.setResizable(false); + frame.setLocationRelativeTo(null); // Center + frame.setEnabled(true); + frame.setVisible(true); // Make the frame visible + + // Add a window listener to handle the close button + frame.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + System.exit(0); + } + }); + + newGame.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + frame.removeAll(); + App.Initialize(frame); + } + }); } } \ No newline at end of file diff --git a/lib/src/main/java/edu/tictactoe/Board.java b/lib/src/main/java/edu/tictactoe/Board.java index a67a789..32ed964 100644 --- a/lib/src/main/java/edu/tictactoe/Board.java +++ b/lib/src/main/java/edu/tictactoe/Board.java @@ -4,19 +4,42 @@ import java.util.Random; import java.util.Timer; import java.util.TimerTask; +import javax.swing.ImageIcon; + import java.awt.Frame; +import java.awt.image.BufferedImage; public final class Board { final int User = 1; final int System = 0; final Timer timer = new Timer(); - + final BoardButton[] buttons = new BoardButton[9]; Frame frame = null; - public Board(Frame frame) { + public Board(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() { Random random = new Random(); // Generate a random integer from 0 to 8. @@ -45,6 +68,8 @@ class SystemPlayTask extends TimerTask { @Override public void run() { System.out.println("Task executed at: " + new java.util.Date()); + int iplay = Board.getRandomPlay(); + board.getButtons()[iplay].doClick(); board.setEnabledUIContainer(true); } } diff --git a/lib/src/main/java/edu/tictactoe/BoardButton.java b/lib/src/main/java/edu/tictactoe/BoardButton.java index 7a344be..13d8691 100644 --- a/lib/src/main/java/edu/tictactoe/BoardButton.java +++ b/lib/src/main/java/edu/tictactoe/BoardButton.java @@ -15,6 +15,7 @@ public class BoardButton extends JButton implements ActionListener { private static final BufferedImage blankImage = ResourceImageLoader.loadImageFromResource("/blank.png"); final Board board; + Boolean played = false; public BoardButton(String label, ImageIcon imageIcon, Board board) { super(label, imageIcon); @@ -37,8 +38,11 @@ public class BoardButton extends JButton implements ActionListener { @Override public void actionPerformed(ActionEvent e) { + if(this.played) return; + this.setIcon(new ImageIcon(xImage)); - + this.played = true; + board.PlayEventUpdate(); } }