岩石,剪刀,纸

时间:2014-11-03 10:42:29

标签: java jpanel

我刚刚制作了摇滚,剪刀,纸质游戏,直到现在还有点远。当用户或计算机首先达到3分时,我不再真正知道(尝试了一段时间)让游戏停止。

我有一些想法来制作一个if语句== 3 = wins。但是,如果我这样做,那么按钮也应该被禁用..我在想这样的东西,但我不能再得到它了!所以想法会很棒!

public void actionPerformed(ActionEvent e) {
        int Choice= 0;

        if(e.getSource() == exit) {
            this.dispose();
            System.exit(0); 
        }
        else if(e.getSource() == newgame) {
            this.dispose();
            new RockScissorsPaper();
        }
        else if (e.getSource() == Rock) {
            Choice= Rock;       
        }
        else if (e.getSource() == Scissors) {
            Choice= Scissors;
        }
        else {
            Choice= Paper; 
        }
        calculate(Choice);

    }//actionPerformed


     / ** 
     * Computer game selection is randomized and compared with the user 
     * Choice (method arguments). The game result is stored and displayed 
     * UI. 
     * / 
    public void calculate(int val){

        //Get user choise
        UserChoose= val;     

        // randomise the computer's choice and typing on the integer
        ComputerChoose = (int)(Math.random() * 3);

        //show result

        resultatLabel.setHorizontalAlignment(0); 
        infoLabel.setHorizontalAlignment(0);    
        infoLabel.setText("Result round " + ++round +":");  

        if (UserChoose == ComputerChoose)
            resultatLabel.setText("Tie!");   

        else if (UserChoose == Rock &&  ComputerChoose == Scissors || 
                UserChoose == Scissors &&  ComputerChoose == Paper||
                UserChoose == Paper &&  ComputerChoose == Rock) {
            resultatLabel.setText("You win! One more game?");
            användarResultat.setText(" Your wins: " + ++UserWins+ "st");
        }
        else {
            resultatLabel.setText(" You Lose! Try again!");
            datorResultat.setText(" Computer wins: " + ++computerWins + "st");
        }
    }

    public static void main(String[] args) {
        new RockScissorsPaper();
    }


}

1 个答案:

答案 0 :(得分:0)

只需向Stensaxpase课程添加属性,例如

private int numberOfWinsUser;
private int numberOfWinsComputer;

每次有人获胜(在berakna中)你都会更新它们并检查是否有人获得了3胜。由于此事件被驱动,因此无需使用循环。