Advertisement
Guest User

Hi Mr. C, this is my battleship

a guest
Feb 12th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.27 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.applet.*;
  3. import java.net.*;
  4. import java.io.*;
  5. public class battleShip{
  6. private static String[][] fakeCompGrid = {
  7. {"║ ", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", " 10", "║ Name of Ship ║", " Status ║"},
  8. {"║A ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
  9. {"║B ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Aircraft Carrier ║", " Alive ║"},
  10. {"║C ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
  11. {"║D ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Battleship ║", " Alive ║"},
  12. {"║E ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
  13. {"║F ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Destroyer ║", " Alive ║"},
  14. {"║G ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
  15. {"║H ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Submarine ║", " Alive ║"},
  16. {"║I ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
  17. {"║J ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Patrol Boat ║", " Alive ║"} };
  18. private static String[][] realCompGrid = {
  19. {"###", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", " 10", "# Name of Ship |", " Status #"},
  20. {"#A ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " #------------------|", "--------#"},
  21. {"#B ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " # Aircraft Carrier |", " Alive #"},
  22. {"#C ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " #------------------|", "--------#"},
  23. {"#D ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " # Battleship |", " Alive #"},
  24. {"#E ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " #------------------|", "--------#"},
  25. {"#F ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " # Destroyer |", " Alive #"},
  26. {"#G ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " #------------------|", "--------#"},
  27. {"#H ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " # Submarine |", " Alive #"},
  28. {"#I ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " #------------------|", "--------#"},
  29. {"#J ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " # Patrol Boat |", " Alive #"} };
  30. private static String[][] humanGrid = {
  31. {"║ ", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", " 10", "║ Name of Ship ║", " Status ║"},
  32. {"║A ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
  33. {"║B ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Aircraft Carrier ║", " Alive ║"},
  34. {"║C ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
  35. {"║D ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Battleship ║", " Alive ║"},
  36. {"║E ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
  37. {"║F ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Destroyer ║", " Alive ║"},
  38. {"║G ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
  39. {"║H ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Submarine ║", " Alive ║"},
  40. {"║I ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ╠══════════════════╬", "════════╣"},
  41. {"║J ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ║ Patrol Boat ║", " Alive ║"} };
  42. private static final int GAME_OVER_LIMIT = 17;
  43. private static final int HUMAN = 1;
  44. private static int currentPlayer = 1;
  45. private static final KeyboardReader INPUT;
  46. private static int humanTotalHitsTaken;
  47. private static int compTotalHitsTaken;
  48. private static String name;
  49. private static int row, column;
  50. private static ArrayList<String> coordsY;
  51. private static ArrayList<Integer> coordsX;
  52. private static ArrayList<String> coordsStringX;
  53. private static AudioClip explosion, splash;
  54. private static int airShipComputer = 5;
  55. private static int battleshipComputer = 4;
  56. private static int destroyerComputer = 3;
  57. private static int submarineComputer = 3;
  58. private static int patrolBoatComputer = 2;
  59. private static int airshipHuman = 5;
  60. private static int battleshipHuman = 4;
  61. private static int destroyerHuman = 3;
  62. private static int submarineHuman = 3;
  63. private static int patrolBoatHuman = 2;
  64. private static String logComputer = "";
  65. private static String logHuman = "";
  66. static{
  67. INPUT = new KeyboardReader();
  68. coordsY = new ArrayList<String>();
  69. coordsY.add("A");
  70. coordsY.add("B");
  71. coordsY.add("C");
  72. coordsY.add("D");
  73. coordsY.add("E");
  74. coordsY.add("F");
  75. coordsY.add("G");
  76. coordsY.add("H");
  77. coordsY.add("I");
  78. coordsY.add("J");
  79. coordsStringX = new ArrayList<String>();
  80. coordsStringX.add("1");
  81. coordsStringX.add("2");
  82. coordsStringX.add("3");
  83. coordsStringX.add("4");
  84. coordsStringX.add("5");
  85. coordsStringX.add("6");
  86. coordsStringX.add("7");
  87. coordsStringX.add("8");
  88. coordsStringX.add("9");
  89. coordsStringX.add("10");
  90. for (int i = 1; i < 10; i++){
  91. coordsX = new ArrayList<Integer>();
  92. coordsX.add(i);
  93. }
  94. explosion = null;
  95. splash = null;
  96. try
  97. {
  98. explosion = Applet.newAudioClip(new URL("file:" + new File(".").getCanonicalPath()
  99. + "/" + "explosion.wav"));
  100. splash = Applet.newAudioClip(new URL("file:" + new File(".").getCanonicalPath() +
  101. "/" + "splash.wav"));
  102. } catch (Exception e)
  103. {
  104. System.out.println(e.toString());
  105. }
  106. }
  107. public static void main (String [] args) {
  108. welcomeBanner();
  109. initializeGame();
  110. while (humanTotalHitsTaken < GAME_OVER_LIMIT && compTotalHitsTaken < GAME_OVER_LIMIT)
  111. {
  112. displayGame();
  113. if (currentPlayer == HUMAN)
  114. humanPlayerTakesTurn();
  115. else
  116. computerPlayerTakesTurn();
  117. }
  118. congratulateWinner();
  119. }
  120. public static void welcomeBanner(){
  121. System.out.println("██████╗ █████╗ ████████╗████████╗██╗ ███████╗███████╗██╗ ██╗██╗██████╗ ");
  122. System.out.println("██╔══██╗██╔══██╗╚══██╔══╝╚══██╔══╝██║ ██╔════╝██╔════╝██║ ██║██║██╔══██╗");
  123. System.out.println("██████╔╝███████║ ██║ ██║ ██║ █████╗ ███████╗███████║██║██████╔╝");
  124. System.out.println("██╔══██╗██╔══██║ ██║ ██║ ██║ ██╔══╝ ╚════██║██╔══██║██║██╔═══╝ ");
  125. System.out.println("██████╔╝██║ ██║ ██║ ██║ ███████╗███████╗███████║██║ ██║██║██║ ");
  126. System.out.println("╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝╚═╝╚═╝ ");
  127. System.out.println(" |__");
  128. System.out.println(" |\\/");
  129. System.out.println(" ---");
  130. System.out.println(" / | [");
  131. System.out.println(" ! | |||");
  132. System.out.println(" _/| _/|-++'");
  133. System.out.println(" + +--| |--|--|_ |-");
  134. System.out.println(" { /|__| |/\\__| |--- |||__/");
  135. System.out.println(" +---------------___[]-_===_.'____ /\\");
  136. System.out.println(" ____`-' ||___-{]_| _[}- | |_[___\\==-- \\/ _");
  137. System.out.println(" __..._____-===/___]_|__|_____________________________[___\\==--____,------' .7");
  138. System.out.println("|═*═*═*═*═*═╗ BB-61/");
  139. System.out.println(" \\═════════╝_______________________________________________________________|");
  140. }
  141. public static void initializeGame(){
  142. placeShips(humanGrid);
  143. placeShips(realCompGrid);
  144. int choiceValue = 1;
  145. int flip = (int) (Math.random()*2)+1;
  146. for (name = INPUT.readString("\nEnter your name: "); name.length() > 49 || name.length() == 0;){
  147. name = INPUT.readString("Invalid. Re-enter your name:");
  148. }
  149. System.out.println("We will be flipping a coin to see who goes first!");
  150. while(true){
  151. String choice = INPUT.readString("Select 'Heads' or 'Tails' (non-case sensitive): ");
  152. choice.trim();
  153. if (choice.equalsIgnoreCase("heads")){
  154. choiceValue = 1;
  155. System.out.println("You chose heads!");
  156. break;
  157. }
  158. else if (choice.equalsIgnoreCase("tails")){
  159. choiceValue = 2;
  160. System.out.println("You chose tails!");
  161. break;
  162. }
  163. else
  164. System.out.println("That's not a valid input -_-");
  165. }
  166. if (choiceValue == flip){
  167. System.out.println("You won the coinflip!");
  168. currentPlayer = 1;
  169. }
  170. else{
  171. System.out.println("Yeah you didn't win the coinflip, not that you care.");
  172. currentPlayer = 2;
  173. }
  174. }
  175. public static void displayGame(){
  176. System.out.println("\f");
  177. System.out.println("╔═══════════════════════════════════════════════════╗");
  178. System.out.println("║ Computer.EXE ║");
  179. System.out.println("╚═══════════════════════════════════════════════════╝");
  180. System.out.println("╔═══════════════════════╦══════════════════╦════════╗");
  181. for (int i = 0; i <= 10; i++){
  182. for (int z = 0; z <= 12; z++)
  183. System.out.print(fakeCompGrid[i][z]);
  184. System.out.println();
  185. }
  186. System.out.println("╚═══════════════════════╩══════════════════╩════════╝");
  187. System.out.println("╔═══════════════════════════════════════════════════╗");
  188. int spaces = 49 - name.length();
  189. System.out.print("║");
  190. for (int i = 0; i < (spaces/2); i++)
  191. System.out.print(" ");
  192. System.out.print(name);
  193. for (int k = 0; k < (spaces/2); k++)
  194. System.out.print(" ");
  195. if (spaces%2 == 1)
  196. System.out.print(" ");
  197. System.out.print(" ║");
  198. System.out.print("\n");
  199. System.out.println("╚═══════════════════════════════════════════════════╝");
  200. System.out.println("╔═══════════════════════╦══════════════════╦════════╗");
  201. for (int i = 0; i <= 10; i++){
  202. for (int z = 0; z <= 12; z++)
  203. System.out.print(humanGrid[i][z]);
  204. System.out.println();
  205.  
  206. }
  207. System.out.println("╚═══════════════════════╩══════════════════╩════════╝");
  208. }
  209. public static void humanPlayerTakesTurn(){
  210. int repeatCount = 0;
  211. System.out.println("╔═════════╗");
  212. System.out.println("║Computer:║ "+logComputer);
  213. System.out.println("╚═════════╝");
  214. System.out.print("╔");
  215. for (int i = 0; i <= name.length();i++)
  216. System.out.print("═");
  217. System.out.print("╗");
  218. System.out.println("\n║"+name+":║ "+logHuman);
  219. System.out.print("╚");
  220. for (int i = 0; i <= name.length();i++)
  221. System.out.print("═");
  222. System.out.print("╝\n");
  223. while(true){
  224. boolean validInput = false;
  225. String coordY;
  226. int coordX = 0;
  227. int valueY = 0;
  228. if (repeatCount >= 1){
  229. System.out.println("Invalid Response");
  230. }
  231. repeatCount++;
  232. String input = INPUT.readString(name+"!Enter a coordinate (like B,4):");
  233. if (input.length() < 5 && input.length() > 2){
  234. coordY = input.substring(0, 1).toUpperCase();
  235. if (input.length() == 4){
  236. if (coordsStringX.indexOf(input.substring(2,3)) >=0 && coordsStringX.indexOf(input.substring(2,4)) <= 10){
  237. coordX = coordsStringX.indexOf(input.substring(2))+1;
  238. validInput = true;
  239. }
  240. }
  241. if (input.length() == 3){
  242. if (coordsStringX.indexOf(input.substring(2)) >=0)
  243. coordX = coordsStringX.indexOf(input.substring(2))+1;
  244. validInput = true;
  245. }
  246. if (coordsY.indexOf(coordY) >=0){
  247. valueY = coordsY.indexOf(coordY) + 1;
  248. validInput = true;
  249. }
  250. else{
  251. validInput = false;
  252. }
  253. if (validInput = true){
  254. if (realCompGrid[valueY][coordX].equals(" *")){
  255. System.out.println("You already attacked this ship!");
  256. repeatCount--;
  257. }
  258. else if (realCompGrid[valueY][coordX].equals(" X")){
  259. System.out.println("You have already attacked and missed this spot!");
  260. repeatCount--;
  261. }
  262. else if (realCompGrid[valueY][coordX].equalsIgnoreCase(" ")){
  263. fakeCompGrid[valueY][coordX] = " X";
  264. realCompGrid[valueY][coordX] = " X";
  265. logHuman ="You have missed!";
  266. splash.play();
  267. currentPlayer ++;
  268. INPUT.pause();
  269. break;
  270. }
  271. else if (!realCompGrid[valueY][coordX].equals(" ") && !realCompGrid[valueY][coordX].equals(" X") && !realCompGrid[valueY][coordX].equals(" *") && coordX != 0 && valueY != 0){
  272. if (realCompGrid[valueY][coordX].equals(" A")){
  273. logHuman = "You hit a ship!";
  274. airShipComputer --;
  275. if (airShipComputer == 0){
  276. fakeCompGrid[2][12] = " Dead ║";
  277. realCompGrid[2][12] = " Dead ║";
  278. logHuman = "You destroyed the enemy aircraft carrier!";
  279. }
  280. }
  281. else if (realCompGrid[valueY][coordX].equals(" B")){
  282. logHuman = "You hit a ship!";
  283. battleshipComputer --;
  284. if (battleshipComputer == 0){
  285. fakeCompGrid[4][12] = " Dead ║";
  286. realCompGrid[4][12] = " Dead ║";
  287. logHuman = "You destroyed the enemy battleship!";
  288. }
  289. }
  290. else if (realCompGrid[valueY][coordX].equals(" D")){
  291. destroyerComputer --;
  292. logHuman = "You hit a ship!";
  293. if (destroyerComputer == 0){
  294. fakeCompGrid[6][12] = " Dead ║";
  295. fakeCompGrid[6][12] = " Dead ║";
  296. logHuman = "You destroyed the enemy destroyer!";
  297. }
  298. }
  299. else if (realCompGrid[valueY][coordX].equals(" S")){
  300. submarineComputer--;
  301. logHuman = "You hit a ship!";
  302. if (submarineComputer == 0){
  303. fakeCompGrid[8][12] = " Dead ║";
  304. fakeCompGrid[8][12] = " Dead ║";
  305. logHuman = "You destroyed the enemy submarine!";
  306. }
  307. }
  308. else if (realCompGrid[valueY][coordX].equals(" P")){
  309. patrolBoatComputer--;
  310. logHuman = "You hit a ship!";
  311. if (patrolBoatComputer == 0){
  312. fakeCompGrid[10][12] = " Dead ║";
  313. fakeCompGrid[10][12] = " Dead ║";
  314. logHuman = "You destroyed the enemy patrol boat!";
  315. }
  316. }
  317. explosion.play();
  318. fakeCompGrid[valueY][coordX] = " *";
  319. realCompGrid[valueY][coordX] = " *";
  320. compTotalHitsTaken ++;
  321. currentPlayer ++;
  322. INPUT.pause();
  323. break;
  324. }
  325. }
  326. }
  327. }
  328. }
  329. public static void computerPlayerTakesTurn(){
  330. int compCoordX = (int) (Math.random()*10)+1;
  331. int compCoordY = (int) (Math.random()*10)+1;
  332. while (true){
  333. compCoordX = (int) (Math.random()*10)+1;
  334. compCoordY = (int) (Math.random()*10)+1;
  335. if (humanGrid[compCoordY][compCoordX].equals(" ")){
  336. currentPlayer--;
  337. humanGrid[compCoordY][compCoordX] = " X";
  338. logComputer = "Computer.EXE has missed";
  339. break;
  340. }
  341. else if (!humanGrid[compCoordY][compCoordX].equals(" ") && !humanGrid[compCoordY][compCoordX].equals(" X") && !humanGrid[compCoordY][compCoordX].equals(" *")){
  342. if (humanGrid[compCoordY][compCoordX].equals(" A")){
  343. logComputer = "Computer.EXE hit an aircraft carrier!";
  344. airshipHuman --;
  345.  
  346. if (airshipHuman == 0){
  347. logComputer = "Computer.EXE has destroyed your aircraft.";
  348. humanGrid[2][12] = " dead #";
  349. }
  350. }
  351. else if (humanGrid[compCoordY][compCoordX].equals(" B")){
  352. logComputer = "Computer.EXE hit a battleship!";
  353. battleshipHuman --;
  354. if (airshipHuman == 0){
  355. logComputer = "Computer.EXE has destroyed your battleship.";
  356. humanGrid[4][12] = " dead #";
  357. }
  358. }
  359. else if (humanGrid[compCoordY][compCoordX].equals(" D")){
  360. logComputer = "Computer.EXE hit a destroyer!";
  361. destroyerHuman --;
  362. if (airshipHuman == 0){
  363. logComputer = "Computer.EXE has destroyed your destroyer.";
  364. humanGrid[6][12] = " dead #";
  365. }
  366. }
  367. else if (humanGrid[compCoordY][compCoordX].equals(" S")){
  368. logComputer = "Computer.EXE hit a submarine!";
  369. submarineHuman --;
  370. if (airshipHuman == 0){
  371. logComputer = "Computer.EXE has destroyed your submarine.";
  372. humanGrid[8][12] = " dead #";
  373. }
  374. }
  375. else if (humanGrid[compCoordY][compCoordX].equals(" P")){
  376. logComputer = "Computer.EXE hit a patrol boat!";
  377. patrolBoatHuman --;
  378. if (airshipHuman == 0){
  379. logComputer = "Computer.EXE has destroyed your patrol boat.";
  380. humanGrid[10][12] = " dead #";
  381. }
  382. }
  383. humanGrid[compCoordY][compCoordX] = " *";
  384. humanTotalHitsTaken ++;
  385. currentPlayer --;
  386. break;
  387. }
  388. else if (humanGrid[compCoordY][compCoordX].equals(" *")){
  389. }
  390. }
  391. }
  392. public static void congratulateWinner(){
  393. System.out.println("╔═╗╔═╗╔╗╔╔═╗╦═╗╔═╗╔╦╗╦ ╦╦ ╔═╗╔╦╗╦╔═╗╔╗╔╔═╗║");
  394. System.out.println("║ ║ ║║║║║ ╦╠╦╝╠═╣ ║ ║ ║║ ╠═╣ ║ ║║ ║║║║╚═╗║");
  395. System.out.println("╚═╝╚═╝╝╚╝╚═╝╩╚═╩ ╩ ╩ ╚═╝╩═╝╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝o");
  396.  
  397. if (humanTotalHitsTaken > compTotalHitsTaken){
  398. System.out.println("Computer.EXE! You are the winner!");
  399.  
  400. }
  401. else if (humanTotalHitsTaken < compTotalHitsTaken){
  402. System.out.println(name+"! You are the winner!");
  403.  
  404. }
  405. }
  406. private static void placeShips (String[][] grid){
  407. int orientation = (int) (Math.random()*2)+1;
  408. int horizontal = 2;
  409. int vertical = 1;
  410. int occupiedSpace = 0;
  411. int size = 5;
  412. if (orientation == vertical && size == 5){
  413. row = (int) (Math.random()*5)+1;
  414. column = (int) (Math.random()*10)+1;
  415. for (int i = 0; i < size; i++)
  416. grid[row+i][column] = " A";
  417. size --;
  418. }
  419. else if (orientation == horizontal && size == 5){
  420. row = (int) (Math.random()*10)+1;
  421. column = (int) (Math.random()*5)+1;
  422. for (int i = 0; i < size; i++)
  423. grid[row][column+i] = " A";
  424. size --;
  425. }
  426. orientation = (int) (Math.random()*2)+1;
  427. if (orientation == vertical && size == 4){
  428. while (size == 4){
  429. occupiedSpace *= 0;
  430. row = (int) (Math.random()*6)+1;
  431. column = (int) (Math.random()*10)+1;
  432. String test = grid[row][column];
  433. for (int i = 0; i < size; i++){
  434. test = grid[row+i][column];
  435. if (!test.equals(" ")){
  436. occupiedSpace ++;
  437. }
  438. }
  439. if (occupiedSpace == 0){
  440. for (int k = 0; k < size; k++)
  441. grid[row+k][column] = " B";
  442. size --;
  443. }
  444. }
  445. }
  446. else if (orientation == horizontal && size == 4){
  447. while (size == 4){
  448. occupiedSpace *= 0;
  449. row = (int) (Math.random()*10)+1;
  450. column = (int) (Math.random()*6)+1;
  451. String test = grid[row][column];
  452. for (int i = 0; i < size; i++){
  453. test = grid[row][column+i];
  454. if (!test.equals(" ")){
  455. occupiedSpace ++;
  456. }
  457. }
  458. if (occupiedSpace == 0){
  459. for (int z = 0; z < size; z++)
  460. grid[row][column+z] = " B";
  461. size --;
  462. }
  463. }
  464. }
  465. orientation = (int) (Math.random()*2)+1;
  466. if (orientation == vertical && size == 3){
  467. while (size == 3){
  468. occupiedSpace *= 0;
  469. row = (int) (Math.random()*7)+1;
  470. column = (int) (Math.random()*10)+1;
  471. String test = grid[row][column];
  472. for (int i = 0; i < size; i++){
  473. test = grid[row+i][column];
  474. if (!test.equals(" ")){
  475. occupiedSpace ++;
  476. }
  477. }
  478. if (occupiedSpace == 0){
  479. for (int k = 0; k < size; k++)
  480. grid[row+k][column] = " D";
  481. break;
  482. }
  483. }
  484. }
  485. else if (orientation == horizontal && size == 3){
  486. while (size == 3){
  487. occupiedSpace *= 0;
  488. row = (int) (Math.random()*10)+1;
  489. column = (int) (Math.random()*7)+1;
  490. String test = grid[row][column];
  491. for (int i = 0; i < size; i++){
  492. test = grid[row][column+i];
  493. if (!test.equals(" ")){
  494. occupiedSpace ++;
  495. }
  496. }
  497. if (occupiedSpace == 0){
  498. for (int z = 0; z < size; z++)
  499. grid[row][column+z] = " D";
  500. break;
  501. }
  502. }
  503. }
  504. orientation = (int) (Math.random()*2)+1;
  505. if (orientation == vertical && size == 3){
  506. while (size == 3){
  507. occupiedSpace *= 0;
  508. row = (int) (Math.random()*7)+1;
  509. column = (int) (Math.random()*10)+1;
  510. String test = grid[row][column];
  511. for (int i = 0; i < size; i++){
  512. test = grid[row+i][column];
  513. if (!test.equals(" ")){
  514. occupiedSpace ++;
  515. }
  516. }
  517. if (occupiedSpace == 0){
  518. for (int k = 0; k < size; k++)
  519. grid[row+k][column] = " S";
  520. size --;
  521. }
  522. }
  523. }
  524. else if (orientation == horizontal && size == 3){
  525. while (size == 3){
  526. occupiedSpace *= 0;
  527. row = (int) (Math.random()*10)+1;
  528. column = (int) (Math.random()*7)+1;
  529. String test = grid[row][column];
  530. for (int i = 0; i < size; i++){
  531. test = grid[row][column+i];
  532. if (!test.equals(" ")){
  533. occupiedSpace ++;
  534. }
  535. }
  536. if (occupiedSpace == 0){
  537. for (int z = 0; z < size; z++)
  538. grid[row][column+z] = " S";
  539. size --;
  540. }
  541. }
  542. }
  543. orientation = (int) (Math.random()*2)+1;
  544. if (orientation == vertical && size == 2){
  545. while (size == 2){
  546. occupiedSpace *= 0;
  547. row = (int) (Math.random()*8)+1;
  548. column = (int) (Math.random()*10)+1;
  549. String test = grid[row][column];
  550. for (int i = 0; i < size; i++){
  551. test = grid[row+i][column];
  552. if (!test.equals(" ")){
  553. occupiedSpace ++;
  554. }
  555. }
  556. if (occupiedSpace == 0){
  557. for (int k = 0; k < size; k++)
  558. grid[row+k][column] = " P";
  559. size --;
  560. }
  561. }
  562. }
  563. else if (orientation == horizontal && size == 2){
  564. while (size == 2){
  565. occupiedSpace *= 0;
  566. row = (int) (Math.random()*10)+1;
  567. column = (int) (Math.random()*8)+1;
  568. String test = grid[row][column];
  569. for (int i = 0; i < size; i++){
  570. test = grid[row][column+i];
  571. if (!test.equals(" ")){
  572. occupiedSpace ++;
  573. }
  574. }
  575. if (occupiedSpace == 0){
  576. for (int z = 0; z < size; z++)
  577. grid[row][column+z] = " P";
  578. size --;
  579. }
  580. }
  581. }
  582. }
  583. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement