Java Craps applet一直在崩溃?

时间:2013-12-06 03:22:32

标签: java applet

我正在为我的编程课制作一个craps applet,看起来我正在做的事情是对的吗?一旦我点击按钮滚动骰子,骰子就会从屏幕上消失,小程序会冻结,不允许我点击任何东西。如果我赢了或输了第一卷,那就是“掷骰子!”按钮被禁用(就像它应该是这样),但是当我点击“开始游戏!”时它没有重新激活,但我可以点击“开始游戏!”按钮。我无法弄清楚如何在整个游戏中保持点值相同。我很迷茫,不知道问题是什么,我什么都做不了。到目前为止,这是我对代码的失望:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class Craps extends JApplet implements ActionListener {
Button button1 = new Button("Start game!");
Button button2 = new Button("Roll dice!");
Random gen = new Random();
int buttonPressed = 0;
int die1 = 0;
int die2 = 0;
int wins = 0;
int losses = 0;
int point = 0;
String result = "";
Image[] dice = new Image[6];

public void init() {
    setLayout(null);
    setSize(400, 500);
    dice[0] = getImage(getCodeBase(), "dice1.gif");
    dice[1] = getImage(getCodeBase(), "dice2.gif");
    dice[2] = getImage(getCodeBase(), "dice3.gif");
    dice[3] = getImage(getCodeBase(), "dice4.gif");
    dice[4] = getImage(getCodeBase(), "dice5.gif");
    dice[5] = getImage(getCodeBase(), "dice6.gif");

    button1.setBounds(50,300,125,20);
    button1.addActionListener(this);
    add(button1);

    button2.setBounds(250,300,125,20);
    button2.addActionListener(this);
    add(button2);
}

public void paint (Graphics g) {
    super.paint(g);
    //background
    g.setColor(Color.black);
    g.fillRect(0, 0, 400, 500);
    //table
    g.setColor(Color.cyan);
    g.fillRoundRect(50, 50, 300, 200, 50, 50);
    //dice
    g.drawImage(dice[die1], 50+(int)(Math.random()*275), 50+(int)(Math.random()*175), 32, 32, this);
    g.drawImage(dice[die2], 50+(int)(Math.random()*275), 50+(int)(Math.random()*175), 32, 32, this);


    if (buttonPressed == 2) {
        if (die1+die2==0||die1+die2==1||die1+die2==10) {
            result = "Sorry, you lose.";
            g.drawString(result, 50, 385);
            button2.setEnabled(false);
            if (buttonPressed == 1) {
                add(button2);
            }
        } else if (die1+die2==5||die1+die2==9) {
            result = "Congrats! You win!";
            g.drawString(result, 50, 385);
            button2.setEnabled(false);
            if (buttonPressed == 2) {
                add(button2);
            }
        } else {
            while (die1+die2!=0||die1+die2!=1||die1+die2!=5||die1+die2!=9||die1+die2!=10) { //2,3,7,11,12
                int point = die1 + die2;
                int sum = (die1+die2)+2;
                result = "You rolled "+sum+".";
                g.drawString(result, 50, 385);
                g.drawString("The point is "+((die1+die2)+2)+".", 50, 410);
            }    
                if (die1+die2==5) {
                    result = "Sorry, you lose.";
                    g.drawString(result, 50, 385);
            }
        }    
    } else if (buttonPressed == 1) {
        g.setColor(Color.white);
        g.drawString("Roll 'em!", 50, 350);
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == button1) {
        buttonPressed = 1;
    }    

    if (e.getSource() == button2) {
        buttonPressed = 2;
        die1 = gen.nextInt(6);
        die2 = gen.nextInt(6);
    }
    repaint();
}


}

3 个答案:

答案 0 :(得分:2)

这是一个无限循环:

        while (die1+die2!=0||die1+die2!=1||die1+die2!=5||die1+die2!=9||die1+die2!=10) { //2,3,7,11,12
            int point = die1 + die2;
            int sum = (die1+die2)+2;
            result = "You rolled "+sum+".";
            g.drawString(result, 50, 385);
            g.drawString("The point is "+((die1+die2)+2)+".", 50, 410);
        }

有两个原因。首先,您在进入循环后永远不会更新die1die2的值。

第二个是因为这样的条件:

while ( someValue != 0 || someValue != 1 ) {

永远都是真的。这是因为如果someValue0,那么它必然不是1,反之亦然。查看while循环之前的if / else结构,我怀疑你的条件是&&而不是||

此外,您不应该在paint中执行任何程序逻辑。只画画。这不仅会减慢绘画速度,而且会从重绘请求之外调用paint。您无法控制代码运行的时间。

答案 1 :(得分:1)

您不应该在paint方法中调用add(..),因为这很可能会触发失效,请再次调用paint(..),然后再次调用add(..)。此循环应该继续,直到发生某种异常或崩溃。此外,在buttonPressed方法处理0之后,您应该将paint(..)重置为初始值{{1}}。

答案 2 :(得分:1)

我建议你重新评估以下逻辑:

if (buttonPressed == 2) {
    if (die1+die2==0||die1+die2==1||die1+die2==10) {
        result = "Sorry, you lose.";
        g.drawString(result, 50, 385);
        button2.setEnabled(false);
        if (buttonPressed == 1) {
            add(button2);
        } 
    ...

您的内部有if (buttonPressed == 1) if (buttonPressed == 2),因此在此特定位置add(button2);永远不会发生。