zoukankan      html  css  js  c++  java
  • 1.14 Headfirstjava第五章 简单游戏代码

    书上的设计过程:

    流程图 --> 找出需要的类 --> 设计类的实例变量和方法 --> 编写方法伪代码 --> 编写测试码 --> 实现类 --> 测试和改错

    编写一个类之前可以先写其测试类TestDriver ,然后依据测试类写出具体类

    这个游戏有 SimpleDotCom ,SimpleDotComGame ,GameHelper 三个类,将三个类编译出来的.class文件放在同一目录下就能通过SimpleDotCom 的main( )方法来调用其他class来进行游戏

    SimpleDotComTestDriver.java:

    class SimpleDotCom{
        
        int[] locationCells;
        int numOfHits = 0;
        
        public void setLocationCells( int[] locs){
            locationCells = locs;
        }
        
        public String checkYourself( String stringGuess ){
            int guess = Integer.parseInt(stringGuess);
            String result = "miss";
            
            for(int cell : locationCells){
                if( guess == cell ){
                    result = "hit";
                    numOfHits++;
                    break;
                }
            }
            
            if( numOfHits == locationCells.length ){
                result = "kill";
            }
            System.out.println(result);
            return result;
        }
        
    }
    // SimpleDotCom 的测试码
    public class SimpleDotComTestDriver{
        public static void main( String[] args ){
            SimpleDotCom dot = new SimpleDotCom();
            
            int [] locations = {2,3,4};
            dot.setLocationCells(locations);
            
            String userGuess = "2";
            String result = dot.checkYourself(userGuess);
            String testResult = "failed";
            if( result.equals("hit") ){
                testResult = "passed";
            }
            System.out.println( testResult );
        }    
    }

    SimpleDotCom.java:

    public class SimpleDotCom{
        
        //实例变量 
        int[] locationCells;
        int numOfHits = 0;
        
        //setter方法
        public void setLocationCells( int[] locs){
            locationCells = locs;
        }
        
        //击中结果判定方法
        public String checkYourself( String stringGuess ){
            int guess = Integer.parseInt(stringGuess);
            String result = "miss";
            
            for(int cell : locationCells){
                if( guess == cell ){
                    result = "hit";
                    numOfHits++;
                    break;
                }
            }
            
            if( numOfHits == locationCells.length ){
                result = "kill";
            }
            System.out.println(result);
            return result;
        }
        
        //main
        public static void main( String[] args ){}
    }

    GameHelper.java:

    import java.io.*;
    public class GameHelper{
        public String getUserInput( String prompt ){
            String inputLine = null;
            System.out.print( prompt+" " );
            try{
                BufferedReader is = new BufferedReader( new InputStreamReader(System.in) );
                inputLine = is.readLine();
                if(inputLine.length() == 0 ) return null;
            } catch (IOException e){
                System.out.println("IOException: "+e);
            }
            return inputLine;
        }
        
         public static void main(String[] args){}
    }

    SimpleDotComGame.java:

    public class SimpleDotComGame{
        
        public static void main( String[] args){
            int numOfGuess = 0;
            //进行游戏交互提示
            GameHelper helper = new GameHelper();
            
            //创建DotCom对象
            SimpleDotCom theDotCom = new SimpleDotCom();
            int randomNum = (int)(Math.random()*5);
            //初始化DotCom对象
            int[] locations = {randomNum ,randomNum+1 ,randomNum+2};
            theDotCom.setLocationCells(locations);
            boolean isAlive = true;
            //游戏判断过程
            while( isAlive==true ){
                String guess = helper.getUserInput("enter a number");
                String result = theDotCom.checkYourself(guess);
                numOfGuess++;
                if( result.equals("kill")){
                    isAlive = false;
                    System.out.println("You took "+numOfGuess+" guesses");
                }
            }
        }
    }
  • 相关阅读:
    Arduino-LCD1602液晶显示器
    photoshop--选区变形
    利用github给国外文件下载加速
    开发老人笔记:Git 常用命令清单
    区块链轻节点:“身”轻,责任重
    需求条目化:一个让用户故事有效落地的套路
    十八般武艺玩转GaussDB(DWS)性能调优:Plan hint运用
    跨越全场景统一架构三大挑战,MindSpore亮出“四招”
    看图学NumPy:掌握n维数组基础知识点,看这一篇就够了
    进来抄作业:分布式系统中保证高可用性的常用经验
  • 原文地址:https://www.cnblogs.com/-ifrush/p/12197509.html
Copyright © 2011-2022 走看看