From 30f5312440b82d75e7d2ea77dbffd062ed9c3cf1 Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 23:31:20 +0100 Subject: [PATCH 1/4] Add Scoreboard class with basic functions --- src/main/java/TicTacToe/Scoreboard.java | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/TicTacToe/Scoreboard.java diff --git a/src/main/java/TicTacToe/Scoreboard.java b/src/main/java/TicTacToe/Scoreboard.java new file mode 100644 index 0000000..b2f4483 --- /dev/null +++ b/src/main/java/TicTacToe/Scoreboard.java @@ -0,0 +1,36 @@ +package TicTacToe; + +import javax.swing.JLabel; + +public class Scoreboard extends JLabel{ + + private static final long serialVersionUID = 1L; + + public int[] playerPoints; + + public Scoreboard(int _playerCount) { + playerPoints = new int[_playerCount]; + } + + public void addPoint(int playerID) { + playerPoints[playerID]++; + updateScores(); + } + + public void subPoint(int playerID) { + playerPoints[playerID]--; + updateScores(); + } + + public void updateScores() { + for(int i = 0; i < playerPoints.length; i++){ + if(i == 0) { + setText("Draws: " + playerPoints[0]); + } else { + setText(getText() + " Player " + i + ": " + playerPoints[i]); + } + } + } + + +} From ca9ca4446a2bce6c5338bc4b864da1651fdfbc42 Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 23:38:26 +0100 Subject: [PATCH 2/4] Show Scoreboard in TicTacToe game --- src/main/java/TicTacToe/Scoreboard.java | 3 ++- src/main/java/TicTacToe/TicTacToeGame.java | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/TicTacToe/Scoreboard.java b/src/main/java/TicTacToe/Scoreboard.java index b2f4483..a9799a5 100644 --- a/src/main/java/TicTacToe/Scoreboard.java +++ b/src/main/java/TicTacToe/Scoreboard.java @@ -10,6 +10,7 @@ public class Scoreboard extends JLabel{ public Scoreboard(int _playerCount) { playerPoints = new int[_playerCount]; + updateScores(); } public void addPoint(int playerID) { @@ -27,7 +28,7 @@ public class Scoreboard extends JLabel{ if(i == 0) { setText("Draws: " + playerPoints[0]); } else { - setText(getText() + " Player " + i + ": " + playerPoints[i]); + setText(getText() + " Player " + i + ": " + playerPoints[i]); } } } diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index acef34d..6d363a9 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -19,11 +19,15 @@ public class TicTacToeGame extends JPanel { public cell[] field; public int playerID = 1; public int turns = 0; + public Scoreboard scoreboard; public TicTacToeGame() { this.setSize(width, height); setLayout(null); initField(); + scoreboard = new Scoreboard(maxPlayers); + scoreboard.setBounds(200, 100, 200, 50); + add(scoreboard); } public static void main(String[] args) { @@ -74,12 +78,14 @@ public class TicTacToeGame extends JPanel { public void endTurn() { if(checkPlayfield() != 0) { JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!"); + scoreboard.addPoint(playerID); resetGame(); } turns++; if(turns >= playFieldSize) { JOptionPane.showMessageDialog(getParent(),"Draw!"); + scoreboard.addPoint(playerID); resetGame(); } From e7468f149851e5a57e95e624880425d2086b7209 Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 23:45:38 +0100 Subject: [PATCH 3/4] Refactor cell, TicTacToe --- .../java/TicTacToe/{cell.java => Cell.java} | 4 ++-- src/main/java/TicTacToe/TicTacToeGame.java | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) rename src/main/java/TicTacToe/{cell.java => Cell.java} (91%) diff --git a/src/main/java/TicTacToe/cell.java b/src/main/java/TicTacToe/Cell.java similarity index 91% rename from src/main/java/TicTacToe/cell.java rename to src/main/java/TicTacToe/Cell.java index 27a3158..2c53223 100644 --- a/src/main/java/TicTacToe/cell.java +++ b/src/main/java/TicTacToe/Cell.java @@ -6,12 +6,12 @@ import java.awt.event.ActionListener; import javax.swing.JButton; -public class cell extends JButton { +public class Cell extends JButton { public int playerID = 0; private TicTacToeGame ttt; - public cell(TicTacToeGame _ttt) { + public Cell(TicTacToeGame _ttt) { ttt = _ttt; setBackground(new Color(255,255,255)); diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index 6d363a9..44aa358 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -16,7 +16,7 @@ public class TicTacToeGame extends JPanel { private static final int maxPlayers = 3; private static final int playFieldSize = 9; - public cell[] field; + public Cell[] field; public int playerID = 1; public int turns = 0; public Scoreboard scoreboard; @@ -25,10 +25,9 @@ public class TicTacToeGame extends JPanel { this.setSize(width, height); setLayout(null); initField(); - scoreboard = new Scoreboard(maxPlayers); - scoreboard.setBounds(200, 100, 200, 50); - add(scoreboard); + initScoreboard(); } + public static void main(String[] args) { JFrame f = new JFrame(); @@ -41,10 +40,10 @@ public class TicTacToeGame extends JPanel { } public void initField() { - field = new cell[playFieldSize]; + field = new Cell[playFieldSize]; for(int i = 0; i < field.length; i++) { - field[i] = new cell(this); + field[i] = new Cell(this); add(field[i]); } @@ -59,6 +58,12 @@ public class TicTacToeGame extends JPanel { } } + private void initScoreboard() { + scoreboard = new Scoreboard(maxPlayers); + scoreboard.setBounds(200, 100, 200, 50); + add(scoreboard); + } + @Override protected void paintComponent(Graphics g) { super.paintComponent(g); @@ -85,7 +90,7 @@ public class TicTacToeGame extends JPanel { turns++; if(turns >= playFieldSize) { JOptionPane.showMessageDialog(getParent(),"Draw!"); - scoreboard.addPoint(playerID); + scoreboard.addPoint(0); resetGame(); } @@ -113,7 +118,7 @@ public class TicTacToeGame extends JPanel { } public void resetGame() { - for (cell c : field) { + for (Cell c : field) { c.reset(); } playerID = 1; From 41b70d270045d59b2ac072081ad35bd9d4b7463c Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 16 Feb 2022 10:54:30 +0100 Subject: [PATCH 4/4] Add Games played to Scoreboard --- src/main/java/TicTacToe/Scoreboard.java | 5 +++++ src/main/java/TicTacToe/TicTacToeGame.java | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/TicTacToe/Scoreboard.java b/src/main/java/TicTacToe/Scoreboard.java index a9799a5..a903129 100644 --- a/src/main/java/TicTacToe/Scoreboard.java +++ b/src/main/java/TicTacToe/Scoreboard.java @@ -24,13 +24,18 @@ public class Scoreboard extends JLabel{ } public void updateScores() { + int gamesPlayed = 0; for(int i = 0; i < playerPoints.length; i++){ if(i == 0) { setText("Draws: " + playerPoints[0]); } else { setText(getText() + " Player " + i + ": " + playerPoints[i]); } + + gamesPlayed += playerPoints[i]; } + setText(getText() + " Games played: " + gamesPlayed); + } diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index 44aa358..7b8fb5a 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -60,7 +60,7 @@ public class TicTacToeGame extends JPanel { private void initScoreboard() { scoreboard = new Scoreboard(maxPlayers); - scoreboard.setBounds(200, 100, 200, 50); + scoreboard.setBounds(150, 100, 300, 50); add(scoreboard); }