dimanche 19 mars 2023

I can't randomize numbers for my numerical memory test

I am doing a program similar to the numeric memory test in Human Benchmark. Everything is alright except for the sequence randomization. It keeps displaying one single number, although it randomizes every new test, and adding onto that. Here's my code:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class numerical extends Thread implements ActionListener {
    Random random = new Random(); 
    ArrayList<Integer> number = new ArrayList<Integer>();
    StringBuffer string = new StringBuffer();
    int length = 1;
    boolean gameEnded = false, wait = true;

    JFrame frame = new JFrame("Numerical Memory");
    JPanel top = new JPanel();
    JPanel bottom = new JPanel();
    JLabel title;
    JLabel output = new JLabel();
    JTextField input;
    JButton button1 = new JButton(), button2 = new JButton();

    public numerical() throws InterruptedException {
        while (gameEnded == false) {
            wait = true;
            for (int i = 0; i < length; i++) {
                number.add(Math.abs(random.nextInt() % 9)); 
                System.out.println(number.get(i));
                string.append(number.get(i)); 
            }

            frame.setLayout(null); 
            frame.setSize(500, 300); 
            frame.setVisible(true); 
 
            title = new JLabel("Remember the number:"); 
            input = new JTextField(string.toString()); 
            input.setEditable(false);

            top.add(title);
            top.add(input);
            top.setLayout(new GridLayout(2, 1));
            top.setBounds(0, 0, 500, 200);

            bottom.setLayout(new GridLayout(1, 3));
            bottom.setBounds(0, 210, 500, 40);

            frame.add(top);
            frame.add(bottom);

            frame.revalidate();
            frame.repaint();

            Thread.sleep(length*1500);

            title.setText("What was the number?"); 
            
            input.setText("");
            input.setEditable(true);

            button1 = new JButton("Submit");
            button1.addActionListener(this);
            button1.setActionCommand("submit");

            output.setVisible(false);
            button2.setVisible(false);

            bottom.add(output);
            bottom.add(button1);
            bottom.add(button2); 

            frame.revalidate();
            frame.repaint();

            while (wait == true) {
                System.out.print("");
            }
            
            top.remove(title);
            top.remove(input);
            bottom.remove(output);
            bottom.remove(button1);
            bottom.remove(button2);
            frame.remove(top);
            frame.remove(bottom);

            frame.revalidate();
            frame.repaint();
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "submit") {
            int x = Integer.parseInt(input.getText());
            int y = Integer.parseInt(string.toString());
            System.out.println("Input: " + x);
            System.out.println("Number: " + y);
            if (x == y) {
                output.setText("CORRECT!");
                button1.setText("Next");
                button1.setActionCommand("next");
            }
            else {
                button2.setVisible(true);
                output.setText("INCORRECT!");
                button1.setText("Retry");
                button1.setActionCommand("retry");
                button2.setText("Back");
            }
        }
        if (e.getActionCommand() == "next") {
            wait = false;
        }
        if (e.getActionCommand() == "retry") {
            wait = false;
            length = 0;
        }
        if (e.getSource() == button2) {
            wait = false;
            frame.dispose();
        }

    }

    public static void main(String[] args) {
        try {
            new numerical();
        }
        catch (InterruptedException exception) {
            exception.printStackTrace();
        }
    }
}

I tried thread.sleep at first but later found out it was incompatible with swing so I had to change it, and it resulted to this. Before changing it, it wasn't any better. The numbers were just stacking. How can I randomize my numbers?




Aucun commentaire:

Enregistrer un commentaire