Advertisement
Guest User

battleship try1

a guest
Oct 22nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class battleship {
  7.  
  8.  
  9.  
  10.  
  11.  
  12. public static char[][] display = new char[][]{
  13. {'.', '.', '.', '.', '.'},
  14. {'.', '.', '.', '.', '.'},
  15. {'.', '.', '.', '.', '.'},
  16. {'.', '.', '.', '.', '.'},
  17. {'.', '.', '.', '.', '.'},
  18. };
  19.  
  20. public static char[][] hidden = new char[][]{
  21. {'.', '.', '.', '.', '.'},
  22. {'.', '.', '.', '.', '.'},
  23. {'.', '.', '.', '.', '.'},
  24. {'.', '.', '.', '.', '.'},
  25. {'.', '.', '.', '.', '.'},
  26. };
  27.  
  28. public static int missCount = 0;
  29. public static int hitCount = 0;
  30. public static String location;
  31.  
  32.  
  33.  
  34. public static void main(String args[]) {
  35.  
  36. Scanner myScanner = new Scanner(System.in);
  37. location = myScanner;
  38.  
  39. userDisplay();
  40. System.out.println();
  41.  
  42.  
  43. setUp();
  44. System.out.println();
  45. hidden();
  46.  
  47.  
  48. }
  49.  
  50.  
  51. public static void userDisplay() {
  52. System.out.println(" 0 1 2 3 4 ");
  53. for (int i = 0; i < 5; i++) {
  54. System.out.print(i + " ");
  55. for (int j = 0; j < display[i].length; j++) {
  56. System.out.print(display[i][j] + " ");
  57.  
  58. }
  59. System.out.println();
  60. }
  61. }
  62.  
  63. public static void hidden() {
  64. System.out.println(" 0 1 2 3 4 ");
  65. for (int i = 0; i < 5; i++) {
  66. System.out.print(i + " ");
  67. for (int j = 0; j < hidden[i].length; j++) {
  68. System.out.print(hidden[i][j] + " ");
  69.  
  70. }
  71. System.out.println();
  72. }
  73. }
  74.  
  75. public static void setUp() {
  76.  
  77. int shipStartRow = (int) (Math.random() * 5); //row
  78. int shipStartCol = (int) (Math.random() * 5); //col
  79. int shipBuildDir = (int) (Math.random() * 2);
  80.  
  81. System.out.println("col is: " + shipStartCol + " row is :" + shipStartRow);
  82. System.out.println("direction is: " + shipBuildDir);
  83. System.out.println(Math.random());
  84.  
  85. if (shipBuildDir == 0) {
  86. if (shipStartCol > 2) {
  87. hidden[shipStartRow][shipStartCol] = 'S';
  88. hidden[shipStartRow][shipStartCol - 1] = 'S';
  89. hidden[shipStartRow][shipStartCol - 2] = 'S';
  90. } else /*if (shipStartCol < 2)*/ {
  91. hidden[shipStartRow][shipStartCol] = 'S';
  92. hidden[shipStartRow][shipStartCol + 1] = 'S';
  93. hidden[shipStartRow][shipStartCol + 2] = 'S';
  94. }
  95. }
  96.  
  97. if (shipBuildDir == 1) {
  98. if (shipStartRow > 2) {
  99. hidden[shipStartRow][shipStartCol] = 'S';
  100. hidden[shipStartRow - 1][shipStartCol] = 'S';
  101. hidden[shipStartRow - 2][shipStartCol] = 'S';
  102. } else /*if (shipStartRow < 2)*/ {
  103. hidden[shipStartRow][shipStartCol] = 'S';
  104. hidden[shipStartRow + 1][shipStartCol] = 'S';
  105. hidden[shipStartRow + 2][shipStartCol] = 'S';
  106. }
  107. }
  108.  
  109. }
  110.  
  111. public static void play(){
  112.  
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement