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/Scoreboard.java b/src/main/java/TicTacToe/Scoreboard.java new file mode 100644 index 0000000..a903129 --- /dev/null +++ b/src/main/java/TicTacToe/Scoreboard.java @@ -0,0 +1,42 @@ +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]; + updateScores(); + } + + public void addPoint(int playerID) { + playerPoints[playerID]++; + updateScores(); + } + + public void subPoint(int playerID) { + playerPoints[playerID]--; + updateScores(); + } + + 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 acef34d..7b8fb5a 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -16,15 +16,18 @@ 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; public TicTacToeGame() { this.setSize(width, height); setLayout(null); initField(); + initScoreboard(); } + public static void main(String[] args) { JFrame f = new JFrame(); @@ -37,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]); } @@ -55,6 +58,12 @@ public class TicTacToeGame extends JPanel { } } + private void initScoreboard() { + scoreboard = new Scoreboard(maxPlayers); + scoreboard.setBounds(150, 100, 300, 50); + add(scoreboard); + } + @Override protected void paintComponent(Graphics g) { super.paintComponent(g); @@ -74,12 +83,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(0); resetGame(); } @@ -107,7 +118,7 @@ public class TicTacToeGame extends JPanel { } public void resetGame() { - for (cell c : field) { + for (Cell c : field) { c.reset(); } playerID = 1;