java零基础入门第六天 ArrayList 导包 java运算符

先附上最终代码 

import java.io.*;
import java.util.ArrayList;
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(){
		DotCom one = new DotCom();
		one.setName("章鱼哥");
		DotCom two = new DotCom();
		two.setName("史努比");
		DotCom three = new DotCom();
		three.setName("唐老鸭");
		dotComsList.add(one);
		dotComsList.add(two);
		dotComsList.add(three);

		System.out.println("对象已经建好 名字是 唐老鸭 史努比 章鱼哥");

		for(DotCom dotComToSet : dotComsList){
			ArrayList<String>newLocation = helper.placeDotCom(3);
			dotComToSet.setLocationCells(newLocation);
		}
	}

	private void startPlaying(){
		while(!dotComsList.isEmpty()){
			String userGuess = helper.getUserInput("输入你的猜测 类似 A1 D5");
			checkUserGuess(userGuess);
		}
		finishGame();

	}

	private void checkUserGuess(String userGuess){
		numOfGuesses++;
		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(){
		System.out.println("所有战舰都被击杀");
		if( numOfGuesses <=18){
			System.out.println("你只使用了"+ numOfGuesses +"次数猜中");
		}else{
			System.out.println("你使用了"+ numOfGuesses +"次数猜中");
		}
	}

	public static void main (String[] args){
		DotComBust game = new DotComBust();
		game.setUpGame();
		game.startPlaying();
	}

}
class DotCom{

	private ArrayList<String>locationCells;
	private String name;
	//int[] locationCells;
	//int numOfHits = 0;

	public void setLocationCells(ArrayList<String> loc){
		locationCells = loc;
	}

	public void setName(String n){
		name = n;
	}

	public String checkYourself(String userInput){
		//int guess = Integer.parseInt(stringGuess);
		String result = "miss";
		int index = locationCells.indexOf(userInput);

		if(index >= 0){
			locationCells.remove(index);
			if(locationCells.isEmpty()){
				result = "kill";
				System.out.println("干的漂亮 你击败了" + name + "战舰");
			}else{
				result = "hit";
			}//close if
		}// close if
		System.out.println("你输入数字猜中的结果是 : "+result);
		return result;
	}//close method
}//close class

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 promt){
		String inputLine = null;
		System.out.println(promt + " ");

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

	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;
     System.out.println("row: "+ row +"column : " +column);
    temp = String.valueOf(alphabet.charAt(column));

    alphaCells.add(temp.concat(Integer.toString(row)));
    x++;
     System.out.println("temp: "+ temp +"  alphaCells : " +alphaCells);
    System.out.println("每个战舰具体位置是 "+ x +"= " +alphaCells.get(x-1));
  }
  return alphaCells;
}

}

第五天的简单simpleDot有一个bug

书中提到了三种解决方案  其实都是说了下数组的弊端,最终解决方案采用了ArrayList,之前我们提到过封装,java这么多年过来,已经有人封装了很多好用的库,我们不需要重新去写,直接在巨人肩膀上继续开发就好,那怎么引用别人封装好的包呢?

在文件最上方引入对应的包就可以了 这个书中139页也有介绍

import java.util.ArrayList;

书中说的很多,ArrayList与数组的区别,跟封装好的方法,我觉得如果你还不懂,或者想了解更多ArrayList的方法或特性 可以用初级工程师必备技能,万事用百度。

在140页的时候 重新提了下需求,要做个7x7的格子,和3间达康公司,每个达康公司占了3个格子

从书中141页到145页都是说跟simpleDot区别有哪些应该改哪些伪代码,讲了很多设计思路,在146-150中将方法实现出来。

在151页的时候提到了一个运算符知识点,&& || !  这三个比较常用,一定要了解,

154页开始介绍导包,引用库。

剩下的就课后作业花点时间坐会,还有就是把我那个最终代码运行起来 运行结果如下图

可能代码有点复杂,但是我觉得可读性还是有的,你们看下能看懂多少,哪些变量对应是什么,不懂得可以把变量值打印到控制台出来,如果实在看不懂页没关系,能读多少是多少,不要气馁。

总结下知识点 ArrayList 导包 java运算符

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值