Advertisement
Guest User

Battleship code

a guest
Nov 22nd, 2013
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script>
  5.  
  6. var BOARD_WIDTH = 10;
  7. var BOARD_HEIGHT = 10;
  8.  
  9. // Vector2D "class"
  10. function Vector2D(x, y) {
  11. return { x: x,
  12. y: y,
  13. add: function (other) {
  14. return new Vector2D(x + other.x, y + other.y)
  15. },
  16. toString: function() {
  17. return "(" + x + "," + y + ")";
  18. }
  19. };
  20. }
  21.  
  22. // Constants
  23. var HORIZONTAL = Vector2D(1, 0);
  24. var VERTICAL = Vector2D(0, 1);
  25.  
  26. // Extend Array class with pickRandom method
  27. Array.prototype.pickRandom = function () {
  28. var idx = Math.floor( Math.random() * this.length );
  29. return this[idx];
  30. };
  31.  
  32. // Returns a random orientation
  33. function randomOrientation() {
  34. return [ HORIZONTAL, VERTICAL ].pickRandom();
  35. }
  36.  
  37. // Generates random integers
  38. function randomInteger(maximum) {
  39. return Math.floor( Math.random() * maximum);
  40. }
  41.  
  42. // Generates random positions
  43. function randomPosition() {
  44. return Vector2D( randomInteger(BOARD_WIDTH), randomInteger(BOARD_HEIGHT) );
  45. }
  46.  
  47. // Boatclass
  48. function Boat(position, orientation, length) {
  49. return { position: position,
  50. orientation: orientation,
  51. length: length,
  52. hits: (function () {
  53. var result = [];
  54. for(var i = 0; i !== length; i++){
  55. result.push(0);
  56. }
  57. return result;
  58. })(),
  59.  
  60. usedPositions: function() {
  61. var result = [];
  62. var current = position;
  63. for ( var i = 0; i != length; i++ ) {
  64. result.push(current);
  65. current = current.add(orientation);
  66. }
  67. return result;
  68. }
  69. };
  70. }
  71.  
  72. // Checks if x is a memeber of the Array
  73. Array.prototype.member = function (x) {
  74. for(var i = 0; i !== this.length; i++)
  75. {
  76. if(x === this[i]){
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82.  
  83. // Checks sf xs overlaps with the Array
  84. Array.prototype.overlaps = function (xs) {
  85. for(var i= 0; i !== xs.length; i++)
  86. {
  87. if(this.member(xs[i])){
  88. return true;
  89. }
  90. }
  91. return false;
  92.  
  93. }
  94.  
  95. // Generates a random boat with a set length
  96. function generateRandomBoat(size) {
  97. return Boat(randomPosition(), randomOrientation(), size);
  98. }
  99.  
  100. // Checks if all elements of xs equal x
  101. function allEquals(xs, x) {
  102. var all = true;
  103. for (var i = 0; i != xs.length; i++) {
  104. if(xs[i] !== x)
  105. all = false;
  106. }
  107. return all;
  108. }
  109.  
  110. // Generates an Array with random boats
  111. function randomBoats(sizes) {
  112. var occupiedPositions = [];
  113. var boats = [];
  114. sizes = sizes.slice(); // kopieren
  115.  
  116. while ( sizes.length > 0 ) {
  117. var currentSize = sizes.pop();
  118.  
  119. var candidate = generateRandomBoat(currentSize);
  120. var candidateSpaces = candidate.usedPositions();
  121.  
  122. if ( candidateSpaces.overlaps(occupiedPositions) ) {
  123. sizes.push(currentSize);
  124. }
  125. else {
  126. occupiedPositions = occupiedPositions.concat(candidateSpaces);
  127. boats.push(candidate);
  128. }
  129.  
  130. }
  131.  
  132. for(var i = 0; i !== boats.length; i++) {
  133. document.write(boats[i].usedPositions());
  134. document.write(" ||| ");
  135. }
  136. return boats;
  137. }
  138.  
  139. // The game itself
  140. function game(){
  141. inputArray = [4,3,2];
  142. var boats = randomBoats(inputArray);
  143. var currentBoat = 0;
  144. var sunkenBoat = 0;
  145. var numberOfTurns = 0;
  146. while(sunkenBoat !== inputArray.length ) {
  147. var raak= false;
  148. var target = "(" + prompt("Enter targetcoordinate (x,y)") + ")";
  149. var targetString = target.replace(/\s+/g, '');
  150. for(var i = 0; i !== inputArray.length; i++) {
  151. for(var j = 0; j !== boats[i].usedPositions().length; j++) {
  152. console.log(targetString)
  153. if(targetString === boats[i].usedPositions()[j].toString()) {
  154. raak = true;
  155. boats[i].hits[j] = 1;
  156. console.log(boats[i].hits);
  157. currentBoat = boats[i];
  158. }
  159. else {
  160. currentBoat = boats[i];
  161.  
  162. }
  163. }
  164. }
  165.  
  166. console.log(currentBoat.hits);
  167. console.log(allEquals(currentBoat.hits, 1));
  168.  
  169. if(raak)
  170. alert ("Hit!");
  171.  
  172. else
  173. alert ("Miss!");
  174.  
  175. if(allEquals(currentBoat.hits, 1)) {
  176. alert("Boat with length " + currentBoat.hits.length + " has sunken!");
  177. sunkenBoat++;
  178. }
  179. numberOfTurns++
  180. }
  181.  
  182. alert("You've won! You did it in " + numberOfTurns + " turns.")
  183. }
  184.  
  185. game();
  186.  
  187.  
  188. </script>
  189. </head>
  190. <body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement