Sink a DotCom

DotCom 类:
用来保存DotCom的名字
是否命中的判定方法

import java.util.ArrayList;

public class DotCom {
    /*
     * 动态数组建立 用来保存DotCom的名字
     */
    private ArrayList<String> locationCells;

    public void setlocationCells(ArrayList<String> location){

        locationCells = location;

        }//close setter
    public String checkYourself(String userInput){
        /*
        设置一个反馈结果的变量 结果有三种 未命中 命中 已击杀
        */
        String result = "未击中";

            /*
             * IndexOf 方法  返回 String 对象内第一次出现子字符串的字符位置。
             * 如果用户输入值在本地字母中 则返回该字母所在位置 并删除
             * 如果数组为空 即已删除掉所有位置 则表明已击杀 否则表明命中
             */
            int index = locationCells.indexOf(userInput);

            if (index >= 0){
                locationCells.remove(index);
                if(locationCells.isEmpty()){

                    result  = "已击杀";

                }else{
                    result = "命中";
                }//close if
            }//close if
    return result;

    }//close method

}//close class

Game类:
创建3个DotCom并命名
取得玩家输入 进行游戏

import java.util.ArrayList;

public class Game {
    /*
     * 声明变量
     */

    private Helper helper = new Helper();

    private ArrayList<DotCom> dotComList = new ArrayList<DotCom>();//设置一个动态数组 用来存放三个Com

    private int num = 0;//玩家猜测次数

    /*
     * setUpGame()
     * 创建DotCom变量并初始化
     * 相关信息展示给玩家
     */
    public void setUpGame(){

        //创建三个DotCom对象
        DotCom c1 = new DotCom();
        DotCom c2 = new DotCom();
        DotCom c3 = new DotCom();
        //指派DotCom名称
        c1.setName("Pets。com");
        c2.setName("eToys.com");
        c3.setName("Go2.com");
        //置入动态数组
        dotComList.add(c1);
        dotComList.add(c2);
        dotComList.add(c3);

        System.out.println("你的目标是击沉以下三个dot com");
        System.out.println(c1.getName()+c2.getName()+c3.getName());
        System.out.println("尝试用最少的次数击沉所有的dot com");

        /*
         * 对list中所有的com重复操作
         */
        for(DotCom dotComToSet:dotComList){

            ArrayList<String> newLocation = helper.placeDotCom(3);//要求dotcom的位置
            dotComToSet.setlocationCells(newLocation);


        }



    }


    /*
     * startPlay()
     * 请求用户输入
     */
    private void startPlay(){
        while(!dotComList.isEmpty()){
            String userGuess = helper.getUserInput("请输入你的猜测");
                    checkUserGuess(userGuess);
        }
        finishGame();
    }


    /*
     * checkUserGame()
     * 检查用户猜测值
     */
    private void checkUserGuess(String userGuess){

        num++;

        String result = "未击中";
        for(DotCom dotComToTest:dotComList){
            result = dotComToTest.checkYourself(userGuess);
            if(result.equals("命中")){
                break;
            }
            if(result.equals("已击杀")){
                dotComList.remove(dotComToTest);
                break;
            }

        }
        System.out.println(result);


    }

    /*
     * finishGame()
     * 结束
     */

    private void finishGame(){

        System.out.println("击杀成功");

    }

    public static void main(String[] args){

        Game g = new Game();
        g.setUpGame();
        g.startPlay();

    }
}
import java.io.*;
import java.util.*;
public class Helper {

    private static final String alphabet = "abcdefg";
    private int gridLength = 7;
    private int gridSize = 49;
    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("IOExcepiton"+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;
                    }
                    if(x>0&&(location%gridLength==0)){
                        success = false;
                    }
                }else{
                    success = false;
                }
            }
        }

        int x=0;
        int row = 0;
        int column = 0;

        while(x<comSize){
            grid[coords[x]]=1;
            row = (int)(coords[x]/gridLength);
            column = coords[x]%gridLength;
            temp = String.valueOf(alphabet.charAt(column));

            alphaCells.add(temp.concat(Integer.toString(row)));
            x++;

        }
        return alphaCells;

    }


}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值