Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Battleship {
  5.  
  6. public static void main(String[] args) {
  7. int[][] board = new int[5][5];
  8. int[][] ships = new int[3][2];
  9. int[] shoot = new int[2];
  10. int attempts=0,
  11. shotHit=0;
  12.  
  13. initBoard(board);
  14. initShips(ships);
  15.  
  16. System.out.println();
  17.  
  18. do{
  19. showBoard(board);
  20. shoot(shoot);
  21. attempts++;
  22.  
  23. if(hit(shoot,ships)){
  24. hint(shoot,ships,attempts);
  25. shotHit++;
  26. }
  27. else
  28. hint(shoot,ships,attempts);
  29.  
  30. changeboard(shoot,ships,board);
  31.  
  32.  
  33. }while(shotHit!=3);
  34.  
  35. System.out.println("\n\n\nBattleship Java game finished! You hit 3 ships in "+attempts+" attempts");
  36. showBoard(board);
  37. System.out.println("Do you want to play again? Type y / n and hit enter");
  38.  
  39. Scanner scan = new Scanner(System.in);
  40. String playAgain = scan.next();
  41.  
  42. }
  43.  
  44.  
  45. public static void initBoard(int[][] board){
  46. for(int row=0 ; row < 5 ; row++ )
  47. for(int column=0 ; column < 5 ; column++ )
  48. board[row][column]=-1;
  49. }
  50.  
  51. public static void showBoard(int[][] board){
  52. System.out.println("\t1 \t2 \t3 \t4 \t5");
  53. System.out.println();
  54.  
  55. for(int row=0 ; row < 5 ; row++ ){
  56. System.out.print((row+1)+"");
  57. for(int column=0 ; column < 5 ; column++ ){
  58. if(board[row][column]==-1){
  59. System.out.print("\t"+"~");
  60. }else if(board[row][column]==0){
  61. System.out.print("\t"+"*");
  62. }else if(board[row][column]==1){
  63. System.out.print("\t"+"X");
  64. }
  65.  
  66. }
  67. System.out.println();
  68. }
  69.  
  70. }
  71.  
  72. public static void initShips(int[][] ships){
  73. Random random = new Random();
  74.  
  75. for(int ship=0 ; ship < 3 ; ship++){
  76. ships[ship][0]=random.nextInt(5);
  77. ships[ship][1]=random.nextInt(5);
  78.  
  79. for(int last=0 ; last < ship ; last++){
  80. if( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) )
  81. do{
  82. ships[ship][0]=random.nextInt(5);
  83. ships[ship][1]=random.nextInt(5);
  84. }while( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) );
  85. }
  86.  
  87. }
  88. }
  89.  
  90. public static void shoot(int[] shoot){
  91. Scanner input = new Scanner(System.in);
  92.  
  93. System.out.print("Row: ");
  94. shoot[0] = input.nextInt();
  95. shoot[0]--;
  96.  
  97. System.out.print("Column: ");
  98. shoot[1] = input.nextInt();
  99. shoot[1]--;
  100.  
  101. }
  102.  
  103. public static boolean hit(int[] shoot, int[][] ships){
  104.  
  105. for(int ship=0 ; ship<ships.length ; ship++){
  106. if( shoot[0]==ships[ship][0] && shoot[1]==ships[ship][1]){
  107. System.out.printf("You hit a ship located in (%d,%d)\n",shoot[0]+1,shoot[1]+1);
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113.  
  114. public static void hint(int[] shoot, int[][] ships, int attempt){
  115. int row=0,
  116. column=0;
  117.  
  118. for(int line=0 ; line < ships.length ; line++){
  119. if(ships[line][0]==shoot[0])
  120. row++;
  121. if(ships[line][1]==shoot[1])
  122. column++;
  123. }
  124.  
  125. System.out.printf("\nHint %d: \nRow %d -> %d ships\n" +
  126. "Column %d -> %d ships\n",attempt,shoot[0]+1,row,shoot[1]+1,column);
  127. }
  128.  
  129. public static void changeboard(int[] shoot, int[][] ships, int[][] board){
  130. if(hit(shoot,ships))
  131. board[shoot[0]][shoot[1]]=1;
  132. else
  133. board[shoot[0]][shoot[1]]=0;
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement