Battleship in java

Started by
17 comments, last by rip-off 9 years, 8 months ago

Hi I made a battleship game in java and there are a couple of places that I would like to reduce the code as much as I can. Because in battleship you are entering in a character and and number for a coordinate system I am having to assign a 1 to a two denominational array and do if loops to determine what part of the array I want to assign to 1: if c == a then a[rowinput][1]. I have made an class Ship and made two different object arrays for the ships of both players. I don't want to run through these big if loops each time I want to initialize my arrays I want to do them both at the same time. If anyone has any ideas on how I could shorten this code please let me know.



import java.util.*;

public class a4 
{
  public static void main( String args[] ) 
  {
	Ship[] s1 = new Ship[5];
	Ship[] s2 = new Ship[5];

	Scanner in = new Scanner(System.in);
	

	s1[0] = new Ship(1,"Aircraft carrier",5,1,'a',1,'e',0);  // player, ship, size, row front, column front, row back, column back, hit
	s1[1] = new Ship(1,"Battleship",      4,2,'b',5,'b',0);
 	s1[2] = new Ship(1,"Submarine",       3,2,'c',4,'c',0);
	s1[3] = new Ship(1,"Destroyer",       3,2,'d',4,'d',0);
	s1[4] = new Ship(1,"Patrol boat",     2,2,'e',3,'e',0);

	s2[0] = new Ship(1,"Aircraft carrier",5,1,'a',1,'e',0);  // player, ship, size, row front, column front, row back, column back, hit
	s2[1] = new Ship(1,"Battleship",      4,2,'b',5,'b',0);
 	s2[2] = new Ship(1,"Submarine",       3,2,'c',4,'c',0);
	s2[3] = new Ship(1,"Destroyer",       3,2,'d',4,'d',0);
	s2[4] = new Ship(1,"Patrol boat",     2,2,'e',3,'e',0);


	int[][] b1  = new int[11][11];
	int[][] g1 = new int[11][11];
	int[][] a1 = new int[11][11];
	int[][] f1 = new int[11][11];

	int[][] b2  = new int[11][11];
	int[][] g2 = new int[11][11];
	int[][] a2 = new int[11][11];
	int[][] f2 = new int[11][11];

	int i = 0;
	int j = 0;
	int k = 0;
	int l = 0;
	int m = 0;
	//int true = 1;
	int b = 0;
	int player = 1;	
	char c;
	int inrow;
	int count1 = 0;
	int count2 = 0;



	for(Ship e: s1)	
	{

 		
		if((e.getColf() == 'a' || e.getColf() == 'A'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][1] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 1; i < (1+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}


		
		if((e.getColf() == 'b' || e.getColf() == 'B'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][2] = 1;

			if(e.getRowf() == e.getRowb())
				for(i = 2; i < (2+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}



		if((e.getColf() == 'c' || e.getColf() == 'C'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][3] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 3; i < (3+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}

		if((e.getColf() == 'd' || e.getColf() == 'D'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][4] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 4; i < (4+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}  

		if((e.getColf() == 'e' || e.getColf() == 'E'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][5] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 5; i < (5+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}   

		if((e.getColf() == 'f' || e.getColf() == 'F'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][6] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 6; i < (6+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}   

		if((e.getColf() == 'g' || e.getColf() == 'G'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][7] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 7; i < (7+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}  

		if((e.getColf() == 'h' || e.getColf() == 'H'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); j < (e.getRowf()+e.getSize()); i++ )
					b1[i][8] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 8; i < (8+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}   

		if((e.getColf() == 'i' || e.getColf() == 'I'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); j < (e.getRowf()+e.getSize()); i++ )
					b1[i][9] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 9; i < (9+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}   

		if((e.getColf() == 'j' || e.getColf() == 'J'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); j < (e.getRowf()+e.getSize()); i++ )
					b1[i][10] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 10; i < (10+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
			
		}       

               
	}



	for(Ship e: s2)	
	{

 		
		if((e.getColf() == 'a' || e.getColf() == 'A'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][1] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 1; i < (1+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}


		
		if((e.getColf() == 'b' || e.getColf() == 'B'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][2] = 1;

			if(e.getRowf() == e.getRowb())
				for(i = 2; i < (2+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}



		if((e.getColf() == 'c' || e.getColf() == 'C'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][3] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 3; i < (3+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}

		if((e.getColf() == 'd' || e.getColf() == 'D'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][4] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 4; i < (4+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}  

		if((e.getColf() == 'e' || e.getColf() == 'E'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][5] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 5; i < (5+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}   

		if((e.getColf() == 'f' || e.getColf() == 'F'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][6] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 6; i < (6+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}   

		if((e.getColf() == 'g' || e.getColf() == 'G'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); i < (e.getRowf()+e.getSize()); i++ )
					b1[i][7] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 7; i < (7+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}  

		if((e.getColf() == 'h' || e.getColf() == 'H'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); j < (e.getRowf()+e.getSize()); i++ )
					b1[i][8] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 8; i < (8+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}   

		if((e.getColf() == 'i' || e.getColf() == 'I'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); j < (e.getRowf()+e.getSize()); i++ )
					b1[i][9] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 9; i < (9+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
				
		}   

		if((e.getColf() == 'j' || e.getColf() == 'J'))
		{
			if(e.getColf() == e.getColb())
				for(i = e.getRowf(); j < (e.getRowf()+e.getSize()); i++ )
					b1[i][10] = 1;
				
			if(e.getRowf() == e.getRowb())
				for(i = 10; i < (10+e.getSize()); i++ )
					b1[e.getRowf()][i] = 1;
			
		}       

               
	}



	while((count1 < 17) || (count2 < 17))
	{



		
		


		System.out.println("\n\n\nPlayer: " + player + "'s turn:\n"); 
		if(player == 1)
		{
			b= 0;


			for(i = 1; i <= 10; i++)
			{
				for(j = 1; j <= 10; j++)
				{
					if(g1[i][j] == 3)
						System.out.print("* ");
					if(g1[i][j] == 1)
						System.out.print("X ");
					if(g1[i][j] == 0)
						System.out.print(g1[i][j] + " "); 
				}

				System.out.print("\n");
			}

			System.out.print("\nEnter a charactor from A - k for column and and number from 1 - 10 \nrespectively for the quardents of your shot: ");

			c = in.next().charAt(0);
			inrow  = in.nextInt();
		


			for(i = 1; i <= 10; i++)
				for(j = 1; j <= 10; j++)
					f1[i][j] = 5;




			if(c == 'A' || c == 'a')
				f1[inrow][1] = 1;
			if(c == 'B' || c == 'b')
				f1[inrow][2] = 1;
			if(c == 'C' || c == 'c')
				f1[inrow][3] = 1;
			if(c == 'D' || c == 'd')
				f1[inrow][4] = 1;
			if(c == 'E' || c == 'e')
				f1[inrow][5] = 1;
			if(c == 'F' || c == 'f')
				f1[inrow][6] = 1;
			if(c == 'G' || c == 'g')
				f1[inrow][7] = 1;
			if(c == 'H' || c == 'h')
				f1[inrow][8] = 1;
			if(c == 'I' || c == 'i')
				f1[inrow][9] = 1;
			if(c == 'J' || c == 'j')
				f1[inrow][10] = 1;




		


			for(i = 1; i <= 10; i++)
				for(j = 1; j <= 10; j++)
					if(  (f1[i][j] == 1) && (g1[i][j] == 3))
						b = 1;


	





			if( b == 0)
			{

				if(c == 'A' || c == 'a')
					g1[inrow][1] = 1;
				if(c == 'B' || c == 'b')
					g1[inrow][2] = 1;
				if(c == 'C' || c == 'c')
					g1[inrow][3] = 1;
				if(c == 'D' || c == 'd')
					g1[inrow][4] = 1;
				if(c == 'E' || c == 'e')
					g1[inrow][5] = 1;
				if(c == 'F' || c == 'f')
					g1[inrow][6] = 1;
				if(c == 'G' || c == 'g')
					g1[inrow][7] = 1;
				if(c == 'H' || c == 'h')
					g1[inrow][8] = 1;
				if(c == 'I' || c == 'i')
					g1[inrow][9] = 1;
				if(c == 'J' || c == 'j')
					g1[inrow][10] = 1;




				for(i = 1; i <= 10; i++)
					for(j = 1; j <= 10; j++)
					{
						if( b1[i][j] == 1 && g1[i][j] == 1)
						{
							for(Ship e : s1)
							{			
		
								for(k = 0; k <= 10; k++) 
								{
									for(l = 0; l <= 10; l++)
									{
										a1[k][l] = 0;
									}
								}


								if((e.getColf() == 'a' || e.getColf() == 'A'))
 									for(k = 1; k <= 10; k++)
									{

										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a1[l][1] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 1; m < 1+e.getSize(); m++)
											{
												a1[k][m] = 1;
											}

									}


		
								if((e.getColf() == 'b' || e.getColf() == 'B'))
 									for(k = 1; k <= 10; k++)
									{

										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a1[l][2] = 1;
		


										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 2; m < 2+e.getSize(); m++)
											{
												a1[k][m] = 1;
											}

					
	
									}


	
								if((e.getColf() == 'c' || e.getColf() == 'C'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a1[l][3] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 3; m < 3+e.getSize(); m++)
											{
												a1[k][m] = 1;
											}


					
	
									}

								if((e.getColf() == 'd' || e.getColf() == 'D'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a1[l][4] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 4; m < 4+e.getSize(); m++)
											{
												a1[k][m] = 1;
											}


					
	
									}   

								if((e.getColf() == 'e' || e.getColf() == 'E'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a1[l][5] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 5; m < 5+e.getSize(); m++)
											{
												a1[k][m] = 1;
											}


					
	
									} 

								if((e.getColf() == 'f' || e.getColf() == 'F'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a1[l][6] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 6; m < 6+e.getSize(); m++)
											{
												a1[k][m] = 1;
											}


					
	
									}  
								if((e.getColf() == 'g' || e.getColf() == 'G'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a1[l][7] = 1;



									if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
										for(m = 7; m < 7+e.getSize(); m++)
										{
											a1[k][m] = 1;
										}


					
		
									} 
								if((e.getColf() == 'h' || e.getColf() == 'H'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a1[l][8] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 8; m < 8+e.getSize(); m++)
											{
												a1[k][m] = 1;
											}


					
	
								}	   

								if((e.getColf() == 'i' || e.getColf() == 'I'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a1[l][9] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 9; m < 9+e.getSize(); m++)
											{
												a1[k][m] = 1;
											}


					
	
									}  

								if((e.getColf() == 'j' || e.getColf() == 'J'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a1[l][10] = 1;
	

										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 10; m < 10+e.getSize(); m++)
											{
												a1[k][m] = 1;
											}
									}


								for(k = 1; k<=10; k++)
									for(l = 1; l<=10; l++)
										if((a1[k][l] == 1) && (g1[k][l] ==1))
											g1[k][l] = 2;
									
												 
							
								for(k = 1; k<=10; k++)
									for(l = 1; l<=10; l++)
									{
										if((a1[k][l] == 1) && (g1[k][l] ==2))
										{
											e.setHit();
											count1++;	
											if(e.getHit() == e.getSize())
												System.out.print("you sunk my : " + e.getName() + "\n");
											else
												System.out.print("\n\nPlayer 1 hit a ship\n");
									
											g1[k][l] = 3;
										}		
									}	
							}	

						}
						//else
							//System.out.print("miss");
					}
	

			}
			else
				System.out.println("Guess already made");

		}
	


		if(player == 2)
		{
			b= 0;

			
			System.out.print("\n");

			for(i = 1; i <= 10; i++)
			{
				for(j = 1; j <= 10; j++)
				{
					if(g2[i][j] == 3)
						System.out.print("* ");
					if(g2[i][j] == 1)
						System.out.print("X ");
					if(g2[i][j] == 0)
						System.out.print(g2[i][j] + " "); 
				}

				System.out.print("\n");
			}

			System.out.print("\nEnter a charactor from A - k for column and and number from 1 - 10 \nrespectively for the quardents of your shot: ");
			c = in.next().charAt(0);
			inrow  = in.nextInt();



			for(i = 1; i <= 10; i++)
				for(j = 1; j <= 10; j++)
					f2[i][j] = 5;


			if(c == 'A' || c == 'a')
				f2[inrow][1] = 1;
			if(c == 'B' || c == 'b')
				f2[inrow][2] = 1;
			if(c == 'C' || c == 'c')
				f2[inrow][3] = 1;
			if(c == 'D' || c == 'd')
				f2[inrow][4] = 1;
			if(c == 'E' || c == 'e')
				f2[inrow][5] = 1;
			if(c == 'F' || c == 'f')
				f2[inrow][6] = 1;
			if(c == 'G' || c == 'g')
				f2[inrow][7] = 1;
			if(c == 'H' || c == 'h')
				f2[inrow][8] = 1;
			if(c == 'I' || c == 'i')
				f2[inrow][9] = 1;
			if(c == 'J' || c == 'j')
				f2[inrow][10] = 1;




		


			for(i = 1; i <= 10; i++)
				for(j = 1; j <= 10; j++)
					if(  (f2[i][j] == 1) && (g2[i][j] == 3))
						b = 1;


	
				

			System.out.print("\n");





			if( b == 0)
			{

				if(c == 'A' || c == 'a')
					g2[inrow][1] = 1;
				if(c == 'B' || c == 'b')
					g2[inrow][2] = 1;
				if(c == 'C' || c == 'c')
					g2[inrow][3] = 1;
				if(c == 'D' || c == 'd')
					g2[inrow][4] = 1;
				if(c == 'E' || c == 'e')
					g2[inrow][5] = 1;
				if(c == 'F' || c == 'f')
					g2[inrow][6] = 1;
				if(c == 'G' || c == 'g')
					g2[inrow][7] = 1;
				if(c == 'H' || c == 'h')
					g2[inrow][8] = 1;
				if(c == 'I' || c == 'i')
					g2[inrow][9] = 1;
				if(c == 'J' || c == 'j')
					g2[inrow][10] = 1;






				for(i = 1; i <= 10; i++)
					for(j = 1; j <= 10; j++)
					{
						if( b2[i][j] == 1 && g2[i][j] == 1)
						{
							for(Ship e : s2)
							{	


						
		
								for(k = 0; k <= 10; k++) 
								{
									for(l = 0; l <= 10; l++)
									{
										a2[k][l] = 0;
									}
								}


								if((e.getColf() == 'a' || e.getColf() == 'A'))
 									for(k = 1; k <= 10; k++)
									{

										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a2[l][1] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 1; m < 1+e.getSize(); m++)
											{
												a2[k][m] = 1;
											}

									}


		
								if((e.getColf() == 'b' || e.getColf() == 'B'))
 									for(k = 1; k <= 10; k++)
									{

										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a2[l][2] = 1;
		


										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 2; m < 2+e.getSize(); m++)
											{
												a2[k][m] = 1;
											}

					
	
									}


	
								if((e.getColf() == 'c' || e.getColf() == 'C'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a2[l][3] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 3; m < 3+e.getSize(); m++)
											{
												a2[k][m] = 1;
											}


					
	
									}

								if((e.getColf() == 'd' || e.getColf() == 'D'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a2[l][4] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 4; m < 4+e.getSize(); m++)
											{
												a2[k][m] = 1;
											}


					
	
									}   

								if((e.getColf() == 'e' || e.getColf() == 'E'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a2[l][5] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 5; m < 5+e.getSize(); m++)
											{
												a2[k][m] = 1;
											}


					
	
									} 

								if((e.getColf() == 'f' || e.getColf() == 'F'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a2[l][6] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 6; m < 6+e.getSize(); m++)
											{
												a2[k][m] = 1;
											}


					
	
									}  
								if((e.getColf() == 'g' || e.getColf() == 'G'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a2[l][7] = 1;



									if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
										for(m = 7; m < 7+e.getSize(); m++)
										{
											a2[k][m] = 1;
										}


					
		
									} 
								if((e.getColf() == 'h' || e.getColf() == 'H'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a2[l][8] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 8; m < 8+e.getSize(); m++)
											{
												a2[k][m] = 1;
											}


					
	
								}	   

								if((e.getColf() == 'i' || e.getColf() == 'I'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a2[l][9] = 1;



										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 9; m < 9+e.getSize(); m++)
											{
												a2[k][m] = 1;
											}


					
	
									}  

								if((e.getColf() == 'j' || e.getColf() == 'J'))
 									for(k = 1; k <= 10; k++)
									{
										if(e.getColf() == e.getColb() && (k == e.getRowf()))
											for(l = k; l < (k+e.getSize()); l++ )
												a2[l][10] = 1;
	


										if(  (e.getRowf() == e.getRowb() ) && (k == e.getRowf()) )
											for(m = 10; m < 10+e.getSize(); m++)
											{
												a2[k][m] = 1;
											}


					
	
									}

	


								for(k = 1; k<=10; k++)
									for(l = 1; l<=10; l++)
										if((a2[k][l] == 1) && (g2[k][l] ==1))
											g2[k][l] = 2;
									
								

							
								for(k = 1; k<=10; k++)
									for(l = 1; l<=10; l++)
									{
										if((a2[k][l] == 1) && (g2[k][l] ==2))
										{
											e.setHit();
											count2++;	
											if(e.getHit() == e.getSize())
												System.out.print("you sunk my : " + e.getName() + "\n");
											else
												System.out.print("\n\nPlayer 2 hit a ship\n");
									

											
											g2[k][l] = 3;
										}		
									}

							}	
	
					



						}
						//else
							//System.out.print("miss");
					}







			}
			else
				System.out.println("Guess already made");

		}

		if(player == 1)
			player = 2;
		else if(player == 2)
			player = 1;

	}

	
  }
}

class Ship
{
	public Ship(int p, String s, int z, int rf, char cf, int rb, char cb, int h)
 	{
		player = p;
		name   = s;		
 		size   = z; 		
		rowf   = rf; 
 		colf   = cf;
 		rowb   = rb; 
 		colb   = cb;
 		hit    = h;
 	}

	public String getName()
	{
		return name;
	}

	public int getSize()
	{
		return size;
	}

	public int getRowf()
	{
		return rowf;
	}

	public int getRowb()
	{
		return rowb;
	}


	public char getColf()
	{
		return colf;
	}


	public char getColb()
	{
		return colb;
	}


	public int getPlayer()
	{
		return player;
	}
	public int getHit()
	{
		return hit;
	}
	
	public void setHit()
	{
		hit = hit +1;
	}




			
	
	private String name;		
 	private int size; 		    //size of ship
	private int rowf; 
 	private char colf;		    //column front char
 	private int rowb; 
 	private char colb;		    //column back char
 	private int player;
	private int hit;
} 






Advertisement

I believe this post should be in the coding horror forum...

Oh the horror of such a long method.

Your code is horribly hard to read, maybe split it up into methods to make things easier for us? wacko.png

Mobile Developer at PawPrint Games ltd.

(Not "mobile" as in I move around a lot, but as in phones, mobile phone developer)

(Although I am mobile. no, not as in a babies mobile, I move from place to place)

(Not "place" as in fish, but location.)

k

I believe this post should be in the coding horror forum...

Oh the horror of such a long method.

Your code is horribly hard to read, maybe split it up into methods to make things easier for us? wacko.png

Well, this is For Beginners, so if you're not being constructive then please don't respond at all. Presumably, the OP wouldn't be asking the question if they found this easy to do.

There are numerous issues with your current program that make it very difficult to read. The first is the variable names. Short names like "s1", "e" and "b1" are devoid of meaning. While you might know what they mean now, we don't and in a few weeks or months you won't remember either.

Instead, use descriptive names like playerOneFleet. You'll almost never regret using a longer word - remember a good IDE can offer you auto-completion so you won't even need to type it out fully!

The next issue is that your program contains lots of magic numbers. The integers in the "g" array seem to correspond with various states, but these are all implicit. Consider using named constants:


final int UNKNOWN = 0;
final int MISS = 1
final int HIT = 2;

// Later ...

if (boardPosition[y][x] == UNKNOWN) {
    if (containsShip(y, x)) {
        damageShip(y, x);
        boardPosition[y][x] = HIT;
    } else {
        boardPosition[y][x] = MISS;
    }
} else {
    System.out.println("You've already made that move!");
}

Better still would be to use an enum to represent this, but I'm not sure if you're familiar with them yet.

Another thing is modelling. You've modelled the Ship explicitly, but everything else is in a bunch of primitive arrays. Consider adding a class that represents the playing board itself, or perhaps two classes, one for the opponents "view" and another for the "true state" board. Try to move much of the logic into these classes.

Then, you should be able to cut the main() method's size in half by saying that a game consists of two player's boards - an array. You have an integer index for the active player. Each turn, you switch the active player between 0 and 1, and delegate to the appropriate object, maybe something like this:


public static void main(String args[]) {
    Board[] boards = { new Board(), new Board() };
    int activePlayer = 0;
    while (!isGameOver(boards)) {
        Board activeBoard = boards[activePlayer];
        Position position = new Position();
        while (!position.isValid()) {
            activeBoard.display();
            position = activeBoard.getMove();
        }
        activeBoard.makeMove(position);
        
        if (activePlayer == 0) {
            activePlayer = 1;
        } else {
            activePlayer = 0;
        }
    }
}


The first is the variable names. Short names like "s1", "e" and "b1" are devoid of meaning.

Yes. It took a while to figure out rowf = rowFront. Naming things is really hard, and when you're just starting out, you may think "Well, I know what all this is, so I don't need descriptive names." But when you come back to your code later, you won't remember all this stuff.

For example, to make it easier to understand, I would make a simple point class:



public class Point {

  private int x;
  private int y;

  public Point( int x, int y ) {
    this.x = x;
    this.y = y;
  }

  public int getX() { return x; }
  public int getY() { return y; }

}

Then, instead of rowf, colf, rowb, colb, I would do this:



  Point bow = new Point( 0, 1 );
  Point stern = new Point( 0, 3 );

And as rip-off pointed out, when you see the same code repeated over and over again, there is probably a better way to do it.

My first program, which shuffled a deck of cards in C++, looked just like this. I had so many nested for loops I'm surprised it didn't run out of memory.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

I put everything in methods



import java.util.*;

public class a4 
{
  public static void main( String args[] ) 
  {
	Ship[] s1 = new Ship[5];
	Ship[] s2 = new Ship[5];

	Scanner in = new Scanner(System.in);
	

	s1[0] = new Ship(1,"Aircraft carrier",5,1,'a',1,'e',0);  // player, ship, size, row front, column front, row back, column back, hit
	s1[1] = new Ship(1,"Battleship",      4,2,'b',5,'b',0);
 	s1[2] = new Ship(1,"Submarine",       3,2,'c',4,'c',0);
	s1[3] = new Ship(1,"Destroyer",       3,2,'d',4,'d',0);
	s1[4] = new Ship(1,"Patrol boat",     2,2,'e',3,'e',0);

	s2[0] = new Ship(1,"Aircraft carrier",5,1,'a',1,'e',0);  // player, ship, size, row front, column front, row back, column back, hit
	s2[1] = new Ship(1,"Battleship",      4,2,'b',5,'b',0);
 	s2[2] = new Ship(1,"Submarine",       3,2,'c',4,'c',0);
	s2[3] = new Ship(1,"Destroyer",       3,2,'d',4,'d',0);
	s2[4] = new Ship(1,"Patrol boat",     2,2,'e',3,'e',0);


	int[][] b1  = new int[11][11];
	int[][] g1  = new int[11][11];
	int[][] a1  = new int[11][11];
	int[][] f1  = new int[11][11];

	int[][] b2  = new int[11][11];
	int[][] g2  = new int[11][11];
	int[][] f2  = new int[11][11];

	int i = 0;
	int j = 0;
	int k = 0;
	int l = 0;
	int m = 0;
	int b = 0;
	int player = 1;	
	char c;
	int inrow;
	int count1 = 0;
	int count2 = 0;



	for(Ship e: s1)
		check(e, b1);

	for(Ship e: s2)	
		check(e, b2);



	while((count1 < 17) || (count2 < 17))
	{
		System.out.println("\n\n\nPlayer: " + player + "'s turn:\n"); 
		if(player == 1)
		{
			b= 0;


			for(i = 1; i <= 10; i++)
			{
				for(j = 1; j <= 10; j++)
				{
					if(g1[i][j] == 3)
						System.out.print("* ");
					if(g1[i][j] == 1)
						System.out.print("X ");
					if(g1[i][j] == 0)
						System.out.print(g1[i][j] + " "); 
				}

				System.out.print("\n");
			}

			System.out.print("\nEnter a charactor from A - k for column and and number from 1 - 10 \nrespectively for the quardents of your shot: ");

			c = in.next().charAt(0);
			inrow  = in.nextInt();
		


			for(i = 1; i <= 10; i++)
				for(j = 1; j <= 10; j++)
					f1[i][j] = 5;




			check1(f1,c,inrow);


			for(i = 1; i <= 10; i++)
				for(j = 1; j <= 10; j++)
					if(  (f1[i][j] == 1) && (g1[i][j] == 3))
						b = 1;


	



			if( b == 0)
			{

				check1(g1,c,inrow);


				for(i = 1; i <= 10; i++)
					for(j = 1; j <= 10; j++)
					{
						if( b1[i][j] == 1 && g1[i][j] == 1)
						{
							for(Ship e : s1)
							{			
		
								for(k = 0; k <= 10; k++) 
								{
									for(l = 0; l <= 10; l++)
									{
										a1[k][l] = 0;
									}
								}


								check(e,a1);								
												 
							
								for(k = 1; k<=10; k++)
									for(l = 1; l<=10; l++)
									{
										if((a1[k][l] == 1) && (g1[k][l] ==1))
										{
											e.setHit();
											count1++;	
											if(e.getHit() == e.getSize())
												System.out.print("you sunk my : " + e.getName() + "\n");
											else
												System.out.print("\n\nPlayer 1 hit a ship\n");
									
											g1[k][l] = 3;
										}		
									}	
							}	

						}
						//else
							//System.out.print("miss");
					}
	

			}
			else
				System.out.println("Guess already made");

		}
	


		if(player == 2)
		{
			b= 0;

			
			System.out.print("\n");

			for(i = 1; i <= 10; i++)
			{
				for(j = 1; j <= 10; j++)
				{
					if(g2[i][j] == 3)
						System.out.print("* ");
					if(g2[i][j] == 1)
						System.out.print("X ");
					if(g2[i][j] == 0)
						System.out.print(g2[i][j] + " "); 
				}

				System.out.print("\n");
			}

			System.out.print("\nEnter a charactor from A - k for column and and number from 1 - 10 \nrespectively for the quardents of your shot: ");
			c = in.next().charAt(0);
			inrow  = in.nextInt();



			for(i = 1; i <= 10; i++)
				for(j = 1; j <= 10; j++)
					f2[i][j] = 5;


			check1(f2,c,inrow);



			for(i = 1; i <= 10; i++)
				for(j = 1; j <= 10; j++)
					if(  (f2[i][j] == 1) && (g2[i][j] == 3))
						b = 1;


	
				

			System.out.print("\n");





			if( b == 0)
			{
				check1(g2,c,inrow);

				for(i = 1; i <= 10; i++)
					for(j = 1; j <= 10; j++)
					{
						if( b2[i][j] == 1 && g2[i][j] == 1)
						{
							for(Ship e : s2)
							{	


						
		
								for(k = 0; k <= 10; k++) 
								{
									for(l = 0; l <= 10; l++)
									{
										a1[k][l] = 0;
									}
								}


								check(e,a1);

									
								
							
								for(k = 1; k<=10; k++)
									for(l = 1; l<=10; l++)
									{
										if((a1[k][l] == 1) && (g2[k][l] == 1))
										{
											e.setHit();
											count2++;	
											if(e.getHit() == e.getSize())
												System.out.print("you sunk my : " + e.getName() + "\n");
											else
												System.out.print("\n\nPlayer 2 hit a ship\n");
									

											
											g2[k][l] = 3;
										}		
									}

							}	
	
					



						}
						//else
							//System.out.print("miss");
					}







			}
			else
				System.out.println("Guess already made");

		}

		if(player == 1)
			player = 2;
		else if(player == 2)
			player = 1;

	}	
  }



	public static void check1(int[][] a, char ch, int ir)
	{

			if(ch == 'A' || ch == 'a')
				a[ir][1] = 1;
			if(ch == 'B' || ch == 'b')
				a[ir][2] = 1;
			if(ch == 'C' || ch == 'c')
				a[ir][3] = 1;
			if(ch == 'D' || ch == 'd')
				a[ir][4] = 1;
			if(ch == 'E' || ch == 'e')
				a[ir][5] = 1;
			if(ch == 'F' || ch == 'f')
				a[ir][6] = 1;
			if(ch == 'G' || ch == 'g')
				a[ir][7] = 1;
			if(ch == 'H' || ch == 'h')
				a[ir][8] = 1;
			if(ch == 'I' || ch == 'i')
				a[ir][9] = 1;
			if(ch == 'J' || ch == 'j')
				a[ir][10] = 1;

	}




	public static void check(Ship x, int[][] a)
	{

		int i;
		
		if((x.getColf() == 'a' || x.getColf() == 'A'))
		{
			if(x.getColf() == x.getColb())
				for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
					a[i][1] = 1;
				
			if(x.getRowf() == x.getRowb())
				for(i = 1; i < (1+x.getSize()); i++ )
					a[x.getRowf()][i] = 1;
				
		}


		
		if((x.getColf() == 'b' || x.getColf() == 'B'))
		{
			if(x.getColf() == x.getColb())
				for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
					a[i][2] = 1;

			if(x.getRowf() == x.getRowb())
				for(i = 2; i < (2+x.getSize()); i++ )
					a[x.getRowf()][i] = 1;
				
		}



		if((x.getColf() == 'c' || x.getColf() == 'C'))
		{
			if(x.getColf() == x.getColb())
				for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
					a[i][3] = 1;
				
			if(x.getRowf() == x.getRowb())
				for(i = 3; i < (3+x.getSize()); i++ )
					a[x.getRowf()][i] = 1;
				
		}

		if((x.getColf() == 'd' || x.getColf() == 'D'))
		{
			if(x.getColf() == x.getColb())
				for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
					a[i][4] = 1;
				
			if(x.getRowf() == x.getRowb())
				for(i = 4; i < (4+x.getSize()); i++ )
					a[x.getRowf()][i] = 1;
				
		}  

		if((x.getColf() == 'e' || x.getColf() == 'E'))
		{
			if(x.getColf() == x.getColb())
				for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
					a[i][5] = 1;
				
			if(x.getRowf() == x.getRowb())
				for(i = 5; i < (5+x.getSize()); i++ )
					a[x.getRowf()][i] = 1;
				
		}   

		if((x.getColf() == 'f' || x.getColf() == 'F'))
		{
			if(x.getColf() == x.getColb())
				for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
					a[i][6] = 1;
				
			if(x.getRowf() == x.getRowb())
				for(i = 6; i < (6+x.getSize()); i++ )
					a[x.getRowf()][i] = 1;
				
		}   

		if((x.getColf() == 'g' || x.getColf() == 'G'))
		{
			if(x.getColf() == x.getColb())
				for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
					a[i][7] = 1;
				
			if(x.getRowf() == x.getRowb())
				for(i = 7; i < (7+x.getSize()); i++ )
					a[x.getRowf()][i] = 1;
				
		}  

		if((x.getColf() == 'h' || x.getColf() == 'H'))
		{
			if(x.getColf() == x.getColb())
				for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
					a[i][8] = 1;
				
			if(x.getRowf() == x.getRowb())
				for(i = 8; i < (8+x.getSize()); i++ )
					a[x.getRowf()][i] = 1;
				
		}   

		if((x.getColf() == 'i' || x.getColf() == 'I'))
		{
			if(x.getColf() == x.getColb())
				for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
					a[i][9] = 1;
				
			if(x.getRowf() == x.getRowb())
				for(i = 9; i < (9+x.getSize()); i++ )
					a[x.getRowf()][i] = 1;
				
		}   

		if((x.getColf() == 'j' || x.getColf() == 'J'))
		{
			if(x.getColf() == x.getColb())
				for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
					a[i][10] = 1;
				
			if(x.getRowf() == x.getRowb())
				for(i = 10; i < (10+x.getSize()); i++ )
					a[x.getRowf()][i] = 1;
			
		}       

	}



}

class Ship
{
	public Ship(int p, String s, int z, int rf, char cf, int rb, char cb, int h)
 	{
		player = p;
		name   = s;		
 		size   = z; 		
		rowf   = rf; 
 		colf   = cf;
 		rowb   = rb; 
 		colb   = cb;
 		hit    = h;
 	}

	public String getName()
	{
		return name;
	}

	public int getSize()
	{
		return size;
	}

	public int getRowf()
	{
		return rowf;
	}

	public int getRowb()
	{
		return rowb;
	}


	public char getColf()
	{
		return colf;
	}


	public char getColb()
	{
		return colb;
	}


	public int getPlayer()
	{
		return player;
	}
	public int getHit()
	{
		return hit;
	}
	
	public void setHit()
	{
		hit = hit +1;
	}


			
	
	private String name;		
 	private int size; 		    //size of ship
	private int rowf; 
 	private char colf;		    //column front char
 	private int rowb; 
 	private char colb;		    //column back char
 	private int player;
	private int hit;
} 






Yeah I know to use descriptive variable names I just didn't think I was going to be showing this to anyone, I will change.

Well Glass_Knife it shouldn't have been that hard to know what rowf meant since I had a comment right in front of it explaining what it meant.

Here it is with meaningful variable names and methods




import java.util.*;

public class a4 
{
  public static void main( String args[] ) 
  {
	Ship[] playerOneShips = new Ship[5];
	Ship[] playerTwoShips = new Ship[5];

	
	

	playerOneShips[0] = new Ship(1,"Aircraft carrier",5,1,'a',1,'e',0);  // player, ship, size, row front, column front, row back, column back, hit
	playerOneShips[1] = new Ship(1,"Battleship",      4,2,'b',5,'b',0);
 	playerOneShips[2] = new Ship(1,"Submarine",       3,2,'c',4,'c',0);
	playerOneShips[3] = new Ship(1,"Destroyer",       3,2,'d',4,'d',0);
	playerOneShips[4] = new Ship(1,"Patrol boat",     2,2,'e',3,'e',0);

	playerTwoShips[0] = new Ship(1,"Aircraft carrier",5,1,'a',1,'e',0);  // player, ship, size, row front, column front, row back, column back, hit
	playerTwoShips[1] = new Ship(1,"Battleship",      4,2,'b',5,'b',0);
 	playerTwoShips[2] = new Ship(1,"Submarine",       3,2,'c',4,'c',0);
	playerTwoShips[3] = new Ship(1,"Destroyer",       3,2,'d',4,'d',0);
	playerTwoShips[4] = new Ship(1,"Patrol boat",     2,2,'e',3,'e',0);


	int[][] playerOneBoard  = new int[11][11];   //All the ships in an array
	int[][] playerOneGuess  = new int[11][11];
	
	

	int[][] playerTwoBoard  = new int[11][11];
	int[][] playerTwoGuess  = new int[11][11];
	int[][] count  = new int[1][2];

	
	
	int player = 1;	
	int count1 = 0;
	int count2 = 0;


	for(Ship e: playerOneShips)
		arrayAssignShip(e, playerOneBoard);

	for(Ship e: playerTwoShips)	
		arrayAssignShip(e, playerTwoBoard);



	while( (count1 < 17) && (count2 < 17) )
	{

		System.out.println("count[0][0] = " + count[0][0]);
		System.out.println("count[0][1] = " + count[0][1]);

		if(player == 1)
			player = play(playerOneBoard, playerOneGuess, playerOneShips,player,count);

		if((player == 2) && (count[0][0] < 17))
			player = play(playerTwoBoard, playerTwoGuess, playerTwoShips,player,count);

		count1 = count[0][0];
		count2 = count[0][1];

		System.out.println("count1 = " + count1);
		System.out.println("count2 = " + count2);

	}	
  }


  public static int play(int[][] playerBoard, int[][] playerGuess, Ship[] playerShips, int player, int[][] count)
  {
	Scanner in = new Scanner(System.in);
	int[][] shipBoard  = new int[11][11];	     //One Ship at a time in an array to check if ship was hit
	int[][] currentGuess  = new int[11][11];     //Current Guess in an array

	int i = 0;
	int j = 0;
	int k = 0;
	int l = 0;
	int m = 0;
	int b = 0;
	char c;
	int inrow;
	

		
	System.out.println("\n\n\nPlayer: " + player + "'s turn:\n");


	for(i = 1; i <= 10; i++)
	{
		for(j = 1; j <= 10; j++)
		{
			if(playerGuess[i][j] == 3)
				System.out.print("* ");
			if(playerGuess[i][j] == 1)
				System.out.print("X ");
			if(playerGuess[i][j] == 0)
				System.out.print(playerGuess[i][j] + " "); 
		}

		System.out.print("\n");
	}

	System.out.print("\nEnter a charactor from A - k for column and and number from 1 - 10 \nrespectively for the quardents of your shot: ");
	c = in.next().charAt(0);
	inrow  = in.nextInt();



	for(i = 1; i <= 10; i++)
		for(j = 1; j <= 10; j++)
			currentGuess[i][j] = 5;


	arrayAssignInput(currentGuess,c,inrow);



	for(i = 1; i <= 10; i++)
		for(j = 1; j <= 10; j++)
			if(  (currentGuess[i][j] == 1) && ((playerGuess[i][j] == 3) || (playerGuess[i][j] == 1)))
				b = 1;


	if( b == 0)
	{
		arrayAssignInput(playerGuess,c,inrow);

		for(i = 1; i <= 10; i++)
			for(j = 1; j <= 10; j++)
			{
				if( playerBoard[i][j] == 1 && playerGuess[i][j] == 1)
				{
					for(Ship x : playerShips)
					{	
		
						for(k = 0; k <= 10; k++) 
						{
							for(l = 0; l <= 10; l++)
							{
								shipBoard[k][l] = 0;
							}
						}


						arrayAssignShip(x,shipBoard);
		
				
						for(k = 1; k<=10; k++)
							for(l = 1; l<=10; l++)
							{
								if((shipBoard[k][l] == 1) && (playerGuess[k][l] == 1))
								{
									x.setHit();

									if(player == 1)
										count[0][0]++;
									if(player == 2)
										count[0][1]++;
	
									if(x.getHit() == x.getSize())
										System.out.print("you sunk my : " + x.getName() + "\n");
									else
										System.out.print("\n\nPlayer hit a ship\n");
											
									playerGuess[k][l] = 3;
								}		
							}
					}	
				}
						//else
							//System.out.print("miss");
			}
			
		
		if(player == 1)
			player = 2;
		else if(player == 2)
			player = 1;
			
	}
	else
		System.out.println("\n\nGuess already made");

	return player;
  }





  public static void arrayAssignInput(int[][] a, char ch, int ir)
  {

	if(ch == 'A' || ch == 'a')
		a[ir][1] = 1;
	if(ch == 'B' || ch == 'b')
		a[ir][2] = 1;
	if(ch == 'C' || ch == 'c')
		a[ir][3] = 1;
	if(ch == 'D' || ch == 'd')
		a[ir][4] = 1;
	if(ch == 'E' || ch == 'e')
		a[ir][5] = 1;
	if(ch == 'F' || ch == 'f')
		a[ir][6] = 1;
	if(ch == 'G' || ch == 'g')
		a[ir][7] = 1;
	if(ch == 'H' || ch == 'h')
		a[ir][8] = 1;
	if(ch == 'I' || ch == 'i')
		a[ir][9] = 1;
	if(ch == 'J' || ch == 'j')
		a[ir][10] = 1;
  }




  public static void arrayAssignShip(Ship x, int[][] a)
  {

	int i;
		
	if((x.getColf() == 'a' || x.getColf() == 'A'))
	{
		if(x.getColf() == x.getColb())
			for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
				a[i][1] = 1;
			
		if(x.getRowf() == x.getRowb())
			for(i = 1; i < (1+x.getSize()); i++ )
				a[x.getRowf()][i] = 1;
			
	}
		
	if((x.getColf() == 'b' || x.getColf() == 'B'))
	{
		if(x.getColf() == x.getColb())
			for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
				a[i][2] = 1;

		if(x.getRowf() == x.getRowb())
			for(i = 2; i < (2+x.getSize()); i++ )
				a[x.getRowf()][i] = 1;
				
	}


	if((x.getColf() == 'c' || x.getColf() == 'C'))
	{
		if(x.getColf() == x.getColb())
			for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
				a[i][3] = 1;
				
		if(x.getRowf() == x.getRowb())
			for(i = 3; i < (3+x.getSize()); i++ )
				a[x.getRowf()][i] = 1;
				
	}

	if((x.getColf() == 'd' || x.getColf() == 'D'))
	{
		if(x.getColf() == x.getColb())
			for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
				a[i][4] = 1;
				
		if(x.getRowf() == x.getRowb())
			for(i = 4; i < (4+x.getSize()); i++ )
				a[x.getRowf()][i] = 1;
				
	}  

	if((x.getColf() == 'e' || x.getColf() == 'E'))
	{
		if(x.getColf() == x.getColb())
			for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
				a[i][5] = 1;
			
		if(x.getRowf() == x.getRowb())
			for(i = 5; i < (5+x.getSize()); i++ )
				a[x.getRowf()][i] = 1;
				
	}   

	if((x.getColf() == 'f' || x.getColf() == 'F'))
	{
		if(x.getColf() == x.getColb())
			for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
				a[i][6] = 1;
				
		if(x.getRowf() == x.getRowb())
			for(i = 6; i < (6+x.getSize()); i++ )
				a[x.getRowf()][i] = 1;
				
	}   

	if((x.getColf() == 'g' || x.getColf() == 'G'))
	{
		if(x.getColf() == x.getColb())
			for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
				a[i][7] = 1;
				
		if(x.getRowf() == x.getRowb())
			for(i = 7; i < (7+x.getSize()); i++ )
				a[x.getRowf()][i] = 1;
				
	}  

	if((x.getColf() == 'h' || x.getColf() == 'H'))
	{
		if(x.getColf() == x.getColb())
			for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
					a[i][8] = 1;
			
		if(x.getRowf() == x.getRowb())
			for(i = 8; i < (8+x.getSize()); i++ )
				a[x.getRowf()][i] = 1;
				
	}   

	if((x.getColf() == 'i' || x.getColf() == 'I'))
	{
		if(x.getColf() == x.getColb())
			for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
				a[i][9] = 1;
				
		if(x.getRowf() == x.getRowb())
			for(i = 9; i < (9+x.getSize()); i++ )
				a[x.getRowf()][i] = 1;
				
	}   

	if((x.getColf() == 'j' || x.getColf() == 'J'))
	{
		if(x.getColf() == x.getColb())
			for(i = x.getRowf(); i < (x.getRowf()+x.getSize()); i++ )
				a[i][10] = 1;
				
		if(x.getRowf() == x.getRowb())
			for(i = 10; i < (10+x.getSize()); i++ )
				a[x.getRowf()][i] = 1;
			
	}       

  }



}

class Ship
{
	public Ship(int p, String s, int z, int rf, char cf, int rb, char cb, int h)
 	{
		player = p;
		name   = s;		
 		size   = z; 		
		rowFront   = rf; 
 		colFront   = cf;
 		rowBack   = rb; 
 		colBack   = cb;
 		hit    = h;
 	}

	public String getName()
	{
		return name;
	}

	public int getSize()
	{
		return size;
	}

	public int getRowf()
	{
		return rowFront;
	}

	public int getRowb()
	{
		return rowBack;
	}


	public char getColf()
	{
		return colFront;
	}


	public char getColb()
	{
		return colBack;
	}


	public int getPlayer()
	{
		return player;
	}
	public int getHit()
	{
		return hit;
	}
	
	public void setHit()
	{
		hit = hit +1;
	}


			
	
	private String name;		
 	private int size; 		    //size of ship
	private int rowFront; 
 	private char colFront;		    //column front char
 	private int rowBack; 
 	private char colBack;		    //column back char
 	private int player;
	private int hit;
} 






This topic is closed to new replies.

Advertisement