Browse Source

Merge commit '848a5ba499d335d70de776f8626c65a10942e230' into HEAD

TicTacToe_Game
Jenkins 3 years ago
parent
commit
91c5969d9c
  1. 35
      src/main/java/TicTacToe/Cell.java
  2. 3
      src/main/java/TicTacToe/Scoreboard.java
  3. 25
      src/main/java/TicTacToe/TicTacToeGame.java

35
src/main/java/TicTacToe/Cell.java

@ -1,6 +1,8 @@
package TicTacToe;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@ -13,7 +15,7 @@ public class Cell extends JButton {
public Cell(TicTacToeGame _ttt) {
ttt = _ttt;
setBackground(new Color(255,255,255));
setBackground(new Color(0));
addActionListener(new ActionListener() {
@Override
@ -26,24 +28,33 @@ public class Cell extends JButton {
protected void OnMouseClick() {
if (playerID == 0) {
playerID = ttt.playerID;
ttt.endTurn();
setEnabled(false);
ttt.endTurn();
repaint();
}
}
switch (playerID) {
case 1:
setBackground(new Color(255, 0, 0));
break;
case 2:
setBackground(new Color(0, 0, 255));
break;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.white);
switch (playerID) {
case 1:
g2.drawLine(5, 5, 90, 90);
g2.drawLine(90, 5, 5, 90);
break;
case 2:
g2.drawOval(5, 5, 90, 90);
break;
}
}
protected void reset() {
playerID = 0;
setEnabled(true);
setBackground(new Color(255,255,255));
repaint();
}
}

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

@ -1,5 +1,7 @@
package TicTacToe;
import java.awt.Color;
import javax.swing.JLabel;
public class Scoreboard extends JLabel{
@ -10,6 +12,7 @@ public class Scoreboard extends JLabel{
public Scoreboard(int _playerCount) {
playerPoints = new int[_playerCount];
setForeground(Color.white);
updateScores();
}

25
src/main/java/TicTacToe/TicTacToeGame.java

@ -1,5 +1,6 @@
package TicTacToe;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
@ -23,6 +24,7 @@ public class TicTacToeGame extends JPanel {
public TicTacToeGame() {
this.setSize(width, height);
setBackground(Color.black);
setLayout(null);
initField();
initScoreboard();
@ -68,6 +70,7 @@ public class TicTacToeGame extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.white);
Line2D lin = new Line2D.Float(250, 150, 250, 450);
Line2D lin2 = new Line2D.Float(350, 150, 350, 450);
@ -85,19 +88,21 @@ public class TicTacToeGame extends JPanel {
JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!");
scoreboard.addPoint(playerID);
resetGame();
} else {
turns++;
if(turns >= playFieldSize) {
JOptionPane.showMessageDialog(getParent(),"Draw!");
scoreboard.addPoint(0);
resetGame();
}
playerID++;
if(playerID >= maxPlayers) {
playerID = 1;
}
}
turns++;
if(turns >= playFieldSize) {
JOptionPane.showMessageDialog(getParent(),"Draw!");
scoreboard.addPoint(0);
resetGame();
}
playerID++;
if(playerID >= maxPlayers) {
playerID = 1;
}
}
public int checkPlayfield() {

Loading…
Cancel
Save