From ea49de00f7da81aadcf79d0e6de0e3b21628deb2 Mon Sep 17 00:00:00 2001 From: ifsygn Date: Sun, 18 Dec 2022 22:35:06 +0300 Subject: [PATCH] TicTacToe game --- Java-Mini-Projects/game/pong/PongGame.java | 6 -- Java-Mini-Projects/game/snake/SnakeGame.java | 1 - Java-Mini-Projects/game/tictactoe/Main.java | 9 ++ .../game/tictactoe/TicTacToe.java | 88 +++++++++++++++++++ 4 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 Java-Mini-Projects/game/tictactoe/Main.java create mode 100644 Java-Mini-Projects/game/tictactoe/TicTacToe.java diff --git a/Java-Mini-Projects/game/pong/PongGame.java b/Java-Mini-Projects/game/pong/PongGame.java index f42e1e2..7dc9de5 100644 --- a/Java-Mini-Projects/game/pong/PongGame.java +++ b/Java-Mini-Projects/game/pong/PongGame.java @@ -1,10 +1,4 @@ package game.pong; - -import java.awt.*; -import java.awt.event.*; -import java.util.*; -import javax.swing.*; - public class PongGame { public static void main(String[] args) { diff --git a/Java-Mini-Projects/game/snake/SnakeGame.java b/Java-Mini-Projects/game/snake/SnakeGame.java index 56538c1..1cf74ce 100644 --- a/Java-Mini-Projects/game/snake/SnakeGame.java +++ b/Java-Mini-Projects/game/snake/SnakeGame.java @@ -1,5 +1,4 @@ package game.snake; - public class SnakeGame { public static void main(String[] args) { diff --git a/Java-Mini-Projects/game/tictactoe/Main.java b/Java-Mini-Projects/game/tictactoe/Main.java new file mode 100644 index 0000000..687171e --- /dev/null +++ b/Java-Mini-Projects/game/tictactoe/Main.java @@ -0,0 +1,9 @@ +package game.tictactoe; + +public class Main { + + public static void main(String[] args) + { + TicTacToe ticTacToe = new TicTacToe(); + } +} diff --git a/Java-Mini-Projects/game/tictactoe/TicTacToe.java b/Java-Mini-Projects/game/tictactoe/TicTacToe.java new file mode 100644 index 0000000..9e06990 --- /dev/null +++ b/Java-Mini-Projects/game/tictactoe/TicTacToe.java @@ -0,0 +1,88 @@ +package game.tictactoe; + +import java.awt.*; +import java.awt.event.*; +import java.util.*; +import javax.swing.*; + +public class TicTacToe implements ActionListener{ + + Random random = new Random(); + JFrame frame = new JFrame(); + JPanel title_panel = new JPanel(); + JPanel button_panel = new JPanel(); + JLabel textfield = new JLabel(); + JButton[] buttons = new JButton[9]; + boolean player1_turn; +// boolean player2_turn; + + TicTacToe() + { + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(800, 800); + frame.getContentPane().setBackground(new Color(50, 50, 50)); + frame.setLayout(new BorderLayout()); + frame.setVisible(true); + + textfield.setBackground(new Color(25,25,25)); + textfield.setForeground(new Color(25,255,0)); + textfield.setFont(new Font("Ink Free", Font.BOLD, 75)); + textfield.setHorizontalAlignment(JLabel.CENTER); + textfield.setText("Tic-Tac-Toe"); + textfield.setOpaque(true); + + title_panel.setLayout(new BorderLayout()); + title_panel.setBounds(0, 0, 800, 800); + + button_panel.setLayout(new GridLayout(3, 3)); + button_panel.setBackground(new Color(150,150,150)); + + title_panel.add(textfield); + frame.add(title_panel, BorderLayout.NORTH); + frame.add(button_panel); + + for (int i = 0; i < 9; i++) { + buttons[i] = new JButton(); + button_panel.add(buttons[i]); + buttons[i].setFont(new Font("MV Boli", Font.BOLD, 120)); + buttons[i].setFocusable(false); + buttons[i].addActionListener(this); + } + } + + @Override + public void actionPerformed(ActionEvent e) + { + //TODO Auto-generated method stub + } + + public void firstTurn() + { + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + if (random.nextInt(2) == 0) { + player1_turn = true; + textfield.setText("X turn"); + } else { + player1_turn = false; + textfield.setText("O turn"); + } + } + + public void check() { + + } + + public void xWins(int a, int b, int c) { + + } + + public void oWins(int a, int b, int c) + { + + } +}