Guest User

Untitled

a guest
Feb 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.26 KB | None | 0 0
  1. function create_game_field(x, y){
  2. var div = document.getElementById("game_field");
  3.  
  4. var table = document.createElement("table");
  5. var tableBody = document.createElement("tbody");
  6.  
  7. //Creating the table
  8. for(var i = 1; i <= x; i++){
  9. //Creates a table row
  10. var row = document.createElement("tr");
  11. //Set ID for that <tr> element of type "row-*"
  12. row.setAttribute("id", "row-"+i);
  13.  
  14. for(var j = 1; j <= y; j++){
  15. //Create a <td> element and put it at the end of the table
  16. var cell = document.createElement("td");
  17. //Set ID for that <td> element of type "col-*-*"
  18. cell.setAttribute("id", "col-"+i+"-"+j);
  19. var cellText = document.createTextNode(".");
  20. cell.appendChild(cellText);
  21. row.appendChild(cell);
  22. }
  23.  
  24. //Now add the row to the end of the table
  25. tableBody.appendChild(row);
  26.  
  27. //Add the <tbody> element in the <table>
  28. table.appendChild(tableBody);
  29.  
  30. //Append <table> into <div id="game_field">
  31. div.appendChild(table);
  32. }
  33. };
  34.  
  35. //Function that returns a random number between min (included) and max (included)
  36. function getRandomInteger(min, max){
  37. return Math.floor(Math.random() * (max - min + 1)) + min;
  38. };
  39.  
  40. //This function adds Battleship (5 squares) on the field;
  41. //quantity - number of battleships,
  42. //string - "horizontal" or "vertical" positioning of the ship
  43. function createBattleship(quantity, string){
  44. for(var i = 1; i <= quantity; i++){
  45. if(string == "horizontal"){
  46. do{
  47. //random number between 0 and 9 (included) for the row
  48. var row = getRandomInteger(0, 9);
  49. //random number between 0 and 5 (included) for the column
  50. var col = getRandomInteger(0, 5);
  51. }while(isEmpty(row, col, 5, "horizontal") != true);
  52.  
  53. for(var j = 0; j < 5; j++){
  54. game_field[row][col] = 1;
  55. col++;
  56. }
  57.  
  58. requiredShots += 5;
  59. }else if(string == "vertical"){
  60. do{
  61. //random number between 0 and 5 (included) for the row
  62. var row = getRandomInteger(0, 5);
  63. //random number between 0 and 9 (included) for the column
  64. var col = getRandomInteger(0, 9);
  65. }while(isEmpty(row, col, 5, "vertical") != true);
  66.  
  67. for(var j = 0; j < 5; j++){
  68. game_field[row][col] = 1;
  69. row++;
  70. }
  71.  
  72. requiredShots += 5;
  73. }else{
  74. alert("Something went wrong. I can't position the Battleship w/o correct info");
  75. }
  76. }
  77. };
  78.  
  79. //This function adds Destroyer (4 squares) on the field;
  80. //quantity - number of destroyers,
  81. //string - "horizontal" or "vertical" positioning of the ship
  82. function createDestroyer(quantity, string){
  83. for(var i = 1; i <= quantity; i++){
  84. if(string == "horizontal"){
  85. do{
  86. //random number between 0 and 9 (included) for the row
  87. var row = getRandomInteger(0, 9);
  88. //random number between 0 and 6 (included) for the column
  89. var col = getRandomInteger(0, 6);
  90. }while(isEmpty(row, col, 4, "horizontal") != true);
  91.  
  92. for(var j = 0; j < 4; j++){
  93. game_field[row][col] = 1;
  94. col++;
  95. }
  96.  
  97. requiredShots += 4;
  98. }else if(string == "vertical"){
  99. do{
  100. //random number between 0 and 6 (included) for the row
  101. var row = getRandomInteger(0, 6);
  102. //random number between 0 and 9 (included) for the column
  103. var col = getRandomInteger(0, 9);
  104. }while(isEmpty(row, col, 4, "vertical") != true);
  105.  
  106. for(var j = 0; j < 4; j++){
  107. game_field[row][col] = 1;
  108. row++;
  109. }
  110.  
  111. requiredShots += 4;
  112. }else{
  113. alert("Something went wrong. I can't position the Destroyer w/o correct info");
  114. }
  115. }
  116. }
  117.  
  118. //This function returns "true" if there is enough space
  119. //to position a ship w/o overlap; otherwise returns "false"
  120. function isEmpty(x, y, count, string){
  121. var empty = true;
  122.  
  123. if(string == "horizontal"){
  124. for(var i = 0; i < count; i++){
  125. if(game_field[x][y] == 1){
  126. empty = false;
  127. y++;
  128. }
  129. }
  130. }
  131.  
  132. if(string == "vertical"){
  133. for(var i = 0; i < count; i++){
  134. if(game_field[x][y] == 1){
  135. empty = false;
  136. x++;
  137. }
  138. }
  139. }
  140.  
  141. return empty;
  142. }
  143.  
  144. //The following array will be used as a game field
  145. //0 - empty space w/o battleship
  146. //1 - battleship
  147. //Battleships will be added on a random principle
  148. var game_field = [
  149. [0,0,0,0,0,0,0,0,0,0],
  150. [0,0,0,0,0,0,0,0,0,0],
  151. [0,0,0,0,0,0,0,0,0,0],
  152. [0,0,0,0,0,0,0,0,0,0],
  153. [0,0,0,0,0,0,0,0,0,0],
  154. [0,0,0,0,0,0,0,0,0,0],
  155. [0,0,0,0,0,0,0,0,0,0],
  156. [0,0,0,0,0,0,0,0,0,0],
  157. [0,0,0,0,0,0,0,0,0,0],
  158. [0,0,0,0,0,0,0,0,0,0]
  159. ];
  160.  
  161. //Variable with the number of successful shots;
  162. //You need 13 successful shots to complete a game with 1 battleship and 2 destroyers;
  163. //1 * 5 squares (battleship) + 2 * 4 squares (destroyer)
  164. var successfulShots = 0;
  165.  
  166. //Variable with the number of successful shots a Player needs to complete the game;
  167. //This variable will be changed for every ship, added to the game field
  168. var requiredShots = 0;
  169.  
  170. //Variable with the number of all shots (missed and successful)
  171. var allShots = 0;
  172.  
  173. $(document).ready(function(){
  174. //Create game field with size 10x10 cells
  175. create_game_field(10, 10);
  176.  
  177. //Add table header with numbers from 1 to 10 for each column
  178. $("table").prepend("<thead></thead>");
  179. $("thead").append("<tr></tr>");
  180. for (var number = 1; number <= 10; number++){
  181. $("table thead tr").append("<td>"+number+"</td>");
  182. }
  183.  
  184. createBattleship(1, "horizontal");
  185. createDestroyer(1, "horizontal");
  186. createDestroyer(1, "vertical");
  187. console.log(game_field);
  188.  
  189. //We will keep the input field's coordinates in two separate variables
  190. var input_index_1, input_index_2;
  191.  
  192. $("input").on("change", function(){
  193. switch($("input").val().substring(0, 1)){
  194. case "A": input_index_1 = 1; break;
  195. case "B": input_index_1 = 2; break;
  196. case "C": input_index_1 = 3; break;
  197. case "D": input_index_1 = 4; break;
  198. case "E": input_index_1 = 5; break;
  199. case "F": input_index_1 = 6; break;
  200. case "G": input_index_1 = 7; break;
  201. case "H": input_index_1 = 8; break;
  202. case "I": input_index_1 = 9; break;
  203. case "J": input_index_1 = 10; break;
  204. default: alert("Incorrect coordinates! Please try again...");
  205. }
  206. input_index_2 = Number($("input").val().substring(1, 3));
  207. });
  208.  
  209. $("button").on("click", function(){
  210. if(game_field[input_index_1 - 1][input_index_2 - 1] == 1){
  211. $("#col-"+input_index_1+"-"+input_index_2).text("x");
  212. $("#col-"+input_index_1+"-"+input_index_2).css({"color":"green", "font-weight":"bold"});
  213.  
  214. $("#info").text("*** Success ***");
  215. $("#info").css("color", "green");
  216.  
  217. successfulShots++;
  218. allShots++;
  219.  
  220. game_field[input_index_1 - 1][input_index_2 - 1] = 2;
  221.  
  222. }else if(game_field[input_index_1 - 1][input_index_2 - 1] == 0){
  223. $("#col-"+input_index_1+"-"+input_index_2).text("-");
  224. $("#col-"+input_index_1+"-"+input_index_2).css({"color":"red", "font-weight":"bold"});
  225.  
  226. $("#info").text("*** Miss ***");
  227. $("#info").css("color", "red");
  228.  
  229. allShots++;
  230.  
  231. }else{
  232. $("#info").text("*** This location has already been hit ***");
  233. $("#info").css("color", "orange");
  234.  
  235. allShots++;
  236. }
  237.  
  238. if(successfulShots == requiredShots){
  239. $("#info").text("Well done! You completed the game in "+allShots+" shots");
  240. $("#info").css({"color":"darkblue", "font-style":"italic"});
  241.  
  242. $("button").attr("disabled", "disabled");
  243. }
  244. });
  245. });
Add Comment
Please, Sign In to add comment