Browse Source

Show Scoreboard in TicTacToe game

feature_TicTacToe_Scoreboard
kfkama 3 years ago
parent
commit
ca9ca4446a
  1. 3
      src/main/java/TicTacToe/Scoreboard.java
  2. 6
      src/main/java/TicTacToe/TicTacToeGame.java

3
src/main/java/TicTacToe/Scoreboard.java

@ -10,6 +10,7 @@ public class Scoreboard extends JLabel{
public Scoreboard(int _playerCount) { public Scoreboard(int _playerCount) {
playerPoints = new int[_playerCount]; playerPoints = new int[_playerCount];
updateScores();
} }
public void addPoint(int playerID) { public void addPoint(int playerID) {
@ -27,7 +28,7 @@ public class Scoreboard extends JLabel{
if(i == 0) { if(i == 0) {
setText("Draws: " + playerPoints[0]); setText("Draws: " + playerPoints[0]);
} else { } else {
setText(getText() + " Player " + i + ": " + playerPoints[i]);
setText(getText() + " Player " + i + ": " + playerPoints[i]);
} }
} }
} }

6
src/main/java/TicTacToe/TicTacToeGame.java

@ -19,11 +19,15 @@ public class TicTacToeGame extends JPanel {
public cell[] field; public cell[] field;
public int playerID = 1; public int playerID = 1;
public int turns = 0; public int turns = 0;
public Scoreboard scoreboard;
public TicTacToeGame() { public TicTacToeGame() {
this.setSize(width, height); this.setSize(width, height);
setLayout(null); setLayout(null);
initField(); initField();
scoreboard = new Scoreboard(maxPlayers);
scoreboard.setBounds(200, 100, 200, 50);
add(scoreboard);
} }
public static void main(String[] args) { public static void main(String[] args) {
@ -74,12 +78,14 @@ public class TicTacToeGame extends JPanel {
public void endTurn() { public void endTurn() {
if(checkPlayfield() != 0) { if(checkPlayfield() != 0) {
JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!"); JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!");
scoreboard.addPoint(playerID);
resetGame(); resetGame();
} }
turns++; turns++;
if(turns >= playFieldSize) { if(turns >= playFieldSize) {
JOptionPane.showMessageDialog(getParent(),"Draw!"); JOptionPane.showMessageDialog(getParent(),"Draw!");
scoreboard.addPoint(playerID);
resetGame(); resetGame();
} }

Loading…
Cancel
Save