2013-06-10 2 views
-3

내 컴퓨터 수업에 바위 종이 가위 유형 게임을하고 있습니다. 생성 된 이미지가로드되고 선택, 바위, 종이 또는 가위로 이미지가 생성되는 한 점수가 1 씩 증가합니다. 내 코드를 검토하고 잘못된 점을 말해 줄 수 있습니까? 감사!내 점수는 이미지로드에 걸리는 한 1 씩 증가합니다.

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

public class RockPaperScissors extends Applet implements ActionListener { 

    private Button rockButton; 
    private Button scissorsButton; 
    private Button paperButton; 
    private String buttonPressed = ""; // the label of the button pressed 
    private int computerValue; 
    private int myValue; 
    Image rockimg; 
    Image rock2img; 
    Image paperimg; 
    Image paper2img; 
    Image scissorsimg; 
    Image scissors2img; 

    int score; 
    int compscore; 
    int drawscore; 

    public void init() { 
     rockimg = getImage(getCodeBase(), "rock.gif"); 
     rock2img = getImage(getCodeBase(), "rock2.gif"); 
     paperimg = getImage(getCodeBase(), "paper.gif"); 
     paper2img = getImage(getCodeBase(), "paper2.gif"); 
     scissorsimg = getImage(getCodeBase(), "scissors.gif"); 
     scissors2img = getImage(getCodeBase(), "scissors2.gif"); 
     paperimg = paperimg.getScaledInstance(350, -1, Image.SCALE_SMOOTH); 
     paper2img = paper2img.getScaledInstance(350, -1, Image.SCALE_SMOOTH); 
     rockimg = rockimg.getScaledInstance(200, -1, Image.SCALE_SMOOTH); 
     rock2img = rock2img.getScaledInstance(200, -1, Image.SCALE_SMOOTH); 
     scissorsimg = scissorsimg 
       .getScaledInstance(200, -1, Image.SCALE_SMOOTH); 
     scissors2img = scissors2img.getScaledInstance(200, -1, 
       Image.SCALE_SMOOTH); 
     setSize(800, 600); 

     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); 
     score = 0; 
     compscore = 0; 
     drawscore = 0; 
     myValue = -1; 
     computerValue = -1; 
    } 

    Image offScreenBuffer; 

    @SuppressWarnings("deprecation") 
    public void update(Graphics g) { 
     Graphics gr; 
     if (offScreenBuffer == null 
       || (!(offScreenBuffer.getWidth(this) == this.size().width && offScreenBuffer 
         .getHeight(this) == this.size().height))) { 
      offScreenBuffer = this.createImage(size().width, size().height); 
     } 
     gr = offScreenBuffer.getGraphics(); 
     paint(gr); 
     g.drawImage(offScreenBuffer, 0, 0, this); 
    } 

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

    // paint is called each time a button is pressed 
    public void paint(Graphics g) { 

     g.fillRect(0, 0, getWidth(), getHeight()); 
     computerChoice(g); 

     ScoreBoard(g); 
     winner(g); 
    } 

    // Randomly generate one of the numbers 0, 1, 2. 
    int randomNumber() { 
     return (int) (Math.random() * 3); 
    } 

    // Prints on the screen one of the strings "Rock", "Scissors", or "Paper" if 
    // one of the generated numbers is 0, 1, or 2 
    void computerChoice(Graphics g) { 

     if (myValue == -1) { 
      g.drawString("", 200, 150); 
     } 
     if (myValue == 0) { 
      g.setColor(Color.WHITE); 
      g.drawString("Your choice: Rock", getWidth()/2 - 50, 170); 
      g.drawImage(rockimg, 0, 100, this); 
     } 
     if (myValue == 1) { 
      g.setColor(Color.WHITE); 
      g.drawString("Your choice: Scissors", getWidth()/2 - 50, 170); 
      g.drawImage(scissorsimg, 0, 100, this); 
     } 
     if (myValue == 2) { 
      g.setColor(Color.WHITE); 
      g.drawString("Your choice: Paper", getWidth()/2 - 50, 170); 
      g.drawImage(paperimg, 0, 100, this); 
     } 
     if (computerValue == 0) { 
      g.setColor(Color.WHITE); 
      g.drawString("Computer's choice: Rock", getWidth()/2 - 50, 190); 
      g.drawImage(rock2img, getWidth() - 240, 100, this); 
     } 
     if (computerValue == 1) { 
      g.setColor(Color.WHITE); 
      g.drawString("Computer's choice: Scissors", getWidth()/2 - 50, 
        190); 
      g.drawImage(scissors2img, getWidth() - 240, 100, this); 
     } 
     if (computerValue == 2) { 
      g.setColor(Color.WHITE); 
      g.drawString("Computer's choice: Paper", getWidth()/2 - 50, 190); 
      g.drawImage(paper2img, getWidth() - 340, 100, this); 
     } 
    } 

    // Translates "Rock" to 0, "Scissors" to 1, and "Paper" to 2. 
    void translator(String s) { 
     if (s.equals("Rock")) { 
      myValue = 0; 
     } else if (s.equals("Scissors")) { 
      myValue = 1; 
     } else if (s.equals("Paper")) { 
      myValue = 2; 
     } 
    } 

    // Decides the winner. 
    void winner(Graphics g) { 
     // Before playing, nothing happens, so print nothing. 
     if (computerValue == -1) { 
      g.setColor(Color.WHITE); 
      g.drawString("", 200, 100); 
     } 
     // If the machine and the player have the same thing, it is a draw. 
     else if (computerValue == myValue) { 
      g.setColor(Color.WHITE); 
      g.drawString("Draw", getWidth()/2 - 50, 210); 
      drawscore+=1; 
     } 
     // Computer beats if 
     // it has Rock and you have scissors, or it has scissors and you have 
     // paper or 
     // you have paper and computer has rock. 
     else if (computerValue == 0 && myValue == 1 || computerValue == 2 
       && myValue == 0 || computerValue == 1 && myValue == 2) { 
      g.setColor(Color.WHITE); 
      g.drawString("Computer wins", getWidth()/2 - 50, 210); 
      compscore+=1; 

     } 
     // You win in any other case 
     else { 
      g.setColor(Color.WHITE); 
      g.drawString("You win", getWidth()/2 - 50, 210); 
      score+=1; 
     } 
    } 

    void ScoreBoard(Graphics g){ 
     g.drawString("Computer Score:" + compscore, 300, 560); 
     g.drawString("Your Score:" + score, 300, 530); 
     g.drawString("Draws:" + drawscore, 300, 500); 
    } 
} 
+3

그리고 무엇이 잘못 되었습니까? 오류가 있습니까? 당신의 기대와 다른 행동? 여기에 어떤 질문이 있습니까? –

+0

아니요. 문제가 무엇인지 알려주고 어디에 지정해야합니다. 아니면 적어도 당신은 무엇을 얻고 당신은 무엇을 기대합니까? – Maroun

답변

1

애플릿 자체가 "업데이트"를 호출 할 때마다 페인트가 호출됩니다.

페인트 통화 "다른 모든 경우에 사용자의 승리", "승자"와 승자라고는 :

// You win in any other case 
    else { 
     g.setColor(Color.WHITE); 
     g.drawString("You win", getWidth()/2 - 50, 210); 
     score+=1; 
    } 

나는 페인트 방법에서와 actionPerformed 메소드로 점수 계산 코드를 이동합니다. 이것은 게임 로직과 뷰를 분리합니다.

관련 문제