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.

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