java ArrayList

package com.Test;
import java.io.*;
import java.util.*;
public class GameHelper {
    private static final String alphabet="abcdef";
    private int gridLength=2;
    private int gridSize=34;
    private int [] grid=new int[gridSize];
    private int comCount=0;
    public String getUserInput(String prompt){
        String inputLine=null;
        System.out.println(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(e);
        }
        return inputLine.toLowerCase();
    }    
    public ArrayList<String> placeDotCom(int comSize){
        ArrayList<String> alphaCells=new ArrayList<String>();
        String[] alphacoords=new String[comSize];
        String temp=null;
        int[] coords=new int[comSize];
        int attempts=0;
        boolean success=false;
        int location=0;
        comCount++;
        int incr=1;
        if((comCount%=2)==1){
            incr=gridLength;
        }while(!success & attempts++<200){
            location=(int)(Math.random()*gridSize);
            int x=0;
            success=true;
            while(success && x<comSize){
                if(grid[location]==0){
                    coords[x++]=location;
                    location+=incr;
                    if(location>=gridSize){
                        success=false;
                    }
                }
            }
        }
        return alphaCells;
    }
    }
//*______________________________________
package com.Test;
import java.util.*;
public class DotCom {
    private ArrayList<String> locationCells;
    private String name;
    public void setLocationCells(ArrayList<String> loc){
        locationCells=loc;
    }
    public void setName(String n){
        name=n;
    }
    public String checkYourself(String userInput){
        String result="miss";
        int index=locationCells.indexOf(userInput);
        if(index>=0){
            locationCells.remove(index);
            if(locationCells.isEmpty()){
                result="kill";
                System.out.println("outch you have sunk");
            }else{
                result="hit";
            }
            
        }
        return result;
    }

}
//*------------------------------------------------------------------------------------
package com.Test;
import java.util.*;

public class DotComBust {
    private GameHelper helper=new GameHelper();
    private ArrayList<DotCom> dotComsList=new ArrayList<DotCom>();
    private int mumOfGuesses=0;
    private void setUpGame(){
        DotCom one=new DotCom();
        one.setName("pets.com");
        DotCom two=new DotCom();
        two.setName("eTos.com");
        DotCom three=new DotCom();
        three.setName("Go.com");
        System.out.println("You goal is to sink three dot com");
        System.out.println("Pets.com,eTos.com,Go.com");
        System.out.println("Try to sink all in the fewest number of guesses");
        for(DotCom dotComToSet:dotComsList){
            ArrayList<String> newLocation=helper.placeDotCom(3);
            dotComToSet.setLocationCells(newLocation);
        }
        
    }



    private void startPlaying(){
        while(!dotComsList.isEmpty()){
            String userGuess=helper.getUserInput("Enter a guess");
            
        }
        finishGame();
    }
    private void checkUserGuess(String userGuess){
        mumOfGuesses++;
        String result="miss";
        for(DotCom dotComToTest :dotComsList){
            result=dotComToTest.checkYourself(userGuess);
            if(result.equals("hit")){
                break;
            }
            if(result.equals("kill")){
                dotComsList.remove(dotComToTest);
                break;
            }
        }
        System.out.println(result);
    }
    private void finishGame(){
        
    }
    public static void main(String[] args){
        DotComBust game=new DotComBust();
        game.setUpGame();
        game.startPlaying();
    }

}



原文地址:https://www.cnblogs.com/xinxianquan/p/8496083.html