【问题标题】:Rock, paper, scissors game Java applet石头、纸、剪刀游戏Java小程序
【发布时间】:2015-11-25 20:51:13
【问题描述】:

我的 java 小程序有问题。我制作了一个石头剪刀布游戏,但有 2 个问题我无法解决: 1.如何加载没有左侧岩石图像的小程序(玩家选择) 2. 玩家和电脑每输一局不减1分

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class RockScissorsPaper extends Applet implements ActionListener
{
private Button rockButton;
private Button scissorsButton;
private Button paperButton;
private String buttonPressed = "";
private int computerValue = -1;
private int myValue;
private int playerScore = 10;
private int computerScore = 10;
private int drawScore = 0;
private Image imgRock;
private Image imgScissors;
private Image imgPaper;

public void init()
{
    rockButton = new Button("Rock");
    scissorsButton = new Button("Scissors");
    paperButton = new Button("Paper");
    add(rockButton);
    add(scissorsButton);
    add(paperButton);
    rockButton.addActionListener(this);
    scissorsButton.addActionListener(this);
    paperButton.addActionListener(this);
    imgRock = getImage(getCodeBase(), "rock.jpg");
    imgScissors = getImage(getCodeBase(), "scissors.jpg");
    imgPaper = getImage(getCodeBase(), "paper.jpg");
}

public void actionPerformed(ActionEvent event)
{
    buttonPressed = ((Button)event.getSource()).getLabel();
    computerValue = randomNumber012();
    translator(buttonPressed);
    repaint();
}

public void paint(Graphics g)
{
    super.paint(g);
    rockButton.setLocation(20,260);
    rockButton.setSize(70,35);
    paperButton.setLocation(210, 260);
    paperButton.setSize(70, 35);
    scissorsButton.setLocation(380, 260);
    scissorsButton.setSize(90, 35);
    choice(g);
    g.drawString("Player's Wins: " + playerScore, 10, 20);
    g.drawString("Computer's Wins: " + computerScore, 180, 20);
    g.drawString("Draws: " + drawScore, 400, 20);
    g.drawString("Your Choice: " + buttonPressed, 20, 60);
    g.drawString("Computer's Pick: ", 350, 60);
    winner(g, computerValue, myValue);
}

int randomNumber012()
{
    return (int)(Math.random()*3);
}

public void translator(String s)
{
    if(s.equals("Rock"))
    {
        myValue = 0;
    }
    else if(s.equals("Scissors"))
    {
        myValue = 1;
    }
    else if(s.equals("Paper"))
    {
        myValue = 2;
    }
}

public void choice(Graphics g)
{
    if(myValue == 0)
    {
        g.drawImage(imgRock, 20, 100, 100, 60, this);
    }
    else if(myValue == 1)
    {
        g.drawImage(imgScissors, 20, 100, 100, 60, this);
    }
    else if(myValue == 2)
    {
        g.drawImage(imgPaper, 20, 100, 100, 60, this);
    }

    if(computerValue == 0)
    {
        g.drawString("Computer's Pick: Rock", 350, 60);
        g.drawImage(imgRock, 350, 100, 100, 60, this);
    }
    else if(computerValue == 1)
    {
        g.drawString("Computer's Pick: Scissors", 350, 60);
        g.drawImage(imgScissors, 350, 100, 100, 60, this);
    }
    else if(computerValue == 2)
    {
        g.drawString("Computer's Pick: Paper", 350, 60);
        g.drawImage(imgPaper, 350, 100, 100, 60, this);
    }
}

public void winner(Graphics g, int cv, int mv)
{
    if(cv == -1)
    {
        g.drawString("", 200, 100);
    }
    else if(cv == mv)
    {
        g.drawString("Draw", 200, 250);
        drawScore = drawScore + 1;
    }
    else if(cv == 0 && mv == 1 || cv == 2 && mv == 0 || cv == 1 && mv == 2)
    {
        g.drawString("Computer Wins", 200, 250);
        playerScore = playerScore - 1;
    }
    else
    {
        g.drawString("You Win!", 200, 250);
        computerScore = computerScore - 1;
    }

    if (playerScore == 0)
    {
        System.out.println("You lost!");
        playerScore = 10;
        computerScore = 10;
        drawScore = 0;
    }
    else if(computerScore == 0)
    {
        System.out.println("You won!");
        playerScore = 10;
        computerScore = 10;
        drawScore = 0;
    }
}
}

【问题讨论】:

  • 您自己调试了吗?你真的来过playerScore = playerScore - 1;吗?
  • 1) 为什么要编写小程序?如果是老师指定的,请参考Why CS teachers should stop teaching Java applets。 2) 为什么使用 AWT?请参阅this answer 了解放弃 AWT 使用支持 Swing 的组件的许多充分理由。

标签: java applet awt


【解决方案1】:

如何加载没有左侧岩石图像的小程序(玩家选择)

当玩家尚未做出选择时,您需要为您的myValue 变量设置第四种情况。现在您只有三个潜在值,默认值为 0,这就是为什么您在做出选择之前会看到岩石图像。

每输一局,玩家和电脑的分数不会减1

您在应该使用enums 和booleans 的地方使用ints,这使您的代码更难理解。我强烈建议重构您的代码以使用enums 和booleans 而不是ints。

此外,您总是从 paint() 方法调用每个方法。不要那样做。相反,只在玩家实际做出选择时调用检查获胜者的函数,大概是通过按钮点击。

最后,您还在 paint() 方法中调用了 setLocation(),这是个坏主意。您应该只调用一次setLocation(),或者更好的是,使用布局管理器。

【讨论】:

    猜你喜欢
    • 2015-01-15
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 2017-01-17
    • 2020-05-22
    • 1970-01-01
    相关资源
    最近更新 更多