1:import java.util.Random;
2:import java.util.Scanner;
3:public class BattleShip {
4:	public static void main(String[] args) {
5:		
6:		int[][] ships = { { -1, -1, -1, -1, -1 }, { -1, -1, -1, -1 }, { -1, -1, -1 },
7:				{ -1, -1, -1 }, { -1, -1 } };
8:		// this may be kind of a redundant way of doing things, but for now... so be it:
9:		int[][] ships_sunk = { { -1, -1, -1, -1, -1 }, { -1, -1, -1, -1 }, { -1, -1, -1 },
10:				{ -1, -1, -1 }, { -1, -1 } };
11:		
12:		int[][] compShips = { { -1, -1, -1, -1, -1 }, { -1, -1, -1, -1 }, { -1, -1, -1 },
13:				{ -1, -1, -1 }, { -1, -1 } };
14:		/*
15:		 * Aircraft carrier = 5 battleship = 4 destroyer = 3 submarine = 3
16:		 * patrol boat = 2
17:		 */
18:		String[] shipNames = { "Aircraft Carrier", "Battleship", "Destroyer",
19:				"Submarine", "Patrol Boat" };
20:		String[] directions = { "left", "right", "up", "down" };
21:		// make a grid 10 by 10 (2 dimensional array)
22:		Scanner input = new Scanner(System.in);
23:		char[][] playerBoard = new char[10][10];
24:		char[][] compBoard = new char[10][10];
25:		char[][] compBoardView = new char[10][10];
26:		for (int i = 0; i < 10; i++) {
27:			for (int j = 0; j < 10; j++) {
28:				playerBoard[i][j] = ' ';
29:				compBoard[i][j] = ' ';
30:				compBoardView[i][j] = ' ';
31:			}
32:		}
33:		
34:		// computer's board: - is a miss, * is a hit,
35:		// player's board: - is a miss, * is a hit, o is an un-hit ship
36:		
37:		// place computer ships: (One day when I'm smarter, I'll use some kind of probability theory or something... but for now...)
38:				for (int i = 0; i < shipNames.length;) {
39:					
40:					Random row = new Random();
41:					int rowInt = row.nextInt(9);
42:					int colInt = row.nextInt(9);
43:					int dir = row.nextInt(3);
44:					
45:					if (compBoard[rowInt][colInt] != ' ') {
46:							continue;
47:						} 
48:					
49:					
50:					// right
51:					if (dir == 0) {
52:						if (colInt + compShips[i].length <= 10) {
53:							int count = 0;
54:							for (int k = 0; k < compShips[i].length; k++) {
55:								if (compBoard[rowInt][colInt + k] == ' ') {
56:									count++;
57:								}
58:							}
59:							if (count == compShips[i].length) {
60:								// if everything OK, add ship to array
61:								for (int j = colInt; j < compShips[i].length + colInt; j++) {
62:									compBoard[rowInt][j] = 'o';
63:									
64:									}
65:								// put the ships in ships array	
66:								for(int s = 0; s < compShips[i].length; s++){
67:									compShips[i][s] = (rowInt * 10) + colInt + s;
68:								}
69:							} else {
70:								continue;
71:							}
72:						} else {
73:							continue;
74:						}
75:					}// end of direction right
76:					
77:					// left
78:					if (dir == 1) {
79:						if (colInt - compShips[i].length >= -1) {
80:							int count = 0;
81:							for (int k = 0; k < compShips[i].length; k++) {
82:								if (compBoard[rowInt][colInt - k] == ' ') {
83:									count++;
84:								}
85:							}
86:							if (count == compShips[i].length) {
87:								// if everything OK, add ship to array
88:								for (int j = colInt; j > colInt - compShips[i].length; j--) {
89:									compBoard[rowInt][j] = 'o';
90:								}
91:								// put the ships in ships array	
92:								for(int s = 0; s < compShips[i].length; s++){
93:									compShips[i][s] = (rowInt * 10) + colInt - s;
94:								}
95:							} else {
96:								continue;
97:							}
98:						} else {
99:							continue;
100:						}
101:					}// end of direction left
102:					// up
103:					if (dir == 2) {
104:						if (rowInt - compShips[i].length >= -1) {
105:							int count = 0;
106:							for (int k = 0; k < compShips[i].length; k++) {
107:								if (compBoard[rowInt -k][colInt] == ' ') {
108:									count++;
109:								}
110:							}
111:							if (count == compShips[i].length) {
112:								// if everything OK, add ship to array
113:								for (int j = rowInt; j > rowInt - compShips[i].length; j--) {
114:									compBoard[j][colInt] = 'o';
115:								}
116:								// put the ships in ships array	
117:								for(int s = 0; s < compShips[i].length; s++){
118:									compShips[i][s] = ((rowInt * 10) - (s * 10)) + colInt;
119:								}
120:							} else {
121:								continue;
122:							}
123:						} else {
124:							continue;
125:						}
126:					}// end of direction up
127:					
128:					// down
129:					if (dir == 3) {
130:						if (rowInt + compShips[i].length <= 10) {
131:							int count = 0;
132:							for (int k = 0; k < compShips[i].length; k++) {
133:								if (compBoard[rowInt + k][colInt] == ' ') {
134:									count++;
135:								}
136:							}
137:							if (count == compShips[i].length) {
138:								// if everything OK, add ship to array
139:								for (int j = rowInt; j < compShips[i].length + rowInt; j++) {
140:									compBoard[j][colInt] = 'o';
141:								}
142:								// put the ships in ships array	
143:								for(int s = 0; s < compShips[i].length; s++){
144:									compShips[i][s] = ((rowInt * 10) + (s * 10)) + colInt;
145:								}
146:							} else {
147:								continue;
148:							}
149:						} else {
150:							continue;
151:						}
152:					}// end of direction down
153:					//printIntArr(compShips);
154:					i++;
155:				} // end of placing computer ships
156:		// ask player to place ships
157:		printBoard(playerBoard);
158:		for (int i = 0; i < shipNames.length;) {
159:			System.out.println("To place your " + shipNames[i] + ", enter a Row Letter followed by a Column Number:");
160:			String coordinate = input.next();
161:			char row = coordinate.charAt(0);
162:			char col = coordinate.charAt(1);
163:			// check validity of row and col
164:			// col has to be between a and j or A and J
165:			// row has to be between 0-9
166:			
167:			try {
168:				if (playerBoard[Character.getNumericValue(row) - 10][Character.getNumericValue(col)] != ' ') {
169:					System.out.println("That spot's already taken!");
170:					continue;
171:				} else if (((row >= 'A' && row <= 'J') || (row >= 'a' && row <= 'j'))
172:						&& (coordinate.length() == 2)) {
173:					System.out.println("Enter your " + shipNames[i] + "'s direction (left, right, up, down):");
174:				} else {
175:					System.out.println("Enter a valid coordinate: Row Letter followed by a Column Number");
176:					continue;
177:				}
178:			} catch (Exception e) {
179:				System.out.println("Enter a valid coordinate: Row Letter followed by a Column Number");
180:				continue;
181:			}
182:			
183:			String direction = input.next();	
184:			// check if direction exists, and remember which one
185:			boolean go = false;
186:			while(go == false){
187:				if (search(directions, direction) == true) {
188:						// System.out.println("That's a valid direction");
189:						go = true;
190:					} else {
191:						System.out.println("Enter a valid direction: left, right, up, down");
192:						direction = input.next();
193:						//continue;
194:					}
195:			}
196:			int rowInt = Character.getNumericValue(row) - 10; // -10 to get 0
197:			int colInt = Character.getNumericValue(col);
198:			// if direction.equals("right"): check if in bounds column + 5 has
199:			// to be less than or equal to 10,
200:			// and if there are other ships there
201:			if (direction.equals("right")) {
202:				if (colInt + ships[i].length <= 10) {
203:					int count = 0;
204:					for (int k = 0; k < ships[i].length; k++) {
205:						if (playerBoard[rowInt][colInt + k] == ' ') {
206:							count++;
207:						}
208:					}
209:					if (count == ships[i].length) {
210:						// if everything OK, add ship to array
211:						for (int j = colInt; j < ships[i].length + colInt; j++) {
212:							playerBoard[rowInt][j] = 'o';
213:							
214:							
215:							}
216:						// put the ships in ships array	
217:						for(int s = 0; s < ships[i].length; s++){
218:							ships[i][s] = (rowInt * 10) + colInt + s;
219:							// and also in this one to use later for checking sunk ship coordinates.
220:							ships_sunk[i][s] = (rowInt * 10) + colInt + s;
221:						}
222:					} else {
223:						System.out.println("You can't put one ship on top of another!, try again:");
224:						continue;
225:					}
226:				} else {
227:					System.out.println("Off the Board!, try again:");
228:					continue;
229:				}
230:			}// end of direction right
231:			if (direction.equals("left")) {
232:				if (colInt - ships[i].length >= -1) {
233:					int count = 0;
234:					for (int k = 0; k < ships[i].length; k++) {
235:						if (playerBoard[rowInt][colInt - k] == ' ') {
236:							count++;
237:						}
238:					}
239:					if (count == ships[i].length) {
240:						// if everything OK, add ship to array
241:						for (int j = colInt; j > colInt - ships[i].length; j--) {
242:							playerBoard[rowInt][j] = 'o';
243:						}
244:						// put the ships in ships array	
245:						for(int s = 0; s < ships[i].length; s++){
246:							ships[i][s] = (rowInt * 10) + colInt - s;
247:							// and also in this one to use later for checking sunk ship coordinates.
248:							ships_sunk[i][s] = (rowInt * 10) + colInt - s;
249:						}
250:					} else {
251:						System.out.println("You can't put one ship on top of another!, try again:");
252:						continue;
253:					}
254:				} else {
255:					System.out.println("Off the Board!, try again:");
256:					continue;
257:				}
258:			}// end of direction left
259:			
260:			if (direction.equals("up")) {
261:				if (rowInt - ships[i].length >= -1) {
262:					int count = 0;
263:					for (int k = 0; k < ships[i].length; k++) {
264:						if (playerBoard[rowInt -k][colInt] == ' ') {
265:							count++;
266:						}
267:					}
268:					if (count == ships[i].length) {
269:						// if everything OK, add ship to array
270:						for (int j = rowInt; j > rowInt - ships[i].length; j--) {
271:							playerBoard[j][colInt] = 'o';
272:						}
273:						// put the ships in ships array	
274:						for(int s = 0; s < ships[i].length; s++){
275:							ships[i][s] = ((rowInt * 10) - (s * 10)) + colInt;
276:							// and also in this one to use later for checking sunk ship coordinates.
277:							ships_sunk[i][s] = ((rowInt * 10) - (s * 10)) + colInt;
278:						}
279:					} else {
280:						System.out.println("You can't put one ship on top of another!, try again:");
281:						continue;
282:					}
283:				} else {
284:					System.out.println("Off the Board!, try again:");
285:					continue;
286:				}
287:			}// end of direction up
288:			
289:			// down
290:			if (direction.equals("down")) {
291:				if (rowInt + ships[i].length <= 10) {
292:					int count = 0;
293:					for (int k = 0; k < ships[i].length; k++) {
294:						if (playerBoard[rowInt + k][colInt] == ' ') {
295:							count++;
296:						}
297:					}
298:					if (count == ships[i].length) {
299:						// if everything OK, add ship to array
300:						for (int j = rowInt; j < ships[i].length + rowInt; j++) {
301:							playerBoard[j][colInt] = 'o';
302:						}
303:						// put the ships in ships array	
304:						for(int s = 0; s < ships[i].length; s++){
305:							ships[i][s] = ((rowInt * 10) + (s * 10)) + colInt;
306:							// and also in this one to use later for checking sunk ship coordinates.
307:							ships_sunk[i][s] = ((rowInt * 10) + (s * 10)) + colInt;
308:						}
309:					} else {
310:						System.out.println("You can't put one ship on top of another!, try again:");
311:						continue;
312:					}
313:				} else {
314:					System.out.println("Off the Board!, try again:");
315:					continue;
316:				}
317:			}// end of direction down
318:			
319:			printBoard(playerBoard);
320:			//printIntArr(ships);
321:			i++;
322:		} // end of placing player ships
323:		
324:		//printIntArr(compShips);
325:		
326:		System.out.println("All your ships are in place! Now let's start shootin'!");
327:		// TODO remove:
328:		// printIntArr(ships);
329:		
330:		boolean win = false;
331:		int player = 1; // 1 is human, 0 is computer
332:		int[] hits = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; // 
333:		int hitsIndex = -1;
334:		
335:		// game loop
336:		gameLoop:
337:		while(win == false){
338:			
339:		if(player == 1){
340:			System.out.println("++++++++++++++++++++++++++++++++++++++++++++++ \n Computer Board:");
341:			// TODO change to printBoard(compBoardView);
342:			// printBoard(compBoard);
343:			printBoard(compBoardView);
344:			 System.out.println("Enter a coordinate on the computer's board (ex: A0):");
345:			 String coordinate = input.next();
346:				char row = coordinate.charAt(0);
347:				char col = coordinate.charAt(1);
348:				
349:				int rowInt = Character.getNumericValue(row) - 10; // -10 to get 0
350:				int colInt = Character.getNumericValue(col);
351:				
352:				
353:				try {
354:					
355:					if (((row >= 'A' || row <= 'J') && (row >= 'a' || row <= 'j')) && coordinate.length() == 2) {
356:						   
357:						if (compBoard[rowInt][colInt] == 'o') {
358:							//System.out.println("You hit a ship at index: "+ hitShipRow+ ", box: "+ hitShipBox);
359:							System.out.println("Yay! - You got a hit!");
360:							
361:						//  store in the computer Boards arrays
362:							compBoard[rowInt][colInt] = '*';
363:							compBoardView[rowInt][colInt] = '*';
364:							
365:							
366:							// find the location of the hit ship in the ships array
367:							int hitShipRow = whichShip(compShips, rowInt, colInt); // returns index of hit ship in ships array
368:							int hitShipBox = whichShipCol(compShips, rowInt, colInt); // returns index of hit ship box in ship
369:							
370:							compShips[hitShipRow][hitShipBox] = -1;
371:							
372:							
373:							// check if hit ship was sunk (all boxes in ship equal -1)
374:							if(isSunk(compShips, hitShipRow) == true){
375:								System.out.println("And... you sunk the "+shipNames[hitShipRow] + "!");
376:							}
377:							
378:							// check if ALL ships were sunk. 
379:							if(allSunk(compShips) == true){
380:								System.out.println("++++++++++++++++++++++++++++++++++++++++++++++ \n Computer Board:");
381:								printBoard(compBoardView);
382:								System.out.println("You win!");
383:								break;
384:							}
385:							
386:						} else if(compBoard[rowInt][colInt] == '-' || compBoard[rowInt][colInt] == '*'){
387:							System.out.println("You tried that before, have another go...");
388:							continue;
389:						}else if(compBoard[rowInt][colInt] == ' '){
390:							
391:							//  store in array
392:							compBoard[rowInt][colInt] = '-';
393:							compBoardView[rowInt][colInt] = '-';
394:							System.out.println("Oh well, you missed...");
395:						}
396:					} else{
397:						 System.out.println("Enter a valid coordinate: Row Letter followed by a Column Number");
398:						 continue;
399:					}
400:				} catch (Exception e) { // if somebody enters number followed by letter
401:					System.out.println("Enter a valid coordinate: Row Letter followed by a Column Number");
402:					continue;
403:				}
404:				
405:				
406:			}// end player 1(human) actions
407:		// computer actions (AI)
408:		
409:		
410:		if(player == 0){
411:			
412:			int rowInt = 0; 
413:			int colInt = 0;
414:			
415:			int row = 0;
416:			int col = 0;
417:			
418:			if(isEmpty(hits, -1) == true){
419:				// if there are no partially hit ships
420:				// generate random hit 0-9, 0-9
421:				
422:				//TODO smarter board search instead of random, based on binary search principle maybe? Or probability? Or something even smarter I don't know about yet :)
423:				
424:				Random hit = new Random();
425:				int rowRan = hit.nextInt(9);
426:				int colRan = hit.nextInt(9);
427:				
428:				while(playerBoard[rowRan][colRan] == '-'  || playerBoard[rowRan][colRan] == '*'){
429:					
430:					rowRan = hit.nextInt(9);
431:					colRan = hit.nextInt(9);
432:				}
433:				rowInt = rowRan;
434:				colInt = colRan;
435:				
436:				
437:			}else{
438:				// find a partially hit ship 
439:				// by finding coordinates in the hits array: 
440:				row = hits[hitsIndex]/10;
441:				col = hits[hitsIndex]%10;
442:				int incrementRow = row;
443:				int incrementCol = col;
444:				
445:				if(isSingle(hits, -1) == true){ // ship was only hit once if hits contains only 1 value other than -1 
446:					// try all 4 directions
447:					// TODO randomize order of direction - use a switch instead of if/else ? 
448:					
449:					// check if potential hit is in bounds and is a space or o
450:					if((row - 1 >= 0) && (playerBoard[row - 1][col] == ' ' || playerBoard[row - 1][col] == 'o' )){  // try going up
451:						rowInt = row - 1;
452:						colInt = col;
453:					}else if((row + 1 <= 9) && (playerBoard[row + 1][col] == ' ' || playerBoard[row + 1][col] == 'o')){ // try going down
454:						rowInt = row + 1;
455:						colInt = col;
456:					}else if((col + 1 <= 9) && (playerBoard[row][col + 1] == ' ' || playerBoard[row][col + 1] == 'o')){ // try going right
457:						rowInt = row;
458:						colInt = col + 1;
459:					}else if((col - 1 >= 0) && (playerBoard[row][col - 1] == ' ' || playerBoard[row][col - 1] == 'o')){ // go left
460:						rowInt = row;
461:						colInt = col - 1;
462:					}
463:					
464:					
465:				}else if(isVertical(hits) == true){ // ship is vertical - check hits array
466:					// try up else down
467:					// TODO randomize order of direction (up or down first) - or maybe that's not a good idea...
468:										
469:					if((row - 1 >= 0) && (playerBoard[row - 1][col] == ' ' || playerBoard[row - 1][col] == 'o')){  // try going up
470:						rowInt = row - 1;
471:						colInt = col;
472:					}else if((row + 1 <= 9) && (playerBoard[row + 1][col] == ' ' || playerBoard[row + 1][col] == 'o')){ // go down
473:						rowInt = row + 1;
474:						colInt = col;
475:					}else if((col + 1 <= 9) && (playerBoard[row][col + 1] == ' ' || playerBoard[row][col + 1] == 'o')){ // try going right
476:						rowInt = row;
477:						colInt = col + 1;
478:					}else if((col - 1 <= 0) && (playerBoard[row][col - 1] == ' ' || playerBoard[row][col - 1] == 'o')){ // go left
479:						rowInt = row;
480:						colInt = col - 1;
481:					}
482:					
483:					
484:				}else{ // ship is horizontal
485:					// try left else right
486:					// TODO randomize order of direction (left or right first)
487:					
488:					if((col + 1 <= 9) && (playerBoard[row][col + 1] == ' ' || playerBoard[row][col + 1] == 'o')){ // try going right
489:						rowInt = row;
490:						colInt = col + 1;
491:					}else if ((col - 1 >= 0) && (playerBoard[row][col - 1] == ' ' || playerBoard[row][col - 1] == 'o')){ // go left
492:						rowInt = row;
493:						colInt = col - 1;
494:					}else if((row - 1 >= 0) && (playerBoard[row - 1][col] == ' ' || playerBoard[row - 1][col] == 'o')){  // try going up
495:						rowInt = row - 1;
496:						colInt = col;
497:					}else if((row + 1 <= 9) && (playerBoard[row + 1][col] == ' ' || playerBoard[row + 1][col] == 'o')){ // go down
498:						rowInt = row + 1;
499:						colInt = col;
500:					}
501:					}
502:				
503:				
504:			}
505:					   
506:					if (playerBoard[rowInt][colInt] == 'o') {
507:						//System.out.println("You hit a ship at index: "+ hitShipRow+ ", box: "+ hitShipBox);
508:						System.out.println("Computer got a hit!" + rowInt+":"+colInt);
509:						
510:						//  store in the playerBoard array
511:						playerBoard[rowInt][colInt] = '*';
512:						hits[hitsIndex + 1] = rowInt * 10 + colInt;
513:						
514:						// TODO remove
515:						// printNumArr(hits);
516:						
517:						hitsIndex++;
518:						
519:						
520:						// find the location of the hit ship in the ships array
521:						int hitShipRow = whichShip(ships, rowInt, colInt); // returns index of hit ship in ships array
522:						int hitShipBox = whichShipCol(ships, rowInt, colInt); // returns index of hit ship box in the hit ship
523:						
524:						// set it to -1 for later use, to check if ship is sunk
525:						ships[hitShipRow][hitShipBox] = -1; 
526:						
527:						
528:						// check if hit ship was sunk (all boxes in ship equal -1)
529:						if(isSunk(ships, hitShipRow) == true){
530:							System.out.println("And... computer sunk your "+shipNames[hitShipRow] + "!");
531:							
532:							// find the coordinates for ships[hitShipRow] and remove them from hits
533:							for(int i = 0; i < ships_sunk[hitShipRow].length; i++){
534:								for(int j = 0; j < hits.length; j++){
535:									if(ships_sunk[hitShipRow][i] == hits[j]){
536:										hits[j] = -1;
537:									}
538:								}
539:									
540:							}
541:							
542:							// 
543:							// shift all coordinates to the left, and set the hitsIndex to last actual coordinate
544:							sortHits(hits);
545:							
546:							// we'll be left with just hits but not sunk ships in the hits array.
547:							hitsIndex = lastIndex(hits);
548:							
549:						}
550:						
551:						// check if ALL ships were sunk. 
552:						if(allSunk(ships) == true){
553:							System.out.println("++++++++++++++++++++++++++++++++++++++++++++++ \n Your Board:");
554:							printBoard(playerBoard);
555:							System.out.println("Oh well - Computer won...");
556:							return;
557:						}
558:						
559:					} else if(playerBoard[rowInt][colInt] == '-' || playerBoard[rowInt][colInt] == '*'){
560:						hitsIndex = 0; // TODO let's try hitting in a different direction - (needs work)
561:						continue gameLoop;
562:					}else if(playerBoard[rowInt][colInt] == ' '){
563:						
564:						//  store the miss in playerBoard array
565:						playerBoard[rowInt][colInt] = '-';
566:						System.out.println("Computer missed - Yay for you!");
567:						// TODO remove
568:						 System.out.println("Computer missed at: " + rowInt+":"+colInt);
569:						// printNumArr(hits);
570:					}
571:				
572:			
573:			System.out.println("++++++++++++++++++++++++++++++++++++++++++++++ \n Your Board:");
574:			printBoard(playerBoard);
575:			
576:		}
577:		
578:		// toggle player...
579:		if (player == 1) {
580:			player = 0;
581:		} else {
582:			player = 1;
583:		}
584:	}// end game loop
585:		
586:	}
587:	public static void printBoard(char[][] array) {
588:		System.out.print("  ");
589:		for (int col = 0; col < array.length; col++) {
590:			System.out.printf("%-1s %-2d", " ", col);
591:		}
592:		System.out.println("\n   -----------------------------------------");
593:		for (int j = 0; j < array.length; j++) {
594:			System.out.printf("%-2c", 'A' + j);
595:			for (int i = 0; i < array.length; i++) {
596:				System.out.printf("%-1s %-2c", "|", array[j][i]);
597:			}
598:			System.out.println("|\n   -----------------------------------------");
599:		}
600:	}
601:	public static boolean search(String[] array, String str) {
602:		boolean found = false;
603:		for (int i = 0; i < array.length; i++) {
604:			if (array[i].equals(str)) {
605:				found = true;
606:				break; // no need to keep searching if str is found
607:			}
608:		}
609:		return found;
610:	}
611:	public static int whichShip(int[][] array, int row, int col){
612:	 //find which ship got hit (find coordinate in ships array, and return the index)
613:		int result = 0;
614:		for (int i = 0; i < array.length; i++){
615:			for (int j = 0; j < array[i].length; j++){
616:				
617:				if(array[i][j] == (row * 10) + col){
618:					result = i;
619:					break;
620:				}
621:				
622:			 }
623:		}
624:		return result;
625:	}
626:	public static int whichShipCol(int[][] array, int row, int col){
627:		 //find where ship got hit (find coordinate in ships array, and return the col index)
628:			int result = 0;
629:			for (int i = 0; i < array.length; i++){
630:				for (int j = 0; j < array[i].length; j++){
631:					
632:					if(array[i][j] == (row * 10) + col){
633:						result = j;
634:						break;
635:					}
636:					
637:				 }
638:			}
639:			return result;
640:		}
641:	
642:	public static boolean isSunk(int[][] array, int hitShip){
643:		boolean sunk = false;
644:		int count = 0;
645:			for(int i = 0; i < array[hitShip].length; i++){
646:				if(array[hitShip][i] == -1){
647:					count++;
648:				}
649:			}
650:			if(count == array[hitShip].length){
651:				sunk = true;
652:			}
653:		return sunk;
654:		
655:	}
656:	public static boolean allSunk(int[][] array){
657:		boolean sunk = false;
658:		int count = 0; // where value == -1
659:		int countInners = 0; // the length of an inner array
660:		
661:		for (int i = 0; i < array.length; i++) {
662:			for (int j = 0; j < array[i].length; j++) {
663:				if(array[i][j] == -1){
664:					count++;
665:				}
666:			}
667:			countInners += 	array[i].length;
668:		}
669:		if(count == countInners){
670:			sunk = true;
671:		}
672:		return sunk;
673:	}
674:	
675:	
676:	// sort highest to lowest
677:	public static void sortHits(int[] array){ 
678:		int temp = 0;
679:		for(int i = 0; i < (array.length-1); ++i){
680:		   for(int j=0; j < (array.length-1); ++j){
681:		      if(array[j] < array[j+1]){
682:		             temp = array[j];
683:		             array[j] = array[j+1];
684:		             array[j+1] = temp;
685:		          }
686:		      }
687:		    }
688:		}
689:	
690:	// sort lowest to highest
691:/*		public static void sortHits(int[] array){ 
692:			int temp = 0;
693:			for(int i = 0; i < (array.length-1); ++i){
694:			   for(int j=0; j < (array.length-1); ++j){
695:			      if(array[j] > array[j+1]){
696:			             temp = array[j];
697:			             array[j] = array[j+1];
698:			             array[j+1] = temp;
699:			          }
700:			      }
701:			    }
702:			}*/
703:	
704:	
705:	public static boolean isVertical(int[] array){
706:		boolean vertical = false;
707:			if(array[0] % 10 == array[1] % 10){
708:				vertical = true;
709:			}
710:			
711:		return vertical;
712:	}
713:	
714:	// get lastIndex by counting the number of values in hits that are not -1
715:	// lastIndex(hits);
716:	public static int lastIndex(int[] array){
717:		
718:		int count = 0;
719:		for(int i = 0; i < array.length; i++){
720:			if(array[i] != -1){
721:				count++;
722:			}
723:		}
724:		return count - 1;
725:		
726:		
727:	}
728:	
729:	
730:	public static void printArr(String[] array) {
731:		for (String x : array) {
732:			System.out.print(x + ", ");
733:		}
734:	}
735:	public static void printNumArr(int[] array) {
736:		for (int x : array) {
737:			System.out.print(x + ", ");
738:		}
739:	}
740:	public static void printIntArr(int[][] array) {
741:		System.out.println("-------------");
742:		for (int j = 0; j < array.length; j++) {
743:			for (int i = 0; i < array[j].length; i++) {
744:				System.out.printf("%-1s %-2d", "|", array[j][i]);
745:				
746:			}
747:			System.out.println("|\n-------------");
748:		}
749:	}
750:	
751:	public static boolean isSingle(int[] array, int value){
752:		boolean single = false;
753:		int count = 0;
754:		for(int i = 0; i < array.length; i++){
755:			if(array[i] != value){
756:				count++;
757:			}
758:		}
759:		if(count == 1){
760:			single = true;
761:		}
762:		
763:		return single;
764:	}
765:	public static boolean isEmpty(int[] array, int value){
766:		boolean empty = false;
767:		int count = 0;
768:		for(int i = 0; i < array.length; i++){
769:			if(array[i] != value){
770:				count++;
771:			}
772:		}
773:		if(count == 0){
774:			empty = true;
775:		}
776:		
777:		return empty;
778:	}
779:}