Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class Battleship {
  5.  
  6. private Spot[][] battleship;
  7. private int guesses;
  8. private int hits;
  9.  
  10.  
  11. public Battleship(int rows, int columns)
  12. {
  13. battleship = new Spot[rows][columns];
  14. for(int i=0;i<rows;i++)
  15. {
  16. for(int j=0;j<columns;j++)
  17. {
  18. battleship[i][j] = new Spot();
  19. battleship[i][j].setShip(0);
  20. battleship[i][j].setVisited(false);
  21. }
  22. }
  23. }
  24.  
  25. public void setBoard()
  26. {
  27. Random rand = new Random();
  28. int rotate = rand.nextInt(2);
  29. int rows = battleship.length;
  30. int columns = battleship[0].length;
  31. int w;
  32. int x;
  33. int y;
  34. int z;
  35. int ship;
  36. boolean valid=false;
  37. for(ship = 5; ship > 1; ship--)
  38. {
  39. while(!valid)
  40. if(rotate == 0)
  41. {
  42. valid = true;
  43.  
  44. //orientation = 0, ship is vertical
  45. x = rand.nextInt(rows);
  46. y = rand.nextInt(columns+1-ship);
  47. for(z=0;z<ship;z++)
  48. {
  49. if(battleship[x][y+z].getShip()!=0)
  50. {
  51. valid = false;
  52. }
  53. }
  54. }
  55. else
  56. {
  57. valid = true;
  58. //ship is horizontal
  59. x = rand.nextInt(rows+1-ship);
  60. y = rand.nextInt(columns);
  61. for(z=0;z<ship;z++)
  62. {
  63. if(battleship[x+z][y].getShip()!=0)
  64. {
  65. valid = false;
  66. }
  67. }
  68. for(w=0;w<ship;w++)
  69. {
  70. battleship[x+w][y].setShip(ship);
  71. valid = false;
  72. }
  73. }
  74.  
  75.  
  76.  
  77. }
  78. for(w=0;w<ship;w++)
  79. {
  80. battleship[x][y+w].setShip(ship);
  81. valid = false;
  82. }
  83.  
  84. }
  85.  
  86. public void printBoard(boolean reveal)
  87. {
  88. if(reveal)
  89. {
  90.  
  91. for(int i=0;i<battleship.length;i++)
  92. {
  93. System.out.print("\n");
  94. System.out.print(" +-+-+-+-+-+-+-+");
  95. System.out.print("\n");
  96. for(int j=0;j<battleship[0].length;j++)
  97. {
  98. if(j==0)
  99. {
  100. System.out.print(i+1 + " ");
  101. }
  102. System.out.print("|" + battleship[i][j].getShip());
  103. if(j==battleship[0].length-1)
  104. {
  105. System.out.print('|');
  106. }
  107. }
  108.  
  109.  
  110. if(i+1==battleship.length)
  111. {
  112. System.out.print("\n");
  113. System.out.print(" +-+-+-+-+-+-+-+");
  114. System.out.print("\n ");
  115. for(int z=0;z<battleship[0].length;z++)
  116. {
  117. System.out.print(z+1 + " ");
  118. }
  119. }
  120. }
  121. System.out.printf("\n\n");
  122. }
  123. else
  124. {
  125.  
  126. }
  127. }
  128.  
  129. public void makeGuess()
  130. {
  131.  
  132. }
  133.  
  134. public void printStatistics()
  135. {
  136.  
  137. }
  138.  
  139. public boolean over()
  140. {
  141. return(hits==14);
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement