0

Edit: Since there is no command for clearing the console in java, this has to be implemented via GUI.

i am writing a small console based game and i want to ask how do i print my matrix at the same location in the console? I want to let it look like changes in the matrix appear in "realtime", which means the user does not see that a new matrix was drawn.

the function level creates a new matrix and display prints out the current matrix:

/**
 * creates a new gameworld(matrix of size mxn)
 * @param m
 * @param n
 * @return
 */
public int[][] level(int m, int n){
    //initialize gameworld
    int[][] lGW = new int[m][n];

    //fill matrix with 0
    for(int i = 0; i <= m-1; i++ ){
        for(int j = 0; j <= n-1; j++ ){
            lGW[i][j] = 0;
        }
    }
    return lGW;
}

/**
 * show the gameworld in the console
 */
public void displayGameWorld(){
    for(int i = 0; i <= gameworld.length-1; i++){
        for(int j = 0; j <= gameworld[0].length-1; j++){
            System.out.printf("%3d", gameworld[i][j]);
        }
        System.out.println();
    }
}
M.Mac
  • 699
  • 3
  • 14

1 Answers1

0

I am not an expert on the topic, but I just had an idea. I remember when I was kid and we used to work on a console (don't remember what operating system), when you hit clrscr, it LITERALLY scrolls all the way down and pushes everything you typed out of your view. Using this concept,
1. Do you have a function that gets called when the user does what he does for the matrix to change? 2. If yes, clearscreen in the beginning of the function.
3. Print the matrix at the top of the screen.
4. Let user perform his task that would eventually result in the matrix to change.
5. Clear screen again and reprint the matrix?
This way, the matrix is on the top of the screen, before and after the change?

I am sorry if that sounded really stupid. I just had this idea when I was reading your question.

Crazy Cucumber
  • 479
  • 8
  • 36
  • This sounds simple, i will try it out thanks! And for me there are no stupid answers/questions(if the person wants to help you :)) – M.Mac Feb 21 '17 at 15:35
  • 2
    They're talking about 'java'. Java doesn't have a `clrscr` option. – progyammer Feb 21 '17 at 15:36
  • 1
    No there isn't. There is one in C an C++ but not in Java. – progyammer Feb 21 '17 at 15:38
  • You can't just erase the console at runtime. You can only get a fresh console when you run the program all over again. – progyammer Feb 21 '17 at 15:38
  • Thats good to know! I will use the GUI classes as you adviced @progyammer. I think there are many ways to let the GUI look like an old console :) – M.Mac Feb 21 '17 at 15:41
  • 2
    @CrazyCucumber These techniques don't work unless you have specific drivers installed or create a platform yourself that allow java programs to do that. – progyammer Feb 21 '17 at 15:43
  • @CrazyCucumber Plus, these are platform specific techniques, can't be generalized. – progyammer Feb 21 '17 at 15:44