Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.33 KB | None | 0 0
  1. public class Battleship {
  2.  
  3. public static void main(String[] args) {
  4. boolean gameOver = false;
  5. boolean playAgain = true;
  6. while (playAgain = true) {
  7. Scanner myScanner = new Scanner(System.in);
  8. BattleshipCommands bc = new BattleshipCommands();
  9. System.out.println("Welcome to battleship");
  10. bc.ResetAllGrids();
  11. bc.ShowGrid();
  12. bc.SetUserGrid();
  13. bc.SetCpuGrid();
  14. while (gameOver != true) {
  15. bc.ShowGrid();
  16. bc.UserTurn();
  17. bc.CpuTurn();
  18. }
  19.  
  20. }
  21.  
  22. }
  23. }
  24. public class BattleshipCommands {
  25.  
  26. public String userGrid[][] = new String[10][10];
  27. public String opponentGrid[][] = new String[10][10];
  28. public String cpuGrid[][] = new String[10][10];
  29. public String shipName[] = {"Patrol", "Destroyer", "Submarine", "Battleship", "Carrier"};
  30. public int shipSize[] = {2, 3, 3, 4, 5};
  31. public Scanner myScanner = new Scanner(System.in);
  32. public char horizGrid[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
  33. public int verGrid[] = {1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10};
  34. char horizPosition;
  35. int verPosition;
  36. int horizArrayPos;
  37. int verArrayPos;
  38. public static final char ESC = (char) 27;
  39. public static final String BLACK = ESC + "[30m",
  40. GREY = ESC + "[47;1m",
  41. BLUE = ESC + "[44;1m",
  42. WHITE = ESC + "[37m",
  43. RED = ESC + "[31m";
  44. Random rand = new Random();
  45.  
  46. public BattleshipCommands() {
  47. this.myScanner.useDelimiter("\r|\n");
  48. }
  49.  
  50. public void SetUserGrid() {
  51. for (int index = 0; index < 5; index++) {
  52. System.out.println("Please enter location for the front of your "
  53. + shipName[index] + " which is " + shipSize[index] + " spaces wide.");
  54. String position = myScanner.next();
  55. horizPosition = position.charAt(0);
  56. System.out.println(horizPosition);
  57. verPosition = Integer.parseInt(new Character(position.charAt(1)).toString())-1;
  58. System.out.println(verPosition);
  59. horizArrayPos = 0;
  60. for (int secondIndex = 0; secondIndex < horizGrid.length; secondIndex++) {
  61. if (horizGrid[secondIndex] == horizPosition) {
  62. horizArrayPos = secondIndex;
  63. break;
  64. }
  65. }
  66. System.out.println(horizArrayPos);
  67. System.out.println(verPosition);
  68. boolean correctPlacement = false;
  69. while (correctPlacement != true) {
  70. System.out.println("Place ship going up, down, left, or right?");
  71. String placement = myScanner.next();
  72. userGrid[horizArrayPos][verPosition] = GREY + " 1 ";
  73. switch (placement.toLowerCase()) {
  74. case "up":
  75. if (horizArrayPos == 0) {
  76. System.out.println("Incorrect Placement: Cannot place outside grid");
  77. break;
  78. } else {
  79. for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
  80. userGrid[horizArrayPos + secondIndex][verPosition] = GREY + " " + secondIndex + 1 + " ";
  81. correctPlacement = true;
  82. break;
  83. }
  84. }
  85.  
  86. case "down":
  87. if (horizArrayPos == 9) {
  88. System.out.println("Incorrect Placement: Cannot place outside grid");
  89. break;
  90. } else {
  91. for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
  92. userGrid[horizArrayPos - secondIndex][verPosition] = GREY + " " + secondIndex + 1 + " ";
  93. correctPlacement = true;
  94. break;
  95. }
  96. }
  97. case "right":
  98. if (verPosition == 9) {
  99. System.out.println("Incorrect Placement: Cannot place outside grid");
  100. break;
  101. } else {
  102. for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
  103. userGrid[horizArrayPos][verPosition + secondIndex] = GREY + " " + secondIndex + 1 + " ";
  104. correctPlacement = true;
  105. break;
  106. }
  107. }
  108.  
  109. case "left":
  110. if (verPosition == 0) {
  111. System.out.println("Incorrect Placement: Cannot place outside grid");
  112. break;
  113. } else {
  114. for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
  115. userGrid[horizArrayPos][verPosition - secondIndex] = GREY + " " + secondIndex + 1 + " ";
  116. correctPlacement = true;
  117. break;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124.  
  125. public void SetCpuGrid() {
  126.  
  127. for (int index = 0; index < 5; index++) {
  128.  
  129. horizArrayPos = rand.nextInt(9) + 0;
  130. verArrayPos = rand.nextInt(9) + 0;
  131.  
  132. boolean correctPlacement = false;
  133. while (correctPlacement != true) {
  134.  
  135. cpuGrid[horizArrayPos][verArrayPos] = GREY + " 1 ";
  136. String direction[] = {"up", "down", "right", "left"};
  137. int position = rand.nextInt(3) + 0;
  138. switch (direction[position]) {
  139. case "up":
  140. if (horizArrayPos == 0) {
  141. break;
  142. } else {
  143. for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
  144. cpuGrid[horizArrayPos + secondIndex][verArrayPos] = GREY + " " + secondIndex + 1 + " ";
  145. correctPlacement = true;
  146. break;
  147. }
  148. }
  149. case "down":
  150. if (horizArrayPos == 9) {
  151.  
  152. break;
  153. } else {
  154. for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
  155. cpuGrid[horizArrayPos - secondIndex][verArrayPos] = GREY + " " + secondIndex + 1 + " ";
  156. correctPlacement = true;
  157. break;
  158. }
  159. }
  160. case "right":
  161. if (verArrayPos == 9) {
  162.  
  163. break;
  164. } else {
  165. for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
  166. cpuGrid[horizArrayPos][verArrayPos + secondIndex] = GREY + " " + secondIndex + 1 + " ";
  167. correctPlacement = true;
  168. break;
  169. }
  170. }
  171. case "left":
  172. if (verArrayPos == 0) {
  173.  
  174. break;
  175. } else {
  176. for (int secondIndex = 1; secondIndex < shipSize[index]; secondIndex++) {
  177. cpuGrid[horizArrayPos][verArrayPos - secondIndex] = GREY + " " + secondIndex + 1 + " ";
  178. correctPlacement = true;
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186.  
  187. public void ShowGrid() {
  188. System.out.println("_| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |");
  189. for (int index = 0; index < 9; index++) {
  190. System.out.println(horizGrid[index] + "|" + cpuGrid[index][0] + "|"
  191. + cpuGrid[index][1] + "|" + cpuGrid[index][2] + "|" + cpuGrid[index][3]
  192. + "|" + cpuGrid[index][4] + "|" + cpuGrid[index][5] + "|" + cpuGrid[index][6] + "|"
  193. + cpuGrid[index][7] + "|" + cpuGrid[index][8] + "|" + cpuGrid[index][9]);
  194. }
  195.  
  196. }
  197.  
  198. public void ResetAllGrids() {
  199. for (int index = 0; index < 10; index++) {
  200. for (int secondIndex = 0; secondIndex < 10; secondIndex++) {
  201. userGrid[index][secondIndex] = BLUE + "~~~";
  202. opponentGrid[index][secondIndex] = BLUE + "~~~";
  203. }
  204. }
  205. }
  206.  
  207. public String HitOrMissUser(int verPosition, int horizArrayPos) {
  208. String output = "hit";
  209. if (cpuGrid[horizArrayPos][verPosition].equals(" ")) {
  210. output = "missed";
  211. }
  212. return output;
  213. }
  214.  
  215. private String HitOrMissCpu(int verArrayPos, int horizArrayPos) {
  216. String output = "hit";
  217. if (userGrid[horizArrayPos][verArrayPos].equals(" ")) {
  218. output = "missed";
  219. }
  220. return output;
  221. }
  222.  
  223.  
  224.  
  225. public String UserTurn() {
  226. System.out.println("Please enter location of your shot ");
  227. String position = myScanner.next();
  228. horizPosition = position.charAt(0);
  229. verPosition = Integer.parseInt(new Character(position.charAt(1)).toString())-1;
  230. System.out.println(verPosition);
  231. horizArrayPos = 0;
  232. for (int secondIndex = 0; secondIndex < horizGrid.length; secondIndex++) {
  233. if (horizGrid[secondIndex] == horizPosition) {
  234. horizArrayPos = secondIndex;
  235. break;
  236. }
  237. }
  238. String result = HitOrMissUser(verPosition, horizArrayPos);
  239. return result;
  240. }
  241.  
  242. public String CpuTurn() {
  243.  
  244. horizArrayPos = rand.nextInt(9) + 0;
  245. verArrayPos = rand.nextInt(9) + 0;
  246. String result = HitOrMissCpu(verArrayPos, horizArrayPos);
  247. return result;
  248. }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement