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 {
// 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'
+45 -39
View File
@@ -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);
}
});
}
}
+27 -2
View File
@@ -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);
}
}
@@ -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();
}
}