add user tracking on board.

This commit is contained in:
2026-02-05 15:03:28 -05:00
parent accd68feff
commit 2f20f32a5f
6 changed files with 104 additions and 42 deletions
+12
View File
@@ -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
+8
View File
@@ -0,0 +1,8 @@
# Ignore Gradle project-specific cache directory
.gradle
# Ignore Gradle build output directory
build
# Ignore bin
bin
+7
View File
@@ -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'
+45 -39
View File
@@ -1,54 +1,60 @@
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(() -> { // Create the Frame
// 1. 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) {
frame.setLayout(new GridLayout(3, 3)); // Set the layout manager to GridLayout (3 rows, 3 columns)
frame.setEnabled(false); frame.setLayout(new GridLayout(3, 3));
Board board = new Board(frame); frame.setEnabled(false);
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));
// 4. Set frame properties Board board = new Board(frame);
frame.setSize(480, 480); // Set an initial size // board Set butttons in grid
frame.pack(); board.setup();
frame.setResizable(false);
frame.setLocationRelativeTo(null); // Center
frame.setEnabled(true);
frame.setVisible(true); // Make the frame visible
// 5. Add a window listener to handle the close button // Set Menubar for new game
frame.addWindowListener(new WindowAdapter() { MenuBar menuBar = new MenuBar();
@Override Menu gameMenu = new Menu("Game");
public void windowClosing(WindowEvent e) { gameMenu.setFont(frame.getFont());
System.exit(0); 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);
}
});
} }
} }
+27 -2
View File
@@ -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,8 +38,11 @@ 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();
} }
} }