Head First java——战舰游戏代码

在这里插入图片描述

代码示例:
主类

package dot.com;

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("Pets.com");
		DotCom two=new DotCom();
		two.setName("eToys.com");
		DotCom three=new DotCom();
		three.setName("Go2.com");
		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中所有的DitCom重复
			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);
		}
		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("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();
	}
}

代码:GameHelper类

package dot.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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.print(prompt+" ");
		try {
			BufferedReader is=new BufferedReader(new InputStreamReader(System.in));
			//java.io里面的类:BufferedReader(readLine是其里面的方法)和InputStreamReader
			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>();
		@SuppressWarnings("unused")
		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;
				}
			}
		}//while结束
		
		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;
	}
}

代码:DotCom类

package dot.com;

import java.util.ArrayList;

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("Ouch! You sunk"+name+" :(");
			}else {
				result="hit";
			}
		}
		return result;
	}
}

运行结果:

Your goal is to sink three dot coms.
Pets.com,eToys.com,Go2.com
Try to sink them all in the fewest number of guesses
Enter a guess a3
miss
//略掉。。。。。
Enter a guess c7
miss
Enter a guess d6
hit
Enter a guess e6
Ouch! You sunkeToys.com :(
kill
Enter a guess d1
miss
Enter a guess d2
hit
Enter a guess d3
hit
Enter a guess d4
Ouch! You sunkPets.com :(
kill
Enter a guess d5
miss
//略掉。。。。。
Enter a guess g1
miss
Enter a guess g2
hit
Enter a guess g3
hit
Enter a guess g5
miss
Enter a guess g4
Ouch! You sunkGo2.com :(
kill
All Dot Coms are dead! Your stock is now worthless.
Took you long enough.52guesses.
Fish are dancing with your options.

猜你喜欢

转载自blog.csdn.net/weixin_41550144/article/details/89035951
今日推荐