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) {
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]);
}
}
}

6
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();
}

Loading…
Cancel
Save