• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
4mad3u5

Battleship in java

18 posts in this topic

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






Edited by 4mad3u5
0

Share this post


Link to post
Share on other sites

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

-1

Share this post


Link to post
Share on other sites

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






Edited by 4mad3u5
0

Share this post


Link to post
Share on other sites

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. 

0

Share this post


Link to post
Share on other sites

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. 

-2

Share this post


Link to post
Share on other sites

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






Edited by 4mad3u5
0

Share this post


Link to post
Share on other sites

I'm not familiar with Java, so I just googled this and haven't tested it, but you can make some optimizations like this:

 

Instead of

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;

	}

use

public static void arrayAssignInput(int[][] a, char ch, int ir)
{
    char upper = Character.toUpperCase(ch)
    int val = (int)upper - (int)'A' + 1;
    a[ir][val] = 1;
}
0

Share this post


Link to post
Share on other sites

I don't want to argue semantics with people, I just want to know how to optimize my code. 

-2

Share this post


Link to post
Share on other sites

I don't want to argue semantics with people, I just want to know how to optimize my code. 

 

1.  Understand the more you learn about programming, the harder it gets.  Frustration is good.  It means you are learning.  If you expect to master programming this month you will disappointed. 

 

2.  You are not trying to optimize your code, but are trying to refactor the code.  The two are not mutually exclusive, but you are not replacing you algorithms to improve the speed or reduce the memory usage, you just want to make the code easier to read without changing the behavior.  That's refactoring.

 

Watch this:

 

https://www.youtube.com/watch?v=vhYK3pDUijk

2

Share this post


Link to post
Share on other sites

I don't want to argue semantics with people, I just want to know how to optimize my code. 

 

He was not arguing semantics, he was informing you that you didn't really do what was suggested to you.

 

Doing what has been suggested, breaking down specific tasks into separate methods, you should be able to end up with a main method that looks similar to rip-offs main.  This will help with readability and maintainability and will give you specific, contained areas of code to work with to optimize.  

1

Share this post


Link to post
Share on other sites

Hi 

 

 

Create classes,

Call only the processes you need at a function not all.

But mostly rip-off gave you very important anwsers.

 

 

HyperV

0

Share this post


Link to post
Share on other sites

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. 

I would still use descriptive names and make it a habit.  In 2-3 years when you revisit this code, (even if you think you never will, trust me you will) you will be able to quickly figure out what you were doing because you won't have to guess what variable names were supposed to represent.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0