最近在学习java,力求能够独立编写好《Head First》中的每个小项目,关于击沉战舰小游戏算是《Head First》里比较典型的应用案例吧,这里我没有用内本书里提供的Helper类,类似Helper类的方法是自己写的,纯粹是自己的想法和设计,但最后的执行效果非常棒,甚至要比用helper的效果更好,我觉得既然是学习,能够把一些东西变成自己的(自己独立写出功能)肯定最好啦~~
话不多说,上CODE,该上的注释都上了,共同学习哦:)
package ArraylistApplyForSinkBoatGame;
import java.util.ArrayList;
public class Boat {
ArrayList boatLocation=new ArrayList();
String boatName;
public void setBoatName(String name){
boatName=name;
}
public String getBoatName(){
return boatName;
}
public ArrayList getBoatString(){
return boatLocation;
}
public void addLocation(String loc){
boatLocation.add(loc);
}
void removeLocation(String loc,int index){
boatLocation.remove(index);
}
boolean arrylislIsEmpty(){
if(boatLocation.isEmpty())
return true;
return false;
}
}
package ArraylistApplyForSinkBoatGame;
public class Game {
String[][] sea2d={
{"A1","A2","A3","A4","A5","A6","A7","A8","A9","A10"},
{"B1","B2","B3","B4","B5","B6","B7","B8","B9","B10"},
{"C1","C2","C3","C4","C5","C6","C7","C8","C9","C10"},
{"D1","D2","D3","D4","D5","D6","D7","D8","D9","D10"},
{"E1","E2","E3","E4","E5","E6","E7","E8","E9","E10"},
{"F1","F2","F3","F4","F5","F6","F7","F8","F9","F10"},
{"G1","G2","G3","G4","G5","G6","G7","G8","G9","G10"},
{"H1","H2","H3","H4","H5","H6","H7","H8","H9","H10"},
{"I1","I2","I3","I4","I5","I6","I7","I8","I9","I10"},
{"J1","J2","J3","J4","J5","J6","J7","J8","J9","J10"},
};
public String xyIntoString(int x,int y){
return sea2d[x][y];
}
}
package ArraylistApplyForSinkBoatGame;
import java.util.ArrayList;
import java.util.Scanner;
public class StartGame {
public static void main(String[] args){
int i=0,rad,randomX,randomY,tempX=0,tempY=0,tryTimes=0;
String str=null;
boolean ber=false,berHit;
ArrayList boats=new ArrayList();//存储boat对象的Arraylist
Scanner scan=new Scanner(System.in);//输入对象
Game myGame=new Game();
//分别创建三个船对象,名字分别为boatone、boattwo、boatthree
Boat boat1=new Boat();
boat1.setBoatName("boatone");
boats.add(boat1);
Boat boat2=new Boat();
boat2.setBoatName("boattwo");
boats.add(boat2);
Boat boat3=new Boat();
boat3.setBoatName("boatthree");
boats.add(boat3);
Object o=new Object();//用于拿出来Arraylist中的某个对象
//产生船所在的随机位置
for(Boat bo:boats){
randomX=(int)(Math.random()*10);
randomY=(int)(Math.random()*10);
tempX=randomX+3;
tempY=randomY+3;
while(tempX>10||tempY>10){//用循环筛去计算机随机产生的船首位置+3后(船长度为3,加三后为船尾)溢出数组的随机数,这里数组最大值为10
randomX=(int)(Math.random()*10);
randomY=(int)(Math.random()*10);
tempX=randomX+3;
tempY=randomY+3;
}
rad=(int)Math.random()*10;//随机一个数,以此设置船占三个位置的方向(上下或左右)
if(rad%2==0){
bo.addLocation(myGame.xyIntoString(randomX, randomY));//产生的随机数即为二维数组中的行列号,按照该行列号将Game类中字符数组对应位置元素添加至Boat类中String类型的Arrayli中
bo.addLocation(myGame.xyIntoString(randomX+1, randomY));
bo.addLocation(myGame.xyIntoString(randomX+2, randomY));
if(!bo.arrylislIsEmpty()){//打印随机产生的船位置,下同
System.out.println(bo.boatName+"in sight");
for(i=0;i<3;i++){
o=bo.boatLocation.get(i);
System.out.println(o);
}
}
}
else{
bo.addLocation(myGame.xyIntoString(randomX, randomY));
bo.addLocation(myGame.xyIntoString(randomX, randomY+1));
bo.addLocation(myGame.xyIntoString(randomX, randomY+2));
if(!bo.arrylislIsEmpty()){//同上:)
System.out.println(bo.boatName+"in sight");
for(i=0;i<3;i++){
o=bo.boatLocation.get(i);
System.out.println(o);
}
}
}
}//end
System.out.println("\n :) Ready for the Game,Let's do it!!\n");//准备开始游戏
//依据输入击沉船
while(!boats.isEmpty()){
berHit=true;//用于标记击中与否
System.out.println("input your attack:)\n");
tryTimes++;//记录尝试次数
str=scan.nextLine();//用户输入打击目标
for(Boat bot:boats){//遍历存储Boat对象的ArrayList
for(String st:bot.getBoatString()){ //遍历存储Boat对象的ArrayList中的,每个存储String类型对象的Arraylist
if(st.equals(str)){
System.out.println("You have hit "+bot.boatName+"!!");
bot.getBoatString().remove(st); //击中后将boat中的击中位置删去
if(bot.getBoatString().isEmpty()){
System.out.println("OH GOD!You have sunk "+bot.boatName+"!!\nLet's sink next boat!\n");
boats.remove(bot);//从存储Boat对象的Arraylist中,删去某个被三个位置都击中的船对象
}
ber=true;
berHit=false;
break;
}
}
if(ber){//若击中一只船,直接跳出两层循环
ber=false;
break;
}
}
if(berHit){//未击中提示
System.out.println("Good hit,but..you missed (T^T)\nCome on! try again! :) ");
}
}
//游戏结束
System.out.println("After "+tryTimes+" times hits\nI can't believe it...\n OMG!!YOU HAVE WON ↖(^ω^)↗!!!!");
scan.close();
}//main()
}