not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

315 lines
8.0 KiB

  1. function Game(stage,xocolor,doc,datastore,activity,sizepalette){
  2. this.margin = 0;
  3. this.radius = 0;
  4. this.circleswidth = 0;
  5. this.circlesheight = 0;
  6. this.colours = [xocolor.stroke,xocolor.fill];
  7. this.level = 4;
  8. this.turns = 1;
  9. this.gridwidth = 7;
  10. this.gridheight = 7;
  11. this.numFlips = 14;
  12. this.startgridwidth = 4;
  13. this.startgridheight = 4;
  14. this.dots = [];
  15. this.stack = [];
  16. this.solveTimeout;
  17. this.newGameTimeout;
  18. //Helper functions
  19. this.radiusFromX = function(){
  20. this.margin = 1/50*stage.canvas.width;
  21. var diameter = (stage.canvas.width-(this.margin*(this.gridwidth+1)))/this.gridwidth;
  22. var radius = diameter/2;
  23. return radius;
  24. }
  25. this.radiusFromY = function(){
  26. this.margin = 1/50*stage.canvas.height;
  27. var diameter = (stage.canvas.height-(this.margin*(this.gridheight+1)))/this.gridheight;
  28. var radius = diameter/2;
  29. return radius;
  30. }
  31. this.canDoFromX = function(){
  32. var rad = this.radiusFromX();
  33. this.margin = 1/50*stage.canvas.width;
  34. if ((((rad*2)*this.gridheight)+(this.margin*(this.gridheight+1)))<=stage.canvas.height){
  35. return rad;
  36. } else {
  37. return false;
  38. }
  39. }
  40. this.checkGameOver = function(){
  41. var fgWin = true;
  42. var bgWin = true;
  43. for (var x = 0; x<this.startgridwidth; x++){
  44. for (var y = 0; y<this.startgridheight; y++){
  45. if (this.dots[x][y].colour!=0){
  46. bgWin = false;
  47. }
  48. if (this.dots[x][y].colour!=1){
  49. fgWin = false;
  50. }
  51. }
  52. }
  53. if(bgWin || fgWin) {
  54. return true;
  55. } else {
  56. return false;
  57. }
  58. }
  59. //Flipping
  60. this.flip = function(x,y,playing,stack){
  61. this.turns = this.turns+1;
  62. document.getElementById("turnno").innerHTML = " " + (this.turns);
  63. this.drawIndicator();
  64. if (playing===undefined){playing=true;}
  65. if (stack===undefined){stack=true;}
  66. if (stack){
  67. this.stack.push([x,y]);
  68. }
  69. //console.log("flipdots");
  70. this.dots[x][y].flipSelf();
  71. if (0<y){
  72. this.dots[x][y-1].flipSelf();
  73. }
  74. if (0<x){
  75. this.dots[x-1][y].flipSelf();
  76. }
  77. if (y<this.startgridheight-1){
  78. this.dots[x][y+1].flipSelf();
  79. }
  80. if (x<this.startgridwidth-1){
  81. this.dots[x+1][y].flipSelf();
  82. }
  83. if (playing){
  84. if (this.checkGameOver()){
  85. this.gameOver();
  86. }
  87. }
  88. }
  89. this.drawIndicator = function() {
  90. diffind = this.turns - this.level*3;
  91. console.log(diffind);
  92. switch(diffind){
  93. case 40:
  94. document.getElementById("fliptext").style.borderBottom = "5px solid #f00";
  95. break;
  96. case 30:
  97. document.getElementById("fliptext").style.borderBottom = "5px solid #ff410b";
  98. break;
  99. case 20:
  100. document.getElementById("fliptext").style.borderBottom = "5px solid #ff7e00";
  101. break;
  102. case 10:
  103. document.getElementById("fliptext").style.borderBottom = "5px solid #c7ff00";
  104. break;
  105. case 5:
  106. document.getElementById("fliptext").style.borderBottom = "5px solid #00ff1b";
  107. console.log("12");
  108. break;
  109. }
  110. }
  111. this.getRandomInt = function(min, max) {
  112. return Math.floor(Math.random() * (max - min + 1)) + min;
  113. }
  114. this.flipRandomDot = function(){
  115. this.flip(this.getRandomInt(0,this.startgridwidth-1),this.getRandomInt(0,this.startgridheight-1),false);
  116. }
  117. //Solve
  118. this.solve = function(){
  119. if (this.stack.length == 0){
  120. return;
  121. } else {
  122. var dot = this.stack.pop();
  123. this.flip(dot[0],dot[1],false,false);
  124. //var t = this;
  125. //this.solveTimeout = setTimeout(function(){t.solve();},750);
  126. }
  127. if(this.checkGameOver()) {
  128. this.gameOver();
  129. }
  130. }
  131. //Game Over
  132. this.gameOver = function(){
  133. for (var x = 0; x<this.startgridwidth; x++){
  134. for (var y = 0; y<this.startgridheight; y++){
  135. this.dots[x][y].showSmile();
  136. this.dots[x][y].clickable = false;
  137. }
  138. }
  139. if (this.startgridwidth<this.gridwidth){
  140. this.startgridwidth++;
  141. this.startgridheight++;
  142. }
  143. this.palette.setUsed();
  144. var t = this;
  145. this.newGameTimeout = setTimeout(function(){t.newGame();},2000);
  146. }
  147. //Save
  148. this.stop = function(){
  149. //store stack, dots, startgridwidth, startgridheight
  150. var arr = {};
  151. arr.stack = this.stack;
  152. arr.startgridwidth = this.startgridwidth;
  153. arr.startgridheight = this.startgridheight;
  154. arr.dots = [];
  155. arr.turns = this.turns;
  156. arr.indcolour = document.getElementById("fliptext").style.borderBottom;
  157. for (var x = 0; x<this.startgridwidth; x++){
  158. temparr = [];
  159. for (var y = 0; y<this.startgridheight; y++){
  160. temparr.push(this.dots[x][y].colour);
  161. }
  162. arr.dots.push(temparr);
  163. }
  164. //console.log(arr);
  165. var js = JSON.stringify(arr);
  166. //console.log(js);
  167. activity.getDatastoreObject().setDataAsText(js);
  168. activity.getDatastoreObject().save(function(){
  169. activity.close();
  170. });
  171. }
  172. //Init
  173. this.initialiseFromArray = function(){
  174. clearTimeout(this.newGameTimeout);
  175. clearTimeout(this.solveTimeout);
  176. stage.removeAllChildren();
  177. this.calculateDimensions();
  178. var incr = (this.radius*2+this.margin);
  179. var xp = (stage.canvas.width-this.circleswidth)/2+this.margin;
  180. var yp = this.margin;
  181. for (var x = 0; x<this.startgridwidth; x++){
  182. yp = this.margin;
  183. for (var y = 0; y<this.startgridheight; y++){
  184. this.dots[x][y].radius = this.radius;
  185. this.dots[x][y].x = xp+this.radius;
  186. this.dots[x][y].y = yp+this.radius;
  187. this.dots[x][y].init();
  188. yp+=incr;
  189. }
  190. xp+=incr;
  191. }
  192. }
  193. this.initDots = function(dotsindex){
  194. this.dots = [];
  195. var temparr = [];
  196. var incr = (this.radius*2+this.margin);
  197. var xp = (stage.canvas.width-this.circleswidth)/2+this.margin;
  198. var yp = this.margin;
  199. for (var x = 0; x<this.startgridwidth; x++){
  200. temparr = [];
  201. yp = this.margin;
  202. for (var y = 0; y<this.startgridheight; y++){
  203. //console.log(x);
  204. //console.log(y);
  205. var ind = 0;
  206. if (dotsindex!=undefined){
  207. ind = dotsindex[x][y];
  208. }
  209. var s = new Dot(stage,xp+this.radius,yp+this.radius,this.colours,ind,this.radius,this,x,y);
  210. s.init();
  211. temparr.push(s);
  212. //console.log(s);
  213. yp+=incr;
  214. }
  215. this.dots.push(temparr);
  216. xp+=incr;
  217. }
  218. //console.log(this.dotsarr);
  219. }
  220. this.calculateDimensions = function(){
  221. var rad = this.canDoFromX();
  222. if (rad===false){
  223. rad = this.radiusFromY();
  224. }
  225. this.radius = rad;
  226. this.circleswidth = this.radius*2*this.startgridwidth+this.margin*(this.startgridwidth+1);
  227. this.circlesheight = this.radius*2*this.startgridheight+this.margin*(this.startgridheight+1);
  228. }
  229. this.newGame = function(){
  230. clearTimeout(this.newGameTimeout);
  231. clearTimeout(this.solveTimeout);
  232. stage.removeAllChildren();
  233. this.calculateDimensions();
  234. this.initDots();
  235. //console.log(this.dots);
  236. this.stack = [];
  237. for (var i = 0; i<14; i++){
  238. this.flipRandomDot();
  239. }
  240. this.turns = 1;
  241. document.getElementById("turnno").innerHTML = " " + (this.turns);
  242. document.getElementById("fliptext").style.borderBottom = "5px solid #00ff1b";
  243. }
  244. this.setSize = function(size){
  245. this.startgridwidth = size;
  246. this.startgridheight = size;
  247. this.newGame();
  248. this.level = size;
  249. }
  250. this.init = function(){
  251. //console.log("init");
  252. //console.log(activity.getDatastoreObject());
  253. this.palette = new sizepalette.SizePalette(this,doc.getElementById('size-button'),undefined);
  254. activity.getDatastoreObject().getMetadata(this.init_canaccessdatastore.bind(this));
  255. document.getElementById("turnno").innerHTML = " " + (this.turns);
  256. this.drawIndicator();
  257. this.palette.setUsed();
  258. }
  259. this.init_canaccessdatastore = function(error,mdata){
  260. //console.log("datastore check");
  261. var d = new Date().getTime();
  262. if (Math.abs(d-mdata.creation_time)<2000){
  263. //console.log("Time too short");
  264. this.newGame();
  265. } else {
  266. activity.getDatastoreObject().loadAsText(this.init_getdatastore.bind(this));
  267. }
  268. }
  269. this.init_getdatastore = function(error,metadata,data){
  270. if (error==null&&data!=null){
  271. data = JSON.parse(data);
  272. this.restoreFromDatastore(data);
  273. } else {
  274. this.newGame();
  275. }
  276. }
  277. this.restoreFromDatastore = function(data){
  278. this.stack = data.stack;
  279. this.startgridwidth = data.startgridwidth;
  280. this.startgridheight = data.startgridheight;
  281. this.turns = data.turns;
  282. document.getElementById("fliptext").style.borderBottom = data.indcolour;
  283. clearTimeout(this.newGameTimeout);
  284. clearTimeout(this.solveTimeout);
  285. stage.removeAllChildren();
  286. this.calculateDimensions();
  287. this.initDots(data.dots);
  288. }
  289. }