diff --git a/README.md b/README.md index 1a0c609..27cf3a9 100644 --- a/README.md +++ b/README.md @@ -1 +1,45 @@ -JAVA/CPP repo for OPENHACK. +# Simple Text Editor + +This is a simple text editor application created using Java Swing. + +## Features + +- Create new text files. +- Open existing text files. +- Save text files. +- Print text files. +- Cut, copy, and paste text. +- Close files. + +## How to Run + +1. Ensure you have Java installed on your PC. If not, download and install Java from [here](https://www.java.com/en/download/). +2. Download the `editor.java` file from this repository. +3. Open Command Prompt (on Windows) or Terminal (on macOS/Linux). +4. Navigate to the directory where the `editor.java` file is located. +5. Compile the Java source file using the following command: + ```bash + javac editor.java + ``` +6. Run the compiled Java program using the following command: + ```bash + java editor + ``` + +## Usage + +- **Creating a New File**: Click on the "File" menu and select "New". +- **Opening an Existing File**: Click on the "File" menu and select "Open". +- **Saving a File**: Click on the "File" menu and select "Save". +- **Printing a File**: Click on the "File" menu and select "Print". +- **Editing Operations**: Use the "Edit" menu for cut, copy, and paste operations. +- **Cutting Text**: Select the text you want to cut, then click on the "Edit" menu and select "Cut". +- **Copying Text**: Select the text you want to copy, then click on the "Edit" menu and select "Copy". +- **Pasting Text**: Place the cursor where you want to paste the text, then click on the "Edit" menu and select "Paste". +- **Closing a File**: Click on the "File" menu and select "Close" to close the current file window. + +## Notes + +- This text editor supports editing multiple text files simultaneously. +- The application uses Java Swing for the graphical user interface. + diff --git a/READme(tic tac toe).md b/READme(tic tac toe).md new file mode 100644 index 0000000..5a4fc4e --- /dev/null +++ b/READme(tic tac toe).md @@ -0,0 +1,41 @@ +# Tic Tac Toe Game + +This is a simple Tic Tac Toe game implemented in Java with a graphical user interface (GUI). Players take turns marking spaces in a 3x3 grid, aiming to be the first to get three of their marks in a row. + +## Features + +- Graphical user interface built using Java Swing. +- Tracks player scores and displays winning ratios. +- Stores high scores in a MySQL database. +- Allows resetting of scores. +- Shows statistics of the game, including winning ratios for X and O players and draw ratios. + +## Prerequisites + +- Java Development Kit (JDK) installed on your system. +- MySQL database server set up and running. + +## Usage + +-Players take turns clicking on empty spaces in the grid to mark them with their symbol (X or O). +-The game tracks the scores of X and O players, as well as draw games. +-Click on the "Show Stats" menu item to display the winning ratios of X and O players, as well as the draw ratio. +-Click on the "High Scores" menu item to display the top scores stored in the database. +-Click on the "Reset Scores" button to clear all scores stored in the database. +-Enjoy playing Tic Tac Toe! + +## Setup + +1. Set up the MySQL database: + +- Create a MySQL database named `tictactoe`. +- Create a table named `scores` with two columns: `player` (VARCHAR) and `wins` (INT). + +2. Open the project in your Java IDE. + +3. Add the MySQL JDBC driver to your project's dependencies. You can download the MySQL JDBC driver from [here](https://dev.mysql.com/downloads/connector/j/). + +4. Update the MySQL connection details in the `TicTacToeGUI.java` file: + +```java +connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/shubham", "root", ""); diff --git a/Texteditor.java b/Texteditor.java new file mode 100644 index 0000000..e60bece --- /dev/null +++ b/Texteditor.java @@ -0,0 +1,268 @@ +// Java Program to create a text editor using java + +import javax.swing.*; +import java.io.*; +import java.awt.event.*; +import javax.swing.plaf.metal.*; + +class Texteditor extends JFrame implements ActionListener { + // Text component + + JTextArea t; + private JFrame[] frames; + private JTextArea[] textAreas; + private int numOpenFiles; + + // Frame + JFrame f; + + // Constructor + Texteditor() { + // Create a frame + f = new JFrame("editor"); + + try { + // Set metal look and feel + UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); + + // Set theme to ocean + MetalLookAndFeel.setCurrentTheme(new OceanTheme()); + } catch (Exception e) { + } + + // Text component + t = new JTextArea(); + // Initialize arrays + frames = new JFrame[10]; // Assuming a maximum of 10 open files + textAreas = new JTextArea[10]; + numOpenFiles = 0; + + // Create a menubar + JMenuBar mb = new JMenuBar(); + + // Create amenu for menu + JMenu m1 = new JMenu("File"); + + // Create menu items + JMenuItem mi1 = new JMenuItem("New"); + JMenuItem mi2 = new JMenuItem("Open"); + JMenuItem mi3 = new JMenuItem("Save"); + JMenuItem mi9 = new JMenuItem("Print"); + + // Add action listener + mi1.addActionListener(this); + mi2.addActionListener(this); + mi3.addActionListener(this); + mi9.addActionListener(this); + + m1.add(mi1); + m1.add(mi2); + m1.add(mi3); + m1.add(mi9); + + // Create amenu for menu + JMenu m2 = new JMenu("Edit"); + + // Create menu items + JMenuItem mi4 = new JMenuItem("cut"); + JMenuItem mi5 = new JMenuItem("copy"); + JMenuItem mi6 = new JMenuItem("paste"); + + // Add action listener + mi4.addActionListener(this); + mi5.addActionListener(this); + mi6.addActionListener(this); + + m2.add(mi4); + m2.add(mi5); + m2.add(mi6); + + JMenuItem mc = new JMenuItem("close"); + + mc.addActionListener(this); + + mb.add(m1); + mb.add(m2); + mb.add(mc); + + f.setJMenuBar(mb); + f.add(t); + f.setSize(500, 500); + f.show(); + } + + // If a button is pressed + public void actionPerformed(ActionEvent e) { + String s = e.getActionCommand(); + + if (s.equals("cut")) { + t.cut(); + } else if (s.equals("copy")) { + t.copy(); + } else if (s.equals("paste")) { + t.paste(); + } else if (s.equals("Save")) { + // Create an object of JFileChooser class + JFileChooser j = new JFileChooser("f:"); + + // Invoke the showsSaveDialog function to show the save dialog + int r = j.showSaveDialog(null); + + if (r == JFileChooser.APPROVE_OPTION) { + + // Set the label to the path of the selected directory + File fi = new File(j.getSelectedFile().getAbsolutePath()); + + try { + // Create a file writer + FileWriter wr = new FileWriter(fi, false); + + // Create buffered writer to write + BufferedWriter w = new BufferedWriter(wr); + + // Write + w.write(t.getText()); + + w.flush(); + w.close(); + } catch (Exception evt) { + JOptionPane.showMessageDialog(f, evt.getMessage()); + } + } // If the user cancelled the operation + else { + JOptionPane.showMessageDialog(f, "the user cancelled the operation"); + } + } else if (s.equals("Print")) { + try { + // print the file + t.print(); + } catch (Exception evt) { + JOptionPane.showMessageDialog(f, evt.getMessage()); + } + } else if (s.equals("Open")) { + // Create an object of JFileChooser class + JFileChooser j = new JFileChooser("f:"); + + // Invoke the showsOpenDialog function to show the save dialog + int r = j.showOpenDialog(null); + + // If the user selects a file + if (r == JFileChooser.APPROVE_OPTION) { + // Set the label to the path of the selected directory + File fi = new File(j.getSelectedFile().getAbsolutePath()); + + try { + // String + String s1 = "", sl = ""; + + // File reader + FileReader fr = new FileReader(fi); + + // Buffered reader + BufferedReader br = new BufferedReader(fr); + + // Initialize sl + sl = br.readLine(); + + // Take the input from the file + while ((s1 = br.readLine()) != null) { + sl = sl + "\n" + s1; + } + + // Set the text + t.setText(sl); + } catch (Exception evt) { + JOptionPane.showMessageDialog(f, evt.getMessage()); + } + } // If the user cancelled the operation + else { + JOptionPane.showMessageDialog(f, "the user cancelled the operation"); + } + } else if (s.equals("New")) { + t.setText(""); + createNewFile(); + } else if (s.equals("close")) { + f.setVisible(false); + } + } + + private void createNewFile() { + JFrame frame = new JFrame("Untitled-" + (numOpenFiles + 1)); + JTextArea textArea = new JTextArea(); + JScrollPane scrollPane = new JScrollPane(textArea); + frame.add(scrollPane); + + // Create a menubar + JMenuBar mb = new JMenuBar(); + + // Create a menu for File + JMenu m1 = new JMenu("File"); + + // Create menu items for File + JMenuItem mi1 = new JMenuItem("New"); + JMenuItem mi2 = new JMenuItem("Open"); + JMenuItem mi3 = new JMenuItem("Save"); + JMenuItem mi9 = new JMenuItem("Print"); + + // Add action listeners to menu items + mi1.addActionListener(this); + mi2.addActionListener(this); + mi3.addActionListener(this); + mi9.addActionListener(this); + + // Add menu items to the File menu + m1.add(mi1); + m1.add(mi2); + m1.add(mi3); + m1.add(mi9); + + // Create a menu for Edit + JMenu m2 = new JMenu("Edit"); + + // Create menu items for Edit + JMenuItem mi4 = new JMenuItem("cut"); + JMenuItem mi5 = new JMenuItem("copy"); + JMenuItem mi6 = new JMenuItem("paste"); + + // Add action listeners to menu items + mi4.addActionListener(this); + mi5.addActionListener(this); + mi6.addActionListener(this); + + // Add menu items to the Edit menu + m2.add(mi4); + m2.add(mi5); + m2.add(mi6); + + // Create a menu item for Close + JMenuItem mc = new JMenuItem("close"); + + // Add action listener to the Close menu item + mc.addActionListener(this); + + // Add File and Edit menus to the menubar + mb.add(m1); + mb.add(m2); + + // Add Close menu item to the menubar + mb.add(mc); + + // Set the menubar for the frame + frame.setJMenuBar(mb); + + // Set frame size and visibility + frame.setSize(500, 500); + frame.setVisible(true); + + // Store references to JFrame and JTextArea + frames[numOpenFiles] = frame; + textAreas[numOpenFiles] = textArea; + numOpenFiles++; +} + + + // Main class + public static void main(String args[]) { + Texteditor e = new Texteditor(); + } +} diff --git a/TicTacToeGUI.java b/TicTacToeGUI.java new file mode 100644 index 0000000..39dc162 --- /dev/null +++ b/TicTacToeGUI.java @@ -0,0 +1,239 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.sql.*; + +public class TicTacToeGUI extends JFrame implements ActionListener { + private JButton[][] buttons; + private boolean xTurn = true; + private JLabel statusLabel; + private int xWins = 0; + private int oWins = 0; + private int draws = 0; + private Connection connection; + + public TicTacToeGUI() { + setTitle("Tic Tac Toe"); + setSize(300, 350); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + JPanel panel = new JPanel(new GridLayout(3, 3)); + buttons = new JButton[3][3]; + + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + buttons[i][j] = new JButton(""); + buttons[i][j].setFont(new Font(Font.SANS_SERIF, Font.BOLD, 50)); + buttons[i][j].setPreferredSize(new Dimension(100, 100)); + buttons[i][j].addActionListener(this); + panel.add(buttons[i][j]); + } + } + + statusLabel = new JLabel("X's turn"); + statusLabel.setHorizontalAlignment(SwingConstants.CENTER); + + JButton resetButton = new JButton("Reset Scores"); + resetButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + resetScores(); + } + }); + + JPanel buttonPanel = new JPanel(); + buttonPanel.add(resetButton); + + JMenuBar menuBar = new JMenuBar(); + JMenu historyMenu = new JMenu("History"); + JMenuItem statsItem = new JMenuItem("Show Stats"); + statsItem.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + showStats(); + } + }); + historyMenu.add(statsItem); + JMenuItem highScoresItem = new JMenuItem("High Scores"); + highScoresItem.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + showHighScores(); + } + }); + historyMenu.add(highScoresItem); + menuBar.add(historyMenu); + + setJMenuBar(menuBar); + + add(panel, BorderLayout.CENTER); + add(statusLabel, BorderLayout.NORTH); + add(buttonPanel, BorderLayout.SOUTH); + + connectToDatabase(); + createTable(); + setVisible(true); + } + + private void connectToDatabase() { + try { + Class.forName("com.mysql.jdbc.Driver"); + connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/shubham", "root", ""); + } catch (ClassNotFoundException | SQLException e) { + e.printStackTrace(); + } + } + + private void createTable() { + try (Statement statement = connection.createStatement()) { + statement.executeUpdate("CREATE TABLE IF NOT EXISTS scores (player VARCHAR(255) PRIMARY KEY, wins INT)"); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Override + public void actionPerformed(ActionEvent e) { + JButton button = (JButton) e.getSource(); + if (!button.getText().equals("")) { + return; + } + + if (xTurn) { + button.setText("X"); + statusLabel.setText("O's turn"); + } else { + button.setText("O"); + statusLabel.setText("X's turn"); + } + + if (checkWin()) { + String winner = xTurn ? "X" : "O"; + JOptionPane.showMessageDialog(this, "Player " + winner + " wins!"); + if (winner.equals("X")) { + xWins++; + } else { + oWins++; + } + updateScores(winner); + resetGame(); + } else if (checkDraw()) { + JOptionPane.showMessageDialog(this, "It's a draw!"); + draws++; + updateScores(null); // No winner + resetGame(); + } + + xTurn = !xTurn; + } + + private boolean checkWin() { + String[][] board = new String[3][3]; + + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + board[i][j] = buttons[i][j].getText(); + } + } + + // Check rows + for (int i = 0; i < 3; i++) { + if (board[i][0].equals(board[i][1]) && board[i][1].equals(board[i][2]) && !board[i][0].equals("")) { + return true; + } + } + + // Check columns + for (int j = 0; j < 3; j++) { + if (board[0][j].equals(board[1][j]) && board[1][j].equals(board[2][j]) && !board[0][j].equals("")) { + return true; + } + } + + // Check diagonals + if ((board[0][0].equals(board[1][1]) && board[1][1].equals(board[2][2]) && !board[0][0].equals("")) || + (board[0][2].equals(board[1][1]) && board[1][1].equals(board[2][0]) && !board[0][2].equals(""))) { + return true; + } + + return false; + } + + private boolean checkDraw() { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (buttons[i][j].getText().equals("")) { + return false; + } + } + } + return true; + } + + private void resetGame() { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + buttons[i][j].setText(""); + } + } + statusLabel.setText("X's turn"); + xTurn = true; + } + + private void showStats() { + double totalGames = xWins + oWins + draws; + double xRatio = (xWins / totalGames) * 100; + double oRatio = (oWins / totalGames) * 100; + double drawRatio = (draws / totalGames) * 100; + + JOptionPane.showMessageDialog(this, String.format("X Wins: %.2f%%\nO Wins: %.2f%%\nDraws: %.2f%%", xRatio, oRatio, drawRatio), + "Winning Ratios", JOptionPane.INFORMATION_MESSAGE); + } + + private void showHighScores() { + StringBuilder message = new StringBuilder("High Scores:\n"); + try (Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery("SELECT * FROM scores")) { + while (resultSet.next()) { + String player = resultSet.getString("player"); + int wins = resultSet.getInt("wins"); + message.append(player).append(": ").append(wins).append("\n"); + } + } catch (SQLException e) { + e.printStackTrace(); + } + JOptionPane.showMessageDialog(this, message.toString(), "High Scores", JOptionPane.INFORMATION_MESSAGE); + } + + private void updateScores(String winner) { + if (winner != null) { + try { + PreparedStatement statement = connection.prepareStatement("INSERT INTO scores (player, wins) VALUES (?, 1) ON DUPLICATE KEY UPDATE wins = wins + 1"); + statement.setString(1, winner); + statement.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + private void resetScores() { + int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to reset all scores?", "Reset Scores", JOptionPane.YES_NO_OPTION); + if (confirm == JOptionPane.YES_OPTION) { + try (Statement statement = connection.createStatement()) { + statement.executeUpdate("TRUNCATE TABLE scores"); + xWins = 0; + oWins = 0; + draws = 0; + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(TicTacToeGUI::new); + } +} diff --git a/mysql-connector-j-8.1.0.jar b/mysql-connector-j-8.1.0.jar new file mode 100644 index 0000000..9e38a71 Binary files /dev/null and b/mysql-connector-j-8.1.0.jar differ diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..eecb906 --- /dev/null +++ b/readme.md @@ -0,0 +1,46 @@ +![TE](https://github.com/Shubham25104/OH24-CPP-Java/assets/117568702/0cad3314-8e5f-4e73-b17c-cac3e380ebbc) +# Simple Text Editor + +This is a simple text editor application created using Java Swing. + +## Features + +- Create new text files. +- Open existing text files. +- Save text files. +- Print text files. +- Cut, copy, and paste text. +- Close files. + +## How to Run + +1. Ensure you have Java installed on your PC. If not, download and install Java from [here](https://www.java.com/en/download/). +2. Download the `editor.java` file from this repository. +3. Open Command Prompt (on Windows) or Terminal (on macOS/Linux). +4. Navigate to the directory where the `editor.java` file is located. +5. Compile the Java source file using the following command: + ```bash + javac editor.java + ``` +6. Run the compiled Java program using the following command: + ```bash + java editor + ``` + +## Usage + +- **Creating a New File**: Click on the "File" menu and select "New". +- **Opening an Existing File**: Click on the "File" menu and select "Open". +- **Saving a File**: Click on the "File" menu and select "Save". +- **Printing a File**: Click on the "File" menu and select "Print". +- **Editing Operations**: Use the "Edit" menu for cut, copy, and paste operations. +- **Cutting Text**: Select the text you want to cut, then click on the "Edit" menu and select "Cut". +- **Copying Text**: Select the text you want to copy, then click on the "Edit" menu and select "Copy". +- **Pasting Text**: Place the cursor where you want to paste the text, then click on the "Edit" menu and select "Paste". +- **Closing a File**: Click on the "File" menu and select "Close" to close the current file window. + +## Notes + +- This text editor supports editing multiple text files simultaneously. +- The application uses Java Swing for the graphical user interface. +