Browse Source

Merge commit '41b70d270045d59b2ac072081ad35bd9d4b7463c' into HEAD

TicTacToe_Game
Jenkins 3 years ago
parent
commit
c16481ba50
  1. 4
      src/main/java/TicTacToe/Cell.java
  2. 42
      src/main/java/TicTacToe/Scoreboard.java
  3. 19
      src/main/java/TicTacToe/TicTacToeGame.java

4
src/main/java/TicTacToe/cell.java → 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));

42
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);
}
}

19
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;

Loading…
Cancel
Save