From ca9ca4446a2bce6c5338bc4b864da1651fdfbc42 Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 23:38:26 +0100 Subject: [PATCH] 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(); }