Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. setInterval(function(){time()},1000);
  2. var sec = -1;
  3. var w,k;
  4.  
  5. function time()
  6. {
  7. if(sec>-1){
  8. $('#time').html(sec++);
  9. }
  10. }
  11.  
  12. function getId(strId) {
  13. arr = new Array(2);
  14. arr[0]=strId.substring(3, strId.indexOf('_'));
  15. arr[1]=strId.substring(strId.indexOf('_')+1, strId.length);
  16. return arr
  17. }
  18.  
  19. function Picture(accept) {
  20. this.val;
  21. do{
  22. rand = Math.floor(Math.random() * accept.length);
  23. }while(accept[rand]==2);
  24. accept[rand]++;
  25. this.val = rand;
  26. }
  27.  
  28. function GameBoard(n,m) {
  29. this.n = n;
  30. this.m = m;
  31.  
  32. score = 0;
  33.  
  34. picBoard = new Array(n);
  35. last = -1;
  36.  
  37. accepTab = new Array(n*m/2);
  38. for (var i=0; i<accepTab.length; i++){
  39. accepTab[i] = 0;
  40. }
  41.  
  42. for (var i=0; i<picBoard.length; i++){
  43. picBoard[i] = new Array(m);
  44. for (var j=0; j<picBoard[i].length; j++){
  45. picBoard[i][j] = new Picture(accepTab);
  46. }
  47. }
  48.  
  49. function getPicture(id) {
  50. return picBoard[id[0]][id[1]].val
  51. }
  52.  
  53. function show(obj){
  54. val = getPicture(getId(obj.id));
  55. $(obj).html(val);
  56. if(last==-1){
  57. last = val;
  58. lastId = getId(obj.id);
  59. } else {
  60. if(last==val){
  61. $('#score').html(++score);
  62. } else {
  63. setTimeout(function(){
  64. $(obj).html("X");
  65. $("#col"+lastId[0]+"_"+lastId[1]).html("X");
  66. }, 1000);
  67. }
  68. last=-1;
  69. }
  70. }
  71.  
  72. for(var i=0; i<n; i++){
  73. $("#board").append("<div class=row id=row"+i+"> ");
  74. for(var j=0; j<m; j++){
  75. $("#board #row"+i).append("<span class=col id=col"+i+"_"+j+">X");
  76. $("#col"+i+"_"+j).bind("click", function() {
  77. show(this);
  78.  
  79. });
  80.  
  81. }
  82.  
  83. }
  84.  
  85. }
  86.  
  87. var board;
  88.  
  89. function start(){
  90. board = new GameBoard(w,k);
  91. sec=0;
  92. $("#start").unbind("click");
  93. }
  94.  
  95. $().ready(function () {
  96. $("#start").bind("click", function() {
  97. wielkosc();
  98. });
  99. });
  100.  
  101. function wielkosc() {
  102. w = parseInt(document.getElementById("w").value)
  103. k = parseInt(document.getElementById("k").value)
  104. if(w*k%2==0)
  105. start()
  106. else{
  107. alert("Bledny rozmiar tablicy");}
  108. }
  109.  
  110.  
  111. //////////////////////////////////////////////////////
  112.  
  113.  
  114. <!DOCTYPE html>
  115. <head>
  116. <meta charset="utf-8" />
  117. <link rel="stylesheet" href="style.css" />
  118. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  119. <script src="skrypt.js"></script>
  120. <title>Tytuł strony / podstrony</title>
  121. </head>
  122. <body>
  123.  
  124. <header>
  125. <h1>Pamięć</h1>
  126. </header>
  127.  
  128. <section>
  129. <p>
  130. <div class="button" id="start">Start</div>
  131. <div>
  132. Czas: <span id="time">0</span>
  133. </div>
  134. <div>
  135. Wynik: <span id="score">0</span>
  136. </div>
  137. </p>
  138.  
  139. <div id="board"></div>
  140. </section>
  141. <section>
  142. wiersze: <input type="text" id="w" name="wier"><br>
  143. kolumny: <input type="text" id="k" name="kol"><br>
  144. </section>
  145.  
  146.  
  147.  
  148. <footer>
  149. <p>
  150. <p>Copyright &copy; 2014 jkozak.pl</p>
  151. </p>
  152. </footer>
  153. </body>
  154. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement