fork download
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Main extends JFrame implements ActionListener {
  6.  
  7. private JButton[][] buttons;
  8. private JLabel statusLabel;
  9. private boolean playerXTurn = true;
  10. private int[][] board;
  11.  
  12. public Main() {
  13. super("Tic Tac Toe");
  14. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. setSize(300, 300);
  16. setLocationRelativeTo(null);
  17. setLayout(new BorderLayout());
  18.  
  19. // Create the board
  20. board = new int[3][3];
  21. buttons = new JButton[3][3];
  22. JPanel boardPanel = new JPanel(new GridLayout(3, 3));
  23. for (int row = 0; row < 3; row++) {
  24. for (int col = 0; col < 3; col++) {
  25. buttons[row][col] = new JButton("");
  26. buttons[row][col].setFont(new Font("Arial", Font.BOLD, 40));
  27. buttons[row][col].addActionListener(this);
  28. boardPanel.add(buttons[row][col]);
  29. }
  30. }
  31. add(boardPanel, BorderLayout.CENTER);
  32.  
  33. // Create the status label
  34. statusLabel = new JLabel("X's turn");
  35. statusLabel.setHorizontalAlignment(SwingConstants.CENTER);
  36. add(statusLabel, BorderLayout.SOUTH);
  37.  
  38. setVisible(true);
  39. }
  40.  
  41. @Override
  42. public void actionPerformed(ActionEvent e) {
  43. for (int row = 0; row < 3; row++) {
  44. for (int col = 0; col < 3; col++) {
  45. if (e.getSource() == buttons[row][col]) {
  46. if (board[row][col] == 0) {
  47. if (playerXTurn) {
  48. buttons[row][col].setText("X");
  49. board[row][col] = 1; // X
  50. } else {
  51. buttons[row][col].setText("O");
  52. board[row][col] = -1; // O
  53. }
  54. playerXTurn = !playerXTurn;
  55. if (playerXTurn) {
  56. statusLabel.setText("X's turn");
  57. } else {
  58. statusLabel.setText("O's turn");
  59. }
  60. if (checkWin()) {
  61. if (playerXTurn) {
  62. statusLabel.setText("O wins!");
  63. } else {
  64. statusLabel.setText("X wins!");
  65. }
  66. disableButtons();
  67. } else if (checkDraw()) {
  68. statusLabel.setText("It's a draw!");
  69. disableButtons();
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76.  
  77. private boolean checkWin() {
  78. // Check rows
  79. for (int row = 0; row < 3; row++) {
  80. if (board[row][0] != 0 && board[row][0] == board[row][1] && board[row][0] == board[row][2]) {
  81. return true;
  82. }
  83. }
  84. // Check columns
  85. for (int col = 0; col < 3; col++) {
  86. if (board[0][col] != 0 && board[0][col] == board[1][col] && board[0][col] == board[2][col]) {
  87. return true;
  88. }
  89. }
  90. // Check diagonals
  91. if (board[0][0] != 0 && board[0][0] == board[1][1] && board[0][0] == board[2][2]) {
  92. return true;
  93. }
  94. if (board[0][2] != 0 && board[0][2] == board[1][1] && board[0][2] == board[2][0]) {
  95. return true;
  96. }
  97. return false;
  98. }
  99.  
  100. private boolean checkDraw() {
  101. for (int row = 0; row < 3; row++) {
  102. for (int col = 0; col < 3; col++) {
  103. if (board[row][col] == 0) {
  104. return false; // There are still empty cells
  105. }
  106. }
  107. }
  108. return true; // All cells are filled
  109. }
  110.  
  111. private void disableButtons() {
  112. for (int row = 0; row < 3; row++) {
  113. for (int col = 0; col < 3; col++) {
  114. buttons[row][col].setEnabled(false);
  115. }
  116. }
  117. }
  118.  
  119. public static void main(String[] args) {
  120. SwingUtilities.invokeLater(new Runnable() {
  121. public void run() {
  122. new Main();
  123. }
  124. });
  125. }
  126. }
Success #stdin #stdout #stderr 0.25s 67408KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "AWT-EventQueue-0" java.awt.HeadlessException: 
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
	at java.desktop/java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:197)
	at java.desktop/java.awt.Window.<init>(Window.java:538)
	at java.desktop/java.awt.Frame.<init>(Frame.java:423)
	at java.desktop/javax.swing.JFrame.<init>(JFrame.java:224)
	at Main.<init>(Main.java:13)
	at Main$1.run(Main.java:122)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)