Nur die besten Spiele ;3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.4 KiB

package TicTacToe;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class TicTacToeGame extends JPanel {
private static final long serialVersionUID = 1L;
public static final int width = 600, height = 600;
private static final int maxPlayers = 3;
private static final int playFieldSize = 9;
public Cell[] field;
public int playerID = 1;
public int turns = 0;
public Scoreboard scoreboard;
public TicTacToeGame() {
this.setSize(width, height);
setBackground(Color.black);
setLayout(null);
initField();
initScoreboard();
}
public static void main(String[] args) {
JFrame f = new JFrame();
TicTacToeGame ttt = new TicTacToeGame();
f.add(ttt);
f.setSize(width,height);
f.setLayout(null);
f.setVisible(true);
}
public void initField() {
field = new Cell[playFieldSize];
for(int i = 0; i < field.length; i++) {
field[i] = new Cell(this);
add(field[i]);
}
for(int i = 0; i < field.length; i++) {
if(i < 3) {
field[i].setBounds(150 + i*100, 150 , 100, 100);
} else if (i < 6) {
field[i].setBounds(150 + i%3*100, 250 , 100, 100);
} else {
field[i].setBounds(150 + i%3*100, 350 , 100, 100);
}
}
}
private void initScoreboard() {
scoreboard = new Scoreboard(maxPlayers);
scoreboard.setBounds(150, 100, 300, 50);
add(scoreboard);
}
@Override
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);
Line2D lin3 = new Line2D.Float(150, 250, 450, 250);
Line2D lin4 = new Line2D.Float(150, 350, 450, 350);
g2.draw(lin);
g2.draw(lin2);
g2.draw(lin3);
g2.draw(lin4);
}
public void endTurn() {
if(checkPlayfield() != 0) {
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;
}
}
}
public int checkPlayfield() {
if ((field[0].playerID == playerID && field[1].playerID == playerID && field[2].playerID == playerID)
|| (field[0].playerID == playerID && field[3].playerID == playerID && field[6].playerID == playerID)
|| (field[8].playerID == playerID && field[5].playerID == playerID && field[2].playerID == playerID)
|| (field[8].playerID == playerID && field[7].playerID == playerID && field[6].playerID == playerID)
|| (field[0].playerID == playerID && field[4].playerID == playerID && field[8].playerID == playerID)
|| (field[0].playerID == playerID && field[4].playerID == playerID && field[8].playerID == playerID)
|| (field[2].playerID == playerID && field[4].playerID == playerID && field[6].playerID == playerID)
|| (field[3].playerID == playerID && field[4].playerID == playerID && field[5].playerID == playerID)
|| (field[1].playerID == playerID && field[4].playerID == playerID && field[7].playerID == playerID)) {
return playerID;
}
return 0;
}
public void resetGame() {
for (Cell c : field) {
c.reset();
}
playerID = 1;
turns = 0;
}
}