我用Java做了一个简单的游戏。是否可以刷新控制台,以便每次收到新数据时它只显示?

时间:2012-03-02 22:26:52

标签: java

我的游戏将进行10轮,因此每次进入新一轮时,前几轮仍然可见。反正有没有让控制台自行刷新,以便只有你所在的圆形可见?

// 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:\\");
            }
        }
    }       
}

}

2 个答案:

答案 0 :(得分:3)

如果您的平台是UNIX / Linux并且您在终端上运行游戏,那么您需要做的就是发送一个清除屏幕的控制序列。您可以通过检查clear命令的作用来找出序列是什么,例如使用以下命令:

clear | od -t c

在大多数终端上显示

0000000  033   [   H 033   [   2   J                                    
0000007

实际上是两个ANSI / VT100控制序列: Cursor Home Erase Screen 。有关详细信息,请参阅ANSI/VT100 control sequences

您可以像这样从Java发送序列:

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

如果您正在寻找更便携的方法,您可以只显示足够多的换行符。但是,这会将光标定位在屏幕的底部。

答案 1 :(得分:1)

您想要“清除屏幕”或“清除控制台”

这是this question

的副本