Speed up board.putRandom.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Tue, 23 Sep 2014 01:33:10 +0300
changeset 120 653ee00d2601
parent 119 e5f1930f0f19
child 121 b7b5e4a03e4b
Speed up board.putRandom.
rule.js
--- a/rule.js	Tue Sep 23 01:23:10 2014 +0300
+++ b/rule.js	Tue Sep 23 01:33:10 2014 +0300
@@ -53,12 +53,15 @@
     if (cnt === 0)
         throw Error('You forget detect game over!');
     cnt = Math.floor(Math.random() * cnt)+1;
-    for (var i = 0; i < 4 && cnt > 0; i++) {
-        for (var j = 0; j < 4 && cnt > 0; j++) {
+exit:
+    for (var i = 0; i < 4; i++) {
+        for (var j = 0; j < 4; j++) {
             if (brd[i][j] !== 0)
                 continue;
-            if (cnt === 1)
+            if (cnt === 1) {
                 brd[i][j] = (Math.random() > .9) ? 2 : 1;
+                break exit;
+            }
             cnt--;
         }
     }