How to remove a letter from the terminal and replace it with a new one in java?

54 Views Asked by At

So I am trying to make a Simon Says game where the user would have to repeat what the console has shown back to it. So for me so far I have made it work so that the characters have been showcased to the user and that each character is stored in a StringBuilder. I want to modify this code so that each character is only shown for a certain amount of time (maybe 0.6 seconds or 1 second etc), and then removed and the next character is shown etc during the for loop. I know Thread.sleep is used for the timing part, just the backspace/removal part I have no clue how to go around.

This is the piece of code I am stuck on and struggle to move forward from, I dont know what to add to the end of the for-loop. If needed, I'm using Eclipse

public static void gameStart() {
    repeat.level++;//originally set to 0, now increments each time
    StringBuilder sb = new StringBuilder();
    sb.ensureCapacity(100);// plan to have 100 levels
    for (int i = 0; i < repeat.level;i++) {
        Random rnd = new Random();
        char c = (char) ('a' + rnd.nextInt(26));//determines random char
        System.out.println(c);//shows char to user
        sb.append(c);//add to sb
        }
    System.out.println(sb);//just to make sure each char is getting added to sb
2

There are 2 best solutions below

1
Gkbinqi On

Actually, you can't remove the characters from the console, but you can play a trick. You can try this way in your for loop:

for (int i = 0; i < repeat.level; i++) {
            Random rnd = new Random();
            char c = (char) ('a' + rnd.nextInt(26)); // determines random char
            System.out.print(c); // print character to the console

            // Sleep to display the character for a certain amount of time
            try {
                Thread.sleep(1000); // millis
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt(); // Restore interrupted status
            }
            // Overwrite the character displayed with spaces to achieve "remove" it
            System.out.print("\b \b"); // Print backspace and space to clear the character
            sb.append(c);
        }
4
Reilas On

"... I know Thread.sleep is used for the timing part, just the backspace/removal part I have no clue how to go around. ..."

You can use the \b character to move the cursor backwards.

System.out.print(c);//shows char to user
sb.append(c);//add to sb
Thread.sleep(600);
System.out.print("\b");

Additionally, your code can be improved.

You can supply a capacity to StringBuilder via the constructor.

StringBuilder sb = new StringBuilder(100);

Add repeat.level to a local variable.

for (int i = 0, level = repeat.level; i < level;i++)

Move the rnd and c declarations to above the for-loop.

Random rnd = new Random();
char c;
for (int i = 0, level = 100; i < level;i++) {

}

And, you can utilize the RandomGenerator#nextInt method to provide a bounds.
Although, what you have is valid.

char c = (char) rnd.nextInt('a', 'z' + 1);

Here is the refactored code.

public static void gameStart() throws InterruptedException {
    repeat.level++;//originally set to 0, now increments each time
    StringBuilder sb = new StringBuilder(100);// plan to have 100 levels
    Random rnd = new Random();
    char c;
    for (int i = 0, level = repeat.level; i < level;i++) {
        c = (char) rnd.nextInt('a', 'z' + 1);//determines random char
        System.out.print(c);//shows char to user
        sb.append(c);//add to sb
        Thread.sleep(600);
        System.out.print("\b");
    }
    System.out.println(sb);//just to make sure each char is getting added to sb
}