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");
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/-ifrush/p/12197509.html