Moortiii

Ling - Assignment03

Feb 19th, 2018
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.util.ArrayList;
  7. import java.util.Random;
  8.  
  9. public class GUI extends JFrame implements ActionListener, KeyListener {
  10.     private JMenuBar menuBar = new JMenuBar();
  11.     private JMenu fileMenu = new JMenu("File");
  12.     private JMenu newGame = new JMenu("New game");
  13.     private JMenuItem deleteGame = new JMenuItem("Delete game");
  14.     private JMenuItem easyGame = new JMenuItem("Easy game");
  15.     private JMenuItem mediumGame = new JMenuItem("Medium game");
  16.     private JMenuItem hardGame = new JMenuItem("Hard game");
  17.     private JTextField[][] gameGrid = new JTextField[9][9];
  18.  
  19.     public GUI() {
  20.         this.setLayout(new GridLayout(9, 9));
  21.         this.setTitle("Sudoku");
  22.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23.  
  24.         fillEmptyGrid();
  25.         addKeyListeners();
  26.         addMenuItems();
  27.         addActionListeners();
  28.         fillBoard(100);
  29.  
  30.         this.setVisible(true);
  31.         this.pack();
  32.     }
  33.  
  34.     private void fillEmptyGrid() {
  35.         for (int i = 0; i < 9 ; i++)
  36.         {
  37.             for (int j = 0; j < 9; j++)
  38.             {
  39.                 this.gameGrid[i][j] = new JTextField("");
  40.                 this.gameGrid[i][j].setPreferredSize(new Dimension(50, 50));
  41.                 this.gameGrid[i][j].setHorizontalAlignment(JTextField.CENTER);
  42.                 this.add(this.gameGrid[i][j]);
  43.             }
  44.         }
  45.     }
  46.  
  47.     private void addKeyListeners() {
  48.         for (int i = 0; i < 9 ; i++)
  49.         {
  50.             for (int j = 0; j < 9; j++)
  51.             {
  52.                 this.gameGrid[i][j].addKeyListener(this);
  53.             }
  54.         }
  55.     }
  56.  
  57.     private void addActionListeners() {
  58.         this.deleteGame.addActionListener(this);
  59.         this.newGame.addActionListener(this);
  60.         this.easyGame.addActionListener(this);
  61.         this.mediumGame.addActionListener(this);
  62.         this.hardGame.addActionListener(this);
  63.     }
  64.  
  65.     private void addMenuItems() {
  66.         this.setJMenuBar(this.menuBar);
  67.         this.menuBar.add(this.fileMenu);
  68.         this.fileMenu.add(this.newGame);
  69.         this.fileMenu.add(this.deleteGame);
  70.         this.newGame.add(this.easyGame);
  71.         this.newGame.add(this.mediumGame);
  72.         this.newGame.add(this.hardGame);
  73.     }
  74.  
  75.     private ArrayList<Integer> generateNumbers()
  76.     {
  77.         ArrayList<Integer> sudokuNumbers = new ArrayList<>();
  78.         for (int i = 0; i < 9; i++) {
  79.             for (int j = 0; j < 9; j++) {
  80.                 sudokuNumbers.add((j + 1));
  81.             }
  82.         }
  83.         return sudokuNumbers;
  84.     }
  85.  
  86.     private void clearGrid() {
  87.         for (int i = 0; i < 9; i++) {
  88.             for (int j = 0; j < 9; j++) {
  89.                 this.gameGrid[i][j].setText("");
  90.                 this.gameGrid[i][j].setEditable(true);
  91.             }
  92.         }
  93.     }
  94.  
  95.     private void fillBoard(int fillPercentage){
  96.         clearGrid();
  97.         ArrayList<Integer> sudokuNumbers = generateNumbers();
  98.         Random randomGenerator = new Random();
  99.  
  100.         for (int i = 0; i < 9; i++) {
  101.             for (int j = 0; j < 9; j++) {
  102.                 int tolerance = randomGenerator.nextInt(100);
  103.                 if(tolerance < fillPercentage) {
  104.                     JTextField currentCell = this.gameGrid[i][j];
  105.                     int randomIndex = randomGenerator.nextInt(sudokuNumbers.size());
  106.                     String randomNumber = Integer.toString(sudokuNumbers.get(randomIndex));
  107.                     sudokuNumbers.remove(randomIndex);
  108.                     currentCell.setText(randomNumber);
  109.                     currentCell.setEditable(false);
  110.                 }
  111.             }
  112.         }
  113.     }
  114.  
  115.     @Override
  116.     public void actionPerformed(ActionEvent e)
  117.     {
  118.         if (e.getSource().equals(this.deleteGame)) {
  119.             clearGrid();
  120.         } else if (e.getSource().equals(this.easyGame)) {
  121.             fillBoard(100);
  122.         } else if (e.getSource().equals(this.mediumGame)) {
  123.             fillBoard(80);
  124.         } else if (e.getSource().equals(this.hardGame)) {
  125.             fillBoard(60);
  126.         }
  127.     }
  128.  
  129.     @Override
  130.     public void keyTyped(KeyEvent e) {
  131.         if (e.getKeyChar() < 49 || e.getKeyChar() > 57){
  132.             e.consume();
  133.         }
  134.     }
  135.  
  136.     @Override
  137.     public void keyPressed(KeyEvent e) {
  138.  
  139.     }
  140.  
  141.     @Override
  142.     public void keyReleased(KeyEvent e) {
  143.  
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment