Moortiii icon

Ling - Assignment03

Moortiii | PRO | 02/19/18 10:32:26 PM UTC | 0 ⭐ | 833 👁️ | Never ⏰ | []
Java |

4.41 KB

|

None

|

0 👍

/

0 👎

package com.company;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
 
public class GUI extends JFrame implements ActionListener, KeyListener {
    private JMenuBar menuBar = new JMenuBar();
    private JMenu fileMenu = new JMenu("File");
    private JMenu newGame = new JMenu("New game");
    private JMenuItem deleteGame = new JMenuItem("Delete game");
    private JMenuItem easyGame = new JMenuItem("Easy game");
    private JMenuItem mediumGame = new JMenuItem("Medium game");
    private JMenuItem hardGame = new JMenuItem("Hard game");
    private JTextField[][] gameGrid = new JTextField[9][9];
 
    public GUI() {
        this.setLayout(new GridLayout(9, 9));
        this.setTitle("Sudoku");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        fillEmptyGrid();
        addKeyListeners();
        addMenuItems();
        addActionListeners();
        fillBoard(100);
 
        this.setVisible(true);
        this.pack();
    }
 
    private void fillEmptyGrid() {
        for (int i = 0; i < 9 ; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                this.gameGrid[i][j] = new JTextField("");
                this.gameGrid[i][j].setPreferredSize(new Dimension(50, 50));
                this.gameGrid[i][j].setHorizontalAlignment(JTextField.CENTER);
                this.add(this.gameGrid[i][j]);
            }
        }
    }
 
    private void addKeyListeners() {
        for (int i = 0; i < 9 ; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                this.gameGrid[i][j].addKeyListener(this);
            }
        }
    }
 
    private void addActionListeners() {
        this.deleteGame.addActionListener(this);
        this.newGame.addActionListener(this);
        this.easyGame.addActionListener(this);
        this.mediumGame.addActionListener(this);
        this.hardGame.addActionListener(this);
    }
 
    private void addMenuItems() {
        this.setJMenuBar(this.menuBar);
        this.menuBar.add(this.fileMenu);
        this.fileMenu.add(this.newGame);
        this.fileMenu.add(this.deleteGame);
        this.newGame.add(this.easyGame);
        this.newGame.add(this.mediumGame);
        this.newGame.add(this.hardGame);
    }
 
    private ArrayList<Integer> generateNumbers()
    {
        ArrayList<Integer> sudokuNumbers = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                sudokuNumbers.add((j + 1));
            }
        }
        return sudokuNumbers;
    }
 
    private void clearGrid() {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                this.gameGrid[i][j].setText("");
                this.gameGrid[i][j].setEditable(true);
            }
        }
    }
 
    private void fillBoard(int fillPercentage){
        clearGrid();
        ArrayList<Integer> sudokuNumbers = generateNumbers();
        Random randomGenerator = new Random();
 
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                int tolerance = randomGenerator.nextInt(100);
                if(tolerance < fillPercentage) {
                    JTextField currentCell = this.gameGrid[i][j];
                    int randomIndex = randomGenerator.nextInt(sudokuNumbers.size());
                    String randomNumber = Integer.toString(sudokuNumbers.get(randomIndex));
                    sudokuNumbers.remove(randomIndex);
                    currentCell.setText(randomNumber);
                    currentCell.setEditable(false);
                }
            }
        }
    }
 
    @Override
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource().equals(this.deleteGame)) {
            clearGrid();
        } else if (e.getSource().equals(this.easyGame)) {
            fillBoard(100);
        } else if (e.getSource().equals(this.mediumGame)) {
            fillBoard(80);
        } else if (e.getSource().equals(this.hardGame)) {
            fillBoard(60);
        }
    }
 
    @Override
    public void keyTyped(KeyEvent e) {
        if (e.getKeyChar() < 49 || e.getKeyChar() > 57){
            e.consume();
        }
    }
 
    @Override
    public void keyPressed(KeyEvent e) {
 
    }
 
    @Override
    public void keyReleased(KeyEvent e) {
 
    }
}

Comments