Rock Paper Scissors图形小程序上的对象与类方法编程?

时间:2015-05-07 01:58:16

标签: java oop applet

我必须在编程设计某种类型的游戏时做一个最终项目,我决定使用石头剪刀。我开始编写程序而没有任何面向对象的编程风格,并意识到我必须改变它以获得项目的最大分数。但是,当我以OOP形式重写它时,它不会像没有OOP创建的那样工作。

这是我的OOP计划:

import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.util.Scanner;
import java.util.Arrays;

public class RockPaperScissorss extends Applet
{
    protected String answer;
    protected Image rock, paper, scissors;
    protected boolean playAgain;
    public void init()
    {
        answer = Expo.enterStringGUI("What is your first draw?");
        rock = getImage(getDocumentBase(),"Rock.gif");
        paper = getImage(getDocumentBase(),"Paper.gif");
        scissors = getImage(getDocumentBase(),"Scissors.gif");
    }

    public void paint(Graphics g)
    {
        Output output = new Output();
        playAgain = true;
        while(playAgain == true)
        {
        output.displayYouAndComp(g);
        output.getCompDraw();
        output.displayPic1(g);
        output.displayComp1(g);
        output.whoWon();
        output.printResult(g);
        output.playAgain();
        if(playAgain == true)
           init();
        }
    }
}

class Initialize extends RockPaperScissorss
{
    protected String again;  
    public void displayYouAndComp(Graphics g)
    {
        Expo.setFont(g,"Algerian",Font.BOLD,50);
        Expo.drawString(g,"You",0,50);
        Expo.drawString(g,"Computer",500,50);
    }

    public boolean playAgain()
    {
        again = Expo.enterStringGUI("Would you like to play again? (Y/N)");
        if(again.equals("Y") || again.equals("y"))
           playAgain = true;
        else
           playAgain = false;
        return playAgain;
    }
}

class Person extends Initialize
{       
    public void displayPic1(Graphics g)
    {
        if(answer.equals("rock"))
        g.drawImage(rock,0,100,this);
        else if(answer.equals("paper"))
        g.drawImage(paper,0,100,this);
        else 
        g.drawImage(scissors,0,100,this);
    }
}

class Computer extends Person
{    
    protected int draw;    
    public void getCompDraw()
    {
        draw = Expo.random(0,2);
    }

    public void displayComp1(Graphics g)
    {
        if(draw == 1)
        g.drawImage(rock,500,100,this);
        else if(draw == 2)
        g.drawImage(paper,500,100,this);
        else 
        g.drawImage(scissors,500,100,this);
    }
}

class Output extends Computer
{   
    protected boolean won;
    protected int tieGame;
    protected String result;
    protected String newArray[];    

    public String[] whoWon()
    {
        newArray = new String[1];
        String personAnswers[] = {answer};
        int compAnswers[] = {draw};
        for(int k = 0; k < 1; k++)
        {
           if(personAnswers[k].equals("rock"))
           {
              switch(compAnswers[k])
              {
                case 1: //rock
                     newArray[k] = "tie";
                     break;
                case 2: //paper
                     newArray[k] = "loss";
                     break;
                case 3: //scissors
                     newArray[k] = "win";
                     break;
              }
           }
           else if(personAnswers[k].equals("paper"))
           {
               switch(compAnswers[k])
               {
                   case 1:
                        newArray[k] = "win";
                        break;
                   case 2:
                        newArray[k] = "tie";
                        break;
                   case 3:
                        newArray[k] = "loss";
                        break;
               }
           }
           else
           {
               switch(compAnswers[k]) //equals scissors
               {
                   case 1: 
                        newArray[k] = "loss";
                        break;
                   case 2:
                        newArray[k] = "win";
                        break;
                   case 3:
                        newArray[k] = "tie";
                        break;
               }
           }
        }
        return newArray;
    }

    public void printResult(Graphics g) 
    {
        String[] result = newArray;
        Expo.setFont(g,"Algerian",Font.BOLD,50);
        int x = 200;
        for(int k = 0; k < 1; k++)
        {
        if(result[k].equals("loss"))
           Expo.drawString(g,"You lost!",700,x);
        else if(result[k].equals("tie"))    
           Expo.drawString(g,"It's a tie!",700,x);
        else
           Expo.drawString(g,"You won!",700,x);
        }
    }
}

vs我的非OOP代码:

import javax.swing.*;
    import java.awt.*;
    import java.applet.*;
    import java.util.Scanner;
    import java.util.Arrays;

    public class RockPaperScissorsss extends Applet
    {
        String answer1, answer2, answer3; //first draw, etc.
        int firstDraw, secondDraw, thirdDraw;
        int tieGame;
        String[] newArray;
        Image rock, paper, scissors;
        boolean playAgain;
        String again;

        public void init()
        {
            answer1 = Expo.enterStringGUI("What is your first draw?");
            rock = getImage(getDocumentBase(),"Rock.gif");
            paper = getImage(getDocumentBase(),"Paper.gif");
            scissors = getImage(getDocumentBase(),"Scissors.gif");
            newArray = new String[1];
        }

        public void paint(Graphics g)
        {
            playAgain = true;
            while(playAgain == true)
            {
            displayYouAndComp(g);
            getCompDraw();
            displayPic1(g);
            displayComp1(g);
            whoWon();
            printResult(g);
            playAgain();
            if(playAgain == true)
               init();
            }
        }

        public boolean playAgain()
        {
            again = Expo.enterStringGUI("Would you like to play again? (Y/N)");
            if(again.equals("Y") || again.equals("y"))
               playAgain = true;
            else
               playAgain = false;
            return playAgain;
        }

        public void displayYouAndComp(Graphics g)
        {
            Expo.setFont(g,"Algerian",Font.BOLD,50);
            Expo.drawString(g,"You",0,50);
            Expo.drawString(g,"Computer",500,50);
        }

        public void getCompDraw()
        {
            firstDraw = Expo.random(0,2);
            secondDraw = Expo.random(0,2);
            thirdDraw = Expo.random(0,2);
        }

        public void displayPic1(Graphics g)
        {
            if(answer1.equals("rock"))
            g.drawImage(rock,0,100,this);
            else if(answer1.equals("paper"))
            g.drawImage(paper,0,100,this);
            else 
            g.drawImage(scissors,0,100,this);
        }

        public void displayComp1(Graphics g)
        {
            if(firstDraw == 1)
            g.drawImage(rock,500,100,this);
            else if(firstDraw == 2)
            g.drawImage(paper,500,100,this);
            else 
            g.drawImage(scissors,500,100,this);
        }

        public String[] whoWon()
        {
            String personAnswers[] = {answer1};
            int compAnswers[] = {firstDraw};
            for(int k = 0; k < 1; k++)
            {
               if(personAnswers[k].equals("rock"))
               {
                  switch(compAnswers[k])
                  {
                    case 1: //rock
                         newArray[k] = "tie";
                         break;
                    case 2: //paper
                         newArray[k] = "loss";
                         break;
                    case 3: //scissors
                         newArray[k] = "win";
                         break;
                  }
               }
               else if(personAnswers[k].equals("paper"))
               {
                   switch(compAnswers[k])
                   {
                       case 1:
                            newArray[k] = "win";
                            break;
                       case 2:
                            newArray[k] = "tie";
                            break;
                       case 3:
                            newArray[k] = "loss";
                            break;
                   }
               }
               else
               {
                   switch(compAnswers[k]) //equals scissors
                   {
                       case 1: 
                            newArray[k] = "loss";
                            break;
                       case 2:
                            newArray[k] = "win";
                            break;
                       case 3:
                            newArray[k] = "tie";
                            break;
                   }
               }
            }
            return newArray;
        }

        public void printResult(Graphics g) 
        {
            String[] result = newArray;
            Expo.setFont(g,"Algerian",Font.BOLD,50);
            int x = 200;        
            for(int k = 0; k < 1; k++)
            {
            if(result[k].equals("loss"))
               Expo.drawString(g,"You lost!",700,x);
            else if(result[k].equals("tie"))    
               Expo.drawString(g,"It's a tie!",700,x);
            else
               Expo.drawString(g,"You won!",700,x);
            x += 200;
            }
        }
    }

如果代码难以理解,我会提前道歉。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我怀疑原因是因为&#34;输出&#34;延伸&#34; RockPaperScissorss&#34;当你执行&#34;输出输出=新输出();&#34;时,扩展&#34;小程序&#34;在paint()中,您获得了一个不能访问&#34;显示&#34;的Applet的不同实例。你需要修复你的继承。

但是,我必须补充一点,你的程序无法实现OOP - 你只是展示了OOP的一个原则:继承(extends)。你缺少另外两个原则:多态(实现)和封装(在类和实现的其他部分共享受保护的布尔playAgain违反了这个原则)。所以,即使你让代码工作,我也会在你的项目上失败,除非你展示了所有的OOP原则。