Browse Source

Refactor cell, TicTacToe

feature_TicTacToe_Scoreboard
kfkama 3 years ago
parent
commit
e7468f1498
  1. 4
      src/main/java/TicTacToe/Cell.java
  2. 21
      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));

21
src/main/java/TicTacToe/TicTacToeGame.java

@ -16,7 +16,7 @@ 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;
@ -25,11 +25,10 @@ public class TicTacToeGame extends JPanel {
this.setSize(width, height);
setLayout(null);
initField();
scoreboard = new Scoreboard(maxPlayers);
scoreboard.setBounds(200, 100, 200, 50);
add(scoreboard);
initScoreboard();
}
public static void main(String[] args) {
JFrame f = new JFrame();
TicTacToeGame ttt = new TicTacToeGame();
@ -41,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]);
}
@ -59,6 +58,12 @@ public class TicTacToeGame extends JPanel {
}
}
private void initScoreboard() {
scoreboard = new Scoreboard(maxPlayers);
scoreboard.setBounds(200, 100, 200, 50);
add(scoreboard);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
@ -85,7 +90,7 @@ public class TicTacToeGame extends JPanel {
turns++;
if(turns >= playFieldSize) {
JOptionPane.showMessageDialog(getParent(),"Draw!");
scoreboard.addPoint(playerID);
scoreboard.addPoint(0);
resetGame();
}
@ -113,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