Sink a DotCom

DotComBust

可能存在的错误是:数组的边界问题

游戏目标:以最少的猜测次数打掉计算机所安排的达康公司(Dot Com)网站。计算机会根据你的表现进行评分。
初始设置:程序启动后,计算机会在7×7的方格上安排3个达康网站,游戏会要求你开始猜坐标。
进行游戏:计算机会提示你输入猜测的位置。例如"A3",“D5"等,计算机会回给你命中"Hit”、没中"Miss"、击沉"Sunk"等回应。当你清空所有的达康时,游戏会列出你的分数并结束

package pers.cc.Sink_a_DotCom;
import java.lang.reflect.Array;
import java.util.*;
public class DotComBust {
    //声明并初始化变量
    private GameHelper helper = new GameHelper();
    private ArrayList<DotCom> dotComsList = new ArrayList<DotCom>();
    private int numOfGuesses = 0;

    private void setUpGame() {
        //实例化3个DotCom并附上地址
        DotCom one = new DotCom();
        one.setName("Pets.com");
        DotCom two = new DotCom();
        two.setName("eToys.com");
        DotCom three = new DotCom();
        three.setName("Go2.com");
        //置入dotComsList
        dotComsList.add(one);
        dotComsList.add(two);
        dotComsList.add(three);

        //提示
        System.out.println("Your goal is to sink three dot coms.");
        System.out.println("Pets.com  eToys.com  Go2.com");
        System.out.println("Try to sink them all in the fewest number of guesses");

        for (DotCom dotComToset : dotComsList) {//对list中所有的DotCom重复
            ArrayList<String> newLocation = helper.placeDotCom(3);;//要求DotCom的位置

            dotComToset.setLocationCells(newLocation);//调用这个DotCom的setter方法来指派取得的位置
        }
    }

    private void startPlaying() {
        while (!dotComsList.isEmpty()) {//判断是否为空
            String userGuess = helper.getUserInput("Enter a guess");//取得玩家的输入
            checkUserGuess(userGuess);//调用checkUserGuess方法
        }
        finishGame();
    }

    private void checkUserGuess(String userGuess){
        numOfGuesses++;//递增玩家猜测次数的计数
        String result="miss";//先假设没有命中
        for(DotCom dotComToTest:dotComsList){//对List中所有的DotCom重复
            result=dotComToTest.checkYourself(userGuess);//要求DotCom检查是否命中或击沉
            if (result.equals("hit")){
                break;
            }
            if(result.equals("kit")){
                dotComsList.remove(dotComToTest);
                break;
            }
        }
        System.out.println(result);
    }
    private void finishGame(){//列出玩家成绩
        System.out.println("All Dot Coms are dead ! Your stock is now worthless.");
        if(numOfGuesses<=18){
            System.out.println("It only took you"+numOfGuesses+"guesses.");
            System.out.println("You got out before your options sank.");
        }
        else{
            System.out.println("Took you long enough"+numOfGuesses+"guesses");
            System.out.println("Fish are dancing with your options");
        }
    }
    public static void main(String[] args){
        DotComBust game=new DotComBust();
        game.setUpGame();
        game.startPlaying();
    }
}



DotCom

package pers.cc.Sink_a_DotCom;
import java.util.*;
public class DotCom {
    private ArrayList<String> locationCells;//保存位置的ArrayList
    private String name;//DotCom的名字

    public void setLocationCells(ArrayList<String> loc){//更新DotCom位置的setter方法
        locationCells=loc;
    }
    public void setName(String n){//基本的setter方法
        name=n;
    }

    public String checkYourself(String userInput){//判断是否猜中并返回相关信息
        String result="miss";
        int index=locationCells.indexOf(userInput);//使用到了indexOf()方法,如果玩家猜中的话,返回相应的值。否则返回-1
        if(index>=0){
            locationCells.remove(index);//删除被猜中的元素

            if(locationCells.isEmpty()){//判断是否还有元素剩余(判断是否被击沉)
                result="kill";
                System.out.println("You sunk"+name+" ");
            }
            else {
                result="hit";
            }
        }
        return result;//返回状态
    }
}

GameHelper

package pers.cc.Sink_a_DotCom;
import java.io.*;
import java.util.*;
public class GameHelper {
    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("IOException: "+e);
        }
        return inputLine.toLowerCase(Locale.ROOT);
    }
    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;//目前起点

        comSize++;//现在处理到第n个点
        int incr=1;//水平增量
        if((comCount%2)==1){//如果是奇数号的话,垂直增量
            incr=gridLength;
        }
        while(!success & attempts++<200){//主要搜素循环
            location=(int)(Math.random()*gridSize);//随机起点
            //System.out.print("try "+location);
            int x=0;//第n个位置
            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{//找到已使用位置
                    //System.out.println("used "+location);
                    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++;
                   // System.out.println("coord"+x+"="+alphaCells.get(x-1));输出DotCom的确切位置

        }
return alphaCells;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值