0

My game will play 10 rounds, so each time you advance to a new round, the preceding rounds are still visible. Is there anyway to have the console refresh itself so that only the round you are on is visible?

// Rock Paper Scissor Shoot Game
import java.util.Random;
import java.util.Scanner;

public class RockPaperSciccor {
public static void main(String[] args){

    int wins = 0; 
    int losses = 0;
    int rnd;


    System.out.print("*************************************\n*");
    System.out.println("    Rock, Paper, Scissor, Shoot!   *\n* \tBy: Alex Hollander \t    *");
    System.out.println("*\t\t\t\t    *");
    System.out.print("*************************************\n");
    System.out.println("\t\t\t\t\t Instructions:\n\t\t\t\t\t Choose a number 1-3\n \t\t\t\t\t to play the computer.");

    // Play's 10 rounds before terminating
    for(rnd=10;rnd>=1;rnd--){

            //Display's rounds left on the right side
        System.out.println("\t\t\t\t\t  ____________\n\t\t\t\t\t |1 = Rock   |\n\t\t\t\t\t |2 = Paper  |\n\t\t\t\t\t |3 = Scissor|");
        System.out.println("\t\t\t\t\t ~~~~~~~~~~~~~~");
        System.out.println("\t\t\t\t\t|Rounds Left:" + (rnd) + "|");
        System.out.println("\t\t\t\t\t ~~~~~~~~~~~~~~");

        // Creates the PC, who chooses a random # 1-3
    Random GAME = new Random();
    int PC = 1+GAME.nextInt(3);

    //User input
    Scanner input = new Scanner (System.in);
    int SCISSOR, ROCK, PAPER;
    ROCK = 1;
    PAPER = 2;
    SCISSOR= 3;

    int USER =  input.nextInt();

    //If the user enters a value greater then 3 or less than 1 it will terminate the prgm
    //and display an error msg
    while (USER > 3 || USER < 1) {
        System.err.println("Incorrect value entered. Restart the Game\nand choose a number 1-3");
        return;
        }
    System.out.println("___________________");

    //All Possible Outcomes
    //Draw
    if(USER == PC){
        if(USER == SCISSOR){
            System.out.println("You Both Played Scissor");
        }
        if(USER == ROCK){
            System.out.println("You Both Played Rock");
        }
        if(USER == PAPER){
            System.out.println("You Both Played Paper");
        }
        System.out.println("Draw :\\");
        System.out.println("|~~~~~~~~~~~~~~~~~~|");
        System.out.println(" Wins: " + wins + "|Losses: " + losses);
        System.out.println("|~~~~~~~~~~~~~~~~~~|");

    }
    //User wins
    if(USER == SCISSOR)
        if(PC == PAPER){
        System.out.println("You: Scissor\nPC: Paper");
        System.out.println("Scissor Cuts Paper");
        System.out.println("You Win! :]");
        wins++;
        System.out.println("|~~~~~~~~~~~~~~~~~~|");
        System.out.println(" Wins: " + wins + "|Losses: " + losses);
        System.out.println("|~~~~~~~~~~~~~~~~~~|");

    }
    //Pc wins
    else if(PC == ROCK){
        System.out.println("You: Scissor \nPC: Rock");
        System.out.println("Rock Breaks Scissor!");
        System.out.println("PC Wins! >:D");
        losses++;
        System.out.println("|~~~~~~~~~~~~~~~~~~|");
        System.out.println(" Wins: " + wins + "|Losses: " + losses);
        System.out.println("|~~~~~~~~~~~~~~~~~~|");

    }
    //User wins
    if(USER == ROCK)
        if(PC == SCISSOR ){
        System.out.println("You: Rock\nPC: Scissor");
        System.out.println("Rock Breaks Scissor");
        System.out.println("You Win! :]");
        wins++;         
        System.out.println("|~~~~~~~~~~~~~~~~~~|");
        System.out.println(" Wins: " + wins + "|Losses: " + losses);
        System.out.println("|~~~~~~~~~~~~~~~~~~|");
    }   
    //Pc wins
    else if (PC == PAPER){
        System.out.println("You: Rock\nPC: Paper");
        System.out.println("Paper Covers Rock!");
        System.out.println("PC Wins! >:D");
        losses++;
        System.out.println("|~~~~~~~~~~~~~~~~~~|");
        System.out.println(" Wins: " + wins + "|Losses: " + losses);
        System.out.println("|~~~~~~~~~~~~~~~~~~|");
    }
    //User Wins
    if(USER == PAPER)
        if(PC == ROCK){
        System.out.println("You: Paper\nPC: Rock");
        System.out.println("Paper Covers Rock");
        System.out.println("You Win! :]");
        wins++;
        System.out.println("|~~~~~~~~~~~~~~~~~~|");
        System.out.println(" Wins: " + wins + "|Losses: " + losses);
        System.out.println("|~~~~~~~~~~~~~~~~~~|");
    }
    // Pc Wins 
    else if (PC == SCISSOR){
        System.out.println("You: Paper\nPC: Scissor");
        System.out.println("Scissor Cuts Paper!");
        System.out.println("PC Wins! >:D");
        losses++;
        System.out.println("|~~~~~~~~~~~~~~~~~~|");
        System.out.println(" Wins: " + wins + "|Losses: " + losses);
        System.out.println("|~~~~~~~~~~~~~~~~~~|");
        }
        if(rnd==1){// I used .err so that in eclipse, the text would be red

        System.err.println("\t\t\t\t\t" +
                " Game Over!");
            if(wins<losses){
                System.err.println("\t\t\t\t\t YOU HAVE BEEN BEATEN BY THE PC!\n \t\t\t\t\t\t>:D");
            }else if(wins>losses){
                System.err.println("\t\t\t\t\t YOU BEAT THE COMPUTER!\n \t\t\t\t\t\t:]");
            }else if(wins==losses){
                System.err.println("\t\t\t\t\t STALEMATE!\n \t\t\t\t\t\t:\\");
            }
        }
    }       
}

}

  • possible duplicate of [clear screen option in java](http://stackoverflow.com/questions/1682212/clear-screen-option-in-java) – Madara's Ghost Jun 09 '12 at 08:14

2 Answers2

3

If your platform is UNIX/Linux and you run your game on a terminal, all you need to do is send a control sequence which clears the screen. You can find out what the sequence is by checking what the clear command does, e.g. with the following command:

clear | od -t c

On most terminals this displays

0000000  033   [   H 033   [   2   J                                    
0000007

which is really two ANSI/VT100 control sequences: Cursor Home and Erase Screen. See ANSI/VT100 control sequences for details.

You can send the sequence from Java like this:

System.out.println("\033[H\033[2J");

If you're looking for a more portable way of doing this, you can just display a sufficiently large number of newline characters. This however will position the cursor at the bottom of the screen.

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
1

You want to "clear the screen" or "clear the console"

This is a duplicate of this question

Community
  • 1
  • 1
Mark Harviston
  • 660
  • 4
  • 18