Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. var canvas, context, list, tileWidth, tileHeight, turn, interval, colors, lines;
  2.  
  3. window.addEventListener("load", loaded, false);
  4.  
  5. function loaded(){
  6. canvas = document.getElementById("myCanvas");
  7. canvas.addEventListener("click", onClick, false);
  8. context = canvas.getContext("2d");
  9.  
  10. context.fillStyle = "grey";
  11. context.strokeStyle ="black";
  12. context.lineWidth = 10;
  13.  
  14. turn = 1;
  15. interval = null;
  16. colors = getRandomColors(64);
  17.  
  18. console.log(colors);
  19.  
  20.  
  21. list = [[0,0,0],
  22. [0,0,0],
  23. [0,0,0]];
  24.  
  25. var hallo = true;
  26. if(hallo) {
  27.  
  28. }
  29.  
  30. tilesInWidth = list[0].length;
  31. tilesInHeight = list.length;
  32. tileWidth = canvas.width / tilesInWidth;
  33. tileHeight = canvas.height / tilesInHeight;
  34.  
  35. draw();
  36. }
  37.  
  38.  
  39.  
  40. function onClick(event){
  41. var ox = event.offsetX;
  42. var oy = event.offsetY;
  43. var tx = Math.floor(ox / tileWidth);
  44. var ty = Math.floor(oy / tileHeight);
  45.  
  46. console.log( list[ty][tx] );
  47.  
  48. if(list[ty][tx] === 0) {
  49. list[ty][tx] = turn;
  50.  
  51.  
  52. if(checkWinner() ) {
  53. alert('we hebben een winnaar!');
  54. }else{
  55. changeTurn();
  56. }
  57. }
  58.  
  59. draw();
  60. }
  61.  
  62. function checkWinner() {
  63. //horizon
  64. if( list[0][0]===turn && list[0][1]===turn && list[0][2]===turn) {
  65. return true;
  66. }
  67. if( list[1][0]===turn && list[1][1]===turn && list[1][2]===turn) {
  68. return true;
  69. }
  70. if( list[2][0]===turn && list[2][1]===turn && list[2][2]===turn) {
  71. return true;
  72. }
  73. //vertikaal
  74. if( list[0][0]===turn && list[1][0]===turn && list[2][0]===turn) {
  75. return true;
  76. }
  77. if( list[0][1]===turn && list[1][1]===turn && list[2][1]===turn) {
  78. return true;
  79. }
  80. if( list[0][2]===turn && list[1][2]===turn && list[2][2]===turn) {
  81. return true;
  82. }
  83. //diagonaal
  84. if( list[0][0]===turn && list[1][1]===turn && list[2][2]===turn) {
  85. rainbow( [[0,0], [1,1], [2,2]] );
  86. return true;
  87. }
  88. if( list[0][2]===turn && list[1][1]===turn && list[2][0]===turn) {
  89. return true;
  90. }
  91.  
  92. return false;
  93. }
  94. function rainbow(tiles) {
  95. interval = setInterval(function() {
  96.  
  97.  
  98.  
  99. console.log('tick!');
  100. },100);
  101. }
  102.  
  103.  
  104.  
  105. function changeTurn() {
  106. if(turn === 1) {
  107. turn = 2;
  108. }else {
  109. turn = 1;
  110. }
  111. }
  112.  
  113. function draw(){
  114. context.clearRect(0,0, canvas.width, canvas.height);
  115. for(var y=0; y<list.length; y++){
  116. for(var x=0; x<list[y].length; x++){
  117.  
  118. switch( list[y][x] ) {
  119. case 0: // lege tegel
  120.  
  121. break;
  122. case 1: // kruisje
  123. cross(x,y);
  124. break;
  125. case 2: // cirkel
  126. circle(x,y);
  127. break;
  128. }
  129. }
  130. }
  131. }
  132.  
  133. function circle(x,y){
  134. var posX= x * tileWidth + (tileWidth/2);
  135. var posY= y * tileHeight + (tileHeight/2);
  136. context.strokeStyle = "#FF0000";
  137. context.lineWidth = 8;
  138. context.beginPath();
  139. context.arc( posX,posY,40,0,2*Math.PI);
  140. context.stroke();
  141. }
  142.  
  143. function cross(x,y){
  144. var posX= x * tileWidth + (tileWidth/2);
  145. var posY = y * tileHeight + (tileHeight/2);
  146. var size = 40;
  147. context.strokeStyle = "#00FF00";
  148. context.lineWidth = 8;
  149. context.beginPath();
  150. context.moveTo(posX -size, posY + size);
  151. context.lineTo(posX + size, posY - size);
  152. context.moveTo(posX - size , posY -size );
  153. context.lineTo(posX + size, posY + size);
  154. context.stroke();
  155. }
  156.  
  157. function getRandomColors(amount) {
  158. var list = [];
  159. for (var i=0; i<amount; i++) {
  160. list.push(getRandomColor() );
  161. }
  162. return list;
  163. }
  164.  
  165. function getRandomColor() {
  166. var chars = '';
  167. for (var i = 0; i < 6; i++) {
  168. chars += getRandomChar();
  169. }
  170. return '#'+chars;
  171. }
  172.  
  173. function getRandomChar() {
  174. var chars = [0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f'];
  175. var char = chars[ getRandomInt(0,15) ];
  176. return char.toString();
  177.  
  178. }
  179.  
  180. function getRandomInt(min,max) {
  181. return Math.floor(Math.random()*(max-min+1)+min);
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement