Browse Source

Reset game on game end

feature_TicTacToe_BasicPlayfield
kfkama 3 years ago
parent
commit
e5690a005d
  1. 12
      src/main/java/TicTacToe/TicTacToeGame.java
  2. 17
      src/main/java/TicTacToe/cell.java

12
src/main/java/TicTacToe/TicTacToeGame.java

@ -74,13 +74,13 @@ 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!");
System.exit(0);
resetGame();
} }
turns++; turns++;
if(turns >= playFieldSize) { if(turns >= playFieldSize) {
JOptionPane.showMessageDialog(getParent(),"Draw!"); JOptionPane.showMessageDialog(getParent(),"Draw!");
System.exit(0);
resetGame();
} }
playerID++; playerID++;
@ -106,5 +106,13 @@ public class TicTacToeGame extends JPanel {
return 0; return 0;
} }
public void resetGame() {
for (cell c : field) {
c.reset();
}
playerID = 1;
turns = 0;
}
} }

17
src/main/java/TicTacToe/cell.java

@ -1,4 +1,5 @@
package TicTacToe; package TicTacToe;
import java.awt.Color; import java.awt.Color;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
@ -12,6 +13,7 @@ public class cell extends JButton {
public cell(TicTacToeGame _ttt) { public cell(TicTacToeGame _ttt) {
ttt = _ttt; ttt = _ttt;
setBackground(new Color(255,255,255));
addActionListener(new ActionListener() { addActionListener(new ActionListener() {
@Override @Override
@ -28,17 +30,20 @@ public class cell extends JButton {
setEnabled(false); setEnabled(false);
switch (playerID) { switch (playerID) {
case 1: setBackground(new Color(255,0,0));
break;
case 2: setBackground(new Color(0,0,255));
case 1:
setBackground(new Color(255, 0, 0));
break; break;
default: ;
case 2:
setBackground(new Color(0, 0, 255));
break; break;
} }
} }
} }
protected void reset() {
playerID = 0;
setEnabled(true);
setBackground(new Color(255,255,255));
}
} }
Loading…
Cancel
Save