• 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();
        }

    }



  • 相关阅读:
    Docker系列【使用docker搭建fastdfs并使用SpringBoot实现文件上传下载】
    Docker系列【使用docker搭建文件单机存储服务器minio】
    Docker系列【查看Latest的镜像具体版本和查看容器用到的镜像版本】
    Docker系列【DockerDesktop的安装和使用】
    SpringBoot系列【如何自定义Starter】
    Redis系列【分布式锁解决方案之Redisson】
    c++实训课
    链队列的实现(上课用)
    循环队列(上课用)
    AE基础01 视频添加水印
  • 原文地址:https://www.cnblogs.com/xinxianquan/p/8496083.html
Copyright © 2020-2023  润新知