From f623a8ac2984028005d27b81bf25ae266c56adc3 Mon Sep 17 00:00:00 2001 From: Sherwin Price Date: Thu, 5 Feb 2026 18:33:24 -0500 Subject: [PATCH] add winner function to board. --- lib/src/main/java/edu/tictactoe/App.java | 3 +- lib/src/main/java/edu/tictactoe/Board.java | 109 ++++++++++++++++-- .../main/java/edu/tictactoe/BoardButton.java | 14 +-- .../main/java/edu/tictactoe/BoardFrame.java | 17 +++ 4 files changed, 119 insertions(+), 24 deletions(-) create mode 100644 lib/src/main/java/edu/tictactoe/BoardFrame.java diff --git a/lib/src/main/java/edu/tictactoe/App.java b/lib/src/main/java/edu/tictactoe/App.java index 65bb6b5..c15d067 100644 --- a/lib/src/main/java/edu/tictactoe/App.java +++ b/lib/src/main/java/edu/tictactoe/App.java @@ -11,12 +11,13 @@ public final class App { public static void main(String[] args) throws Exception { EventQueue.invokeLater(() -> { // Create the Frame - Frame frame = new Frame("TicTacToe"); + BoardFrame frame = new BoardFrame("TicTacToe"); App.Initialize(frame); }); } final static void Initialize(Frame frame) { + frame.setTitle("TicTacToe"); // Set the layout manager to GridLayout (3 rows, 3 columns) frame.setLayout(new GridLayout(3, 3)); frame.setEnabled(false); diff --git a/lib/src/main/java/edu/tictactoe/Board.java b/lib/src/main/java/edu/tictactoe/Board.java index 32ed964..e8e29f3 100644 --- a/lib/src/main/java/edu/tictactoe/Board.java +++ b/lib/src/main/java/edu/tictactoe/Board.java @@ -1,26 +1,40 @@ package edu.tictactoe; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; import java.util.Random; import java.util.Timer; import java.util.TimerTask; +import javax.swing.Icon; import javax.swing.ImageIcon; +import javax.swing.JOptionPane; +import java.awt.Component; +import java.awt.EventQueue; import java.awt.Frame; import java.awt.image.BufferedImage; public final class Board { - final int User = 1; - final int System = 0; + final static int PlayerUser = 1; + final static int PlayerSystem = 0; + + private static final BufferedImage xImage = ResourceImageLoader.loadImageFromResource("/x.png"); + private static final BufferedImage naughtImage = ResourceImageLoader.loadImageFromResource("/naught.png"); + final Timer timer = new Timer(); + final BoardButton[] buttons = new BoardButton[9]; + + Integer player = Board.PlayerUser; Frame frame = null; - public Board(Frame frame) { + public Board(Frame frame) { this.frame = frame; } - public void setup(){ // user inputs on tictactoe board + 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); @@ -34,26 +48,97 @@ public final class Board { for (BoardButton button : buttons) { frame.add(button); } + + this.setPlayer(Board.PlayerUser); } public BoardButton[] getButtons() { return buttons; } - public static int getRandomPlay() { + public int getRandomPlay() { Random random = new Random(); // Generate a random integer from 0 to 8. - return random.nextInt(9); + int position = random.nextInt(9); + // Generate another guess if position played. + while (getButtons()[position].played) { + position = random.nextInt(9); + } + ; + + return position; } protected void setEnabledUIContainer(boolean enabled) { this.frame.setEnabled(enabled); } - void PlayEventUpdate() { - long delay = 1800; - frame.setEnabled(false); - timer.schedule(new SystemPlayTask(this), delay); + protected void setPlayerTurnOnBoard() { + // Set turn on board + if (player == Board.PlayerUser) { + player = Board.PlayerSystem; + } else if (player == Board.PlayerSystem) { + player = Board.PlayerUser; + } + } + + protected boolean winnerCheck() { + HashSet checks = new HashSet(9); + if ((buttons[0].played && buttons[4].played && buttons[8].played) + && + buttons[0].player == buttons[4].player + && buttons[4].player == buttons[8].player) { + + return true; + + } + + if ((buttons[2].played && buttons[4].played && buttons[6].played) + && + buttons[2].player == buttons[4].player + && buttons[4].player == buttons[6].player) { + + return true; + + } + return false; + } + + synchronized protected void PlayEventUpdate() { + long delay = 0L; + + if (winnerCheck()) { + + frame.setEnabled(false); + // show Winner Dialog + String message = this.player == Board.PlayerUser ? String.format("You") : String.format("System"); + frame.setTitle("TicTacToe - " + message + " won."); + JOptionPane.showMessageDialog(frame, message + " won.", "Winner!", JOptionPane.WARNING_MESSAGE); + EventQueue.invokeLater(() -> { + ((BoardFrame) frame).fireGameOverWindowEvent("Game over."); + }); + return; + } + + setPlayerTurnOnBoard(); + + if (this.player == Board.PlayerSystem) { + delay = 1400L; + frame.setEnabled(false); + timer.schedule(new SystemPlayTask(this), delay); + } + } + + public int getPlayer() { + return this.player; + } + + public void setPlayer(int player) { + this.player = player; + } + + public Icon setPlayerMark() { + return new ImageIcon(player == Board.PlayerUser ? xImage : naughtImage); } } @@ -67,8 +152,8 @@ class SystemPlayTask extends TimerTask { @Override public void run() { - System.out.println("Task executed at: " + new java.util.Date()); - int iplay = Board.getRandomPlay(); + int iplay = board.getRandomPlay(); + System.out.println("Task execute mark " + iplay + " at: " + new java.util.Date()); 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 13d8691..1d4c8b5 100644 --- a/lib/src/main/java/edu/tictactoe/BoardButton.java +++ b/lib/src/main/java/edu/tictactoe/BoardButton.java @@ -10,12 +10,11 @@ import javax.swing.JButton; public class BoardButton extends JButton implements ActionListener { // Board - private static final BufferedImage xImage = ResourceImageLoader.loadImageFromResource("/x.png"); - private static final BufferedImage naughtImage = ResourceImageLoader.loadImageFromResource("/naught.png"); private static final BufferedImage blankImage = ResourceImageLoader.loadImageFromResource("/blank.png"); final Board board; Boolean played = false; + Integer player = -1; public BoardButton(String label, ImageIcon imageIcon, Board board) { super(label, imageIcon); @@ -24,14 +23,6 @@ public class BoardButton extends JButton implements ActionListener { this.addActionListener(this); } - public static final BufferedImage getXImage() { - return xImage; - } - - public static final BufferedImage getNaughtImage() { - return naughtImage; - } - public static final BufferedImage getBlankImage() { return blankImage; } @@ -40,8 +31,9 @@ public class BoardButton extends JButton implements ActionListener { public void actionPerformed(ActionEvent e) { if(this.played) return; - this.setIcon(new ImageIcon(xImage)); + this.setIcon(board.setPlayerMark()); this.played = true; + this.player = board.player; board.PlayEventUpdate(); } diff --git a/lib/src/main/java/edu/tictactoe/BoardFrame.java b/lib/src/main/java/edu/tictactoe/BoardFrame.java new file mode 100644 index 0000000..d0acd8e --- /dev/null +++ b/lib/src/main/java/edu/tictactoe/BoardFrame.java @@ -0,0 +1,17 @@ +package edu.tictactoe; + +import java.awt.Frame; + +public class BoardFrame extends Frame { + + + public BoardFrame(String title) { + super(title); + } + + // Method to fire game over event + public void fireGameOverWindowEvent(String message) { + this.removeAll(); + App.Initialize(this); + } +}