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.

133 lines
3.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package TicTacToe;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.geom.Line2D;
  6. import javax.swing.JFrame;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JPanel;
  9. public class TicTacToeGame extends JPanel {
  10. private static final long serialVersionUID = 1L;
  11. public static final int width = 600, height = 600;
  12. private static final int maxPlayers = 3;
  13. private static final int playFieldSize = 9;
  14. public Cell[] field;
  15. public int playerID = 1;
  16. public int turns = 0;
  17. public Scoreboard scoreboard;
  18. public TicTacToeGame() {
  19. this.setSize(width, height);
  20. setBackground(Color.black);
  21. setLayout(null);
  22. initField();
  23. initScoreboard();
  24. }
  25. public static void main(String[] args) {
  26. JFrame f = new JFrame();
  27. TicTacToeGame ttt = new TicTacToeGame();
  28. f.add(ttt);
  29. f.setSize(width,height);
  30. f.setLayout(null);
  31. f.setVisible(true);
  32. }
  33. public void initField() {
  34. field = new Cell[playFieldSize];
  35. for(int i = 0; i < field.length; i++) {
  36. field[i] = new Cell(this);
  37. add(field[i]);
  38. }
  39. for(int i = 0; i < field.length; i++) {
  40. if(i < 3) {
  41. field[i].setBounds(150 + i*100, 150 , 100, 100);
  42. } else if (i < 6) {
  43. field[i].setBounds(150 + i%3*100, 250 , 100, 100);
  44. } else {
  45. field[i].setBounds(150 + i%3*100, 350 , 100, 100);
  46. }
  47. }
  48. }
  49. private void initScoreboard() {
  50. scoreboard = new Scoreboard(maxPlayers);
  51. scoreboard.setBounds(150, 100, 300, 50);
  52. add(scoreboard);
  53. }
  54. @Override
  55. protected void paintComponent(Graphics g) {
  56. super.paintComponent(g);
  57. Graphics2D g2 = (Graphics2D) g;
  58. g2.setColor(Color.white);
  59. Line2D lin = new Line2D.Float(250, 150, 250, 450);
  60. Line2D lin2 = new Line2D.Float(350, 150, 350, 450);
  61. Line2D lin3 = new Line2D.Float(150, 250, 450, 250);
  62. Line2D lin4 = new Line2D.Float(150, 350, 450, 350);
  63. g2.draw(lin);
  64. g2.draw(lin2);
  65. g2.draw(lin3);
  66. g2.draw(lin4);
  67. }
  68. public void endTurn() {
  69. if(checkPlayfield() != 0) {
  70. JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!");
  71. scoreboard.addPoint(playerID);
  72. resetGame();
  73. } else {
  74. turns++;
  75. if(turns >= playFieldSize) {
  76. JOptionPane.showMessageDialog(getParent(),"Draw!");
  77. scoreboard.addPoint(0);
  78. resetGame();
  79. }
  80. playerID++;
  81. if(playerID >= maxPlayers) {
  82. playerID = 1;
  83. }
  84. }
  85. }
  86. public int checkPlayfield() {
  87. if ((field[0].playerID == playerID && field[1].playerID == playerID && field[2].playerID == playerID)
  88. || (field[0].playerID == playerID && field[3].playerID == playerID && field[6].playerID == playerID)
  89. || (field[8].playerID == playerID && field[5].playerID == playerID && field[2].playerID == playerID)
  90. || (field[8].playerID == playerID && field[7].playerID == playerID && field[6].playerID == playerID)
  91. || (field[0].playerID == playerID && field[4].playerID == playerID && field[8].playerID == playerID)
  92. || (field[0].playerID == playerID && field[4].playerID == playerID && field[8].playerID == playerID)
  93. || (field[2].playerID == playerID && field[4].playerID == playerID && field[6].playerID == playerID)
  94. || (field[3].playerID == playerID && field[4].playerID == playerID && field[5].playerID == playerID)
  95. || (field[1].playerID == playerID && field[4].playerID == playerID && field[7].playerID == playerID)) {
  96. return playerID;
  97. }
  98. return 0;
  99. }
  100. public void resetGame() {
  101. for (Cell c : field) {
  102. c.reset();
  103. }
  104. playerID = 1;
  105. turns = 0;
  106. }
  107. }