需要在GWT中运行Java代码

时间:2014-12-04 04:52:44

标签: java gwt

我创建了一个新的Google Web应用程序项目(使用eclipse)并注意到代码在.server包中运行。我有一个用java编写的老虎机游戏,我试图将它实现到GWT中,但语法似乎完全不同。 (例如,我注意到\ n不起作用)

这是我想要实现的代码 - 我将如何做到这一点?

// clear console
static void clearConsole()
{
    for (int i=0; i<25; i++)
    {
        System.out.println("\n");
    }
}


// actual slot machine algorithm
static void slots()
{

    Scanner input = new Scanner (System.in);

    char newGame = ' ';

    int chips = 100;
    int bet = 0;


    do{
         System.out.println( "Try your luck on the SLOT MACHINE." +
                 "\nLine up (=^^=) or ))<>(( symbols to win big!\n\n" +
                "You currently have " + chips + " chips.\nHow much do you want to bet? ");



         //check for accidental char input
         try{
             bet = input.nextInt();

         }
         catch(Exception e){

             input.nextLine();
             System.out.println("NOT A VALID NUMBER\nHow much do you want to bet? ");
             bet = input.nextInt();
         }



        if (bet<=chips && bet>0){

            // to add some realism, slot machine will not execute until 'enter' pressed
            // then console cleared
            input.nextLine();
            System.out.println( "\nPress 'enter' to start the slot machine.");
            input.nextLine();
            clearConsole();

            String[] machine = {"(=^^=)", "(=^^=)", "))<>((", " XXXX ", " XXXX ", " :^{) ",
                                  " :^{) ", " (>_<)", " (>_<)", " [-O-]", " [-O-]"}; 
            Random rand = new Random();
            int slot1 = rand.nextInt(11);
            int slot2 = rand.nextInt(11);
            int slot3 = rand.nextInt(11);
            int slot4 = rand.nextInt(11);
            int slot5 = rand.nextInt(11);
            int slot6 = rand.nextInt(11);
            int slot7 = rand.nextInt(11);
            int slot8 = rand.nextInt(11);
            int slot9 = rand.nextInt(11);

            System.out.println( "-----------------------");
            System.out.printf( "%-7s %-7s %-7s %n%n", machine[slot1], machine[slot2], machine[slot3]);
            System.out.printf( "%-7s %-7s %-7s %n%n", machine[slot4], machine[slot5], machine[slot6]);
            System.out.printf( "%-7s %-7s %-7s %n", machine[slot7], machine[slot8], machine[slot9]);
            System.out.println( "-----------------------\n\n\n\n");

            // 3 wild cards
            if (slot4 == 2 && slot5 == 2 && slot6 == 2 ){
                bet = bet*100;
                chips = chips + bet;
                System.out.println( "JACKPOT! ");
            }

            //3 cats (inclusive of wild card)
            else if (   slot4 <3 && slot5 <3 && slot6 <3 ){
                bet = bet*5;
                chips = chips + bet;
                System.out.println( "YOU WIN! ");
            }

            // 3 of any other symbol (inclusive of wild card)
            else if( ((slot4==2 || slot4==3 || slot4==4) && (slot5==2 || slot5==3 || 
                                            slot5==4) && (slot6==2 || slot6==3 || slot6==4))
                    || ((slot4==2 || slot4==5 || slot4==6) && (slot5==2 || slot5==5 || 
                                            slot5==6) && (slot6==2 || slot6==5 || slot6==6))
                    || ((slot4==2 || slot4==7 || slot4==8) && (slot5==2 || slot5==7 || 
                                            slot5==8) && (slot6==2 || slot6==7 || slot6==8))
                    || ((slot4==2 || slot4==9 || slot4==10) && (slot5==2 || slot5==9 || 
                                       slot5==10) && (slot6==2 || slot6==9 || slot6==10)) ){
                bet = bet*3;
                chips = chips + bet;
                System.out.println( "YOU WIN! ");
            }

            // 2 cats
            else if ( slot4<2 && slot5<2 || slot4<2 && slot6<2 || slot5<2 && slot6<2){
                bet = bet*2;
                chips = chips + bet;
                System.out.println( "YOU WIN! ");
            }

            // 1 cat
            else if ( slot4 < 2 || slot5 < 2 || slot6 < 2 ){
                chips = chips + bet;
                System.out.println( "YOU WIN! ");
            }

            // nothing
            else{
                chips = chips - bet;
                System.out.print( "You Lose... ");
            }

            // display current amount of chips
            System.out.println( "You have " + chips + " chips.");


         }else{
            System.out.println( "You do not have enough chips to make that bet!");
         }


        if (chips > 0){
            System.out.println( "\nDo you wanna play this game again? (y/n): ");
            newGame = input.next().charAt(0);
            clearConsole();
        }

    } while (newGame =='y' && chips > 0 );


    input.close();

    System.out.println( "\nGame Over\n\n");
}

1 个答案:

答案 0 :(得分:0)

您可以将原始文本(甚至不是HTML)替换为具有固定空间等的元素(类似于&#34; pre&#34;元素)的System.out.println。

我建议您切换到类似Button的内容,然后输入&#34;,&#34; y&#34; /&#34; n&#34; ...和DoubleBox for the他们想赌的金额。

可能有一个更通用的控制台&#34;在浏览器方法,但它会有点奇怪?

也许最好从一个GWT样本开始,然后慢慢转换成你想要的样子?相当多的引导以启动并运行GWT应用程序。