海战小游戏

游戏中两个对手,分别是玩家和计算机。
两块10*10的方格上放置自己的舰艇。
每人5艘舰艇:一艘驱逐舰2格,一艘潜艇3格,一艘巡洋舰3格,一艘战列舰4格,一艘航空母舰5格。
舰艇可以竖着放或横着放,任意两艘舰艇不能互相接触。
双方轮流轰炸对方的舰艇。如果击中,可以继续攻击,直到击不中为止。
游戏的目标是赶在对手之前把他所有的舰艇击沉。要击沉一艘舰艇,该舰艇的所有格子都必须被命中。

做这个程序,熟悉了一下子类父类。
Node是方格

package dd;

public class Node {

	private int i;//保存为1说明已经放了元素
	private Ship s;//当前这个Node属于哪个船
	
	public Node()
	{
		i=0;
	}

	public int getI() {
		return i;
	}
	
	public Ship getShip()
	{
		return s;
	}
	
	
	public void setI() {
		this.i=1;
	}
	
	public void setShip(Ship s)
	{
		this.s=s;
	}
	
	public boolean attacked() {
		//如果被攻击,值变为0,所在的船生命-1
		if(i==1) 
		{
		i=0;
		this.s.live--;
		return true;
		}
		
		return false;
	}
	
	
	
}

Filed是场地,存储10*10的方格

package dd;

public class Field {
	private   Node n[][]=new Node[10][10];
	private int Border_length=10;
	private  int  ship=0;
	public Field()
	{
		for(int i=0;i<n.length;i++)
		for(int j=0;j<n[i].length;j++)
		n[i][j]=new Node();
	}
	

	
	
	public  Node  get(int i,int j)
	{
		return  n[i][j];
	}
	
	public  int  GetBorder_length()
	{
		return  Border_length;
	}
	
	
	public  void addship()
	{
		ship++;
	}
	
	public void  reduceship()
	{
		ship--;
	}
	
	public  int getship()
	{
		return ship;
	}
	
}

Ship代指舰艇类

package dd;

public class Ship {
	static int number;//该船占的格子数
	int live;//现在存活的数量
	int i;//船头的行
	int j;//船头的列
	char direction;//船头的方向
	
	
	
	public Ship(Field f,char s,int i,int j,int number)
	{this.i=i;
	this.j=j;
	live=number;//存活数为live
	f.addship();
	
		if(s=='H'||s=='h')
		{
			this.direction='h';
			
			for(int k=0;k<number;k++)
			{
				f.get(i,j+k).setI();
			    f.get(i,j+k).setShip(this);
			}
		}
				
		
			
		else //在没有异常时等价于if(s=='V'||s=='v')
		{
			this.direction='v';
			for(int k=0;k<number;k++)
			{   
			   f.get(i+k,j).setI();
			   f.get(i+k,j).setShip(this);
			}
		}
				
		
		
		
		
	}
	
	
	
	
	
	public boolean living()
	{
	if(live==0)   return false;
	return true;
	}

	public static int getnumber()
	{
		return number;
	}
	
}

下面的五种舰艇都继承自Ship

package dd;

public class Aircraft_carrier extends Ship{//航空母舰  5
	static int number=5;
	
	public Aircraft_carrier(Field f, char s, int i, int j) {
		
		super(f, s, i, j, 5);
		// TODO Auto-generated constructor stub
	}

	

}

package dd;

public class Battleship extends Ship{//战列舰  4

static int number=4;
	
public Battleship(Field f, char s, int i, int j) {
	super(f, s, i, j,4);
		// TODO Auto-generated constructor stub
	}

	
	
	
}

package dd;

public class Destroyer extends Ship {//驱逐舰 2
//放的时候船头总是保持在左或上
//horizon是水平(横着)     vertical是竖着
//用H或h            用V或v
static 	int number=2;
	
	public Destroyer(Field f, char s, int i, int j) {
		super(f, s, i, j,2);
		// TODO Auto-generated constructor stub
	}

	
}

package dd;

public class Cruiser extends Ship{//巡洋舰  3

static int number=3;
	
public Cruiser(Field f, char s, int i, int j) {
		super(f, s, i, j,3);
		// TODO Auto-generated constructor stub
	}

}

package dd;

public class Submarine extends Ship{//潜艇  3

static int number=3;
	
	public Submarine(Field f, char s, int i, int j) {
		super(f, s, i, j,3);
		// TODO Auto-generated constructor stub
	}


}

主程序

package dd;

import java.util.Scanner;

public class Main {
//放的时候船头总是保持在左或上
//horizon是水平(横着)     vertical是竖着
//用H或h            用V或v
//假定输入的都是正确的格式
	public static boolean set(Field f,char s,int i,int j,int number)
	{//判断是否可以放
		
	if(s=='H'||s=='h')
	{for(int k=0;k<number;k++)//判断是否已经放了
		if(f.get(i,j+k).getI()==1)  return false;
	
	return true;
	}
		
		
		
		
		
	else if(s=='V'||s=='v')
	{for(int k=0;k<number;k++)//判断是否已经放了
		if(f.get(i+k,j).getI()==1)  return false;
	
	return true;
	}
	
	
	
	else  return false;
	
	}

	
	
	
	
	
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		Field  f1=new Field();
//像下面的if一样,如果能放置,才去放置
//后面四个也要加上,为省劲不再写了
/*		System.out.println("请输入放置方式、"
				+ "船头所在行i、船头所在列j。"
				+ "用空格分隔。");
		String s=in.next();//输入一行
		char a=s.charAt(0);//取第一个字符
		int i=in.nextInt();
		int j=in.nextInt();
		
		while(!set(f1,'h',0,0,Aircraft_carrier.getnumber()))
		{
		System.out.println("会重叠,请重新输入");
		System.out.println("请输入放置方式、"
				+ "船头所在行i、船头所在列j。"
				+ "用空格分隔。");
		 s=in.next();//输入一行
		 a=s.charAt(0);//取第一个字符
		 i=in.nextInt();
		 j=in.nextInt();
		}
  new Aircraft_carrier(f1,a,i,j);*/
		new Aircraft_carrier(f1,'h',0,0);		
		new Battleship(f1,'h',1,0);
		new Cruiser(f1,'h',2,0);
		new Destroyer(f1,'h',3,0);
		new Submarine(f1,'h',4,0);
		
//实际游戏中,不需要输出当前放置情况		
		System.out.println("玩家1矩阵:");
		for(int i=0;i<f1.GetBorder_length();i++)
		{for(int j=0;j<f1.GetBorder_length();j++)
		{int aa=f1.get(i, j).getI();
		System.out.print(aa);}
		System.out.println();}
		System.out.println();
///////////////////////////////////		
		Field  f2=new Field();
		new Aircraft_carrier(f2,'v',0,0);		
		new Battleship(f2,'v',0,1);
		new Cruiser(f2,'v',0,2);
		new Destroyer(f2,'v',0,3);
		new Submarine(f2,'v',0,4);
		
		
		System.out.println("玩家2矩阵:");
		for(int i=0;i<f2.GetBorder_length();i++)
		{for(int j=0;j<f2.GetBorder_length();j++)
		{int aa=f2.get(i, j).getI();
		System.out.print(aa);}
		System.out.println();}
		System.out.println();
		
		
//首先	1攻2         然后     2攻1	
//我们让2为电脑,随机的
//所以2的每一步都要停一下,看一看
		
		int row;
		int col;
		boolean bb;
		
		
		while(true)
		{	
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();}
			
			bb=true;
			System.out.println("现在是玩家1在进攻");
			
			while(bb&&f2.getship()>0)
			{
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();}
			System.out.println("输入行和列(用空格分隔)");
			row=in.nextInt();
			col=in.nextInt();
			bb=f2.get(row, col).attacked();
		       //打中了	  
			if(bb)   
			 {
			 
				
				System.out.println("打中了");
	              /*判断船是否活着*/	   
			 if(!f2.get(row, col).getShip().living())
			   {f2.reduceship();
				 }   
			 }
			}
			
			//下面的try catch 是处理Thread.sleep
			//Thread.sleep是程序中断  毫秒数
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {
		e.printStackTrace();}
		
		if(bb==false)
			{System.out.println("未打中");
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();}
			System.out.println("玩家2还剩余"+
			f2.getship()+"只船");
			System.out.println();
			}
		else 
		{System.out.println("玩家1打沉了所有船!");
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();}
		System.out.println("玩家1赢了!");
		break;}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();}
		
		
		
		
		
		bb=true;
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();}
		System.out.println("现在是玩家2在进攻");
		while(bb&&f1.getship()>0)
		{try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();}
		//System.out.println("输入行和列(用空格分隔)");
		//下面由输入改成随机生成
		row=(int)(Math.random()*10);
		col=(int)(Math.random()*10);
		System.out.println("玩家二选择行和列为"+row+" "+col);
		//row=in.nextInt();
		//col=in.nextInt();
		bb=f1.get(row, col).attacked();
	       //打中了	  
		if(bb)   
		 {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();}
			System.out.println("打中了");
			
	          /*判断船是否活着*/	   
		 if(!f1.get(row, col).getShip().living())
		   {f1.reduceship();
			 System.out.println("船被击毁");}   
		 }
		}
		//下面的try catch 是处理Thread.sleep
				//Thread.sleep是程序中断  毫秒数
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();}
		
		
	if(bb==false)
		{System.out.println("未打中");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();}
		System.out.println("玩家1还剩余"+
		f1.getship()+"只船");
		System.out.println();
		}
	else 
	{System.out.println("玩家2打沉了所有船!");
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {
		e.printStackTrace();}
	System.out.println("玩家2赢了!");
	break;}




		}
			
			
			
			
			
			
			in.close();
		}

	}

猜你喜欢

转载自blog.csdn.net/weixin_41677899/article/details/84348209