8
\$\begingroup\$

In Cookie Clicker there are Golden cookies, and Wrath Golden Cookies.

Golden cookies are Clickable cookies that randomly appear, and when clicked they give Bonus cookies, or temporary boosts to clicking/Cookies Per second (i.e. Cps).

The Wrath Golden cookies can still give boosts and bonuses, but they have the chance of taking cookies or giving a reverse boost to Cps.

Wrath Golden Cookies happen as an effect of pushing the Grandmas too far.

Some "power-ups" require Golden cookies to stay on the screen and not be clicked right away, so I wanted a fast way to turn off the clicker.

I came up with a way to turn on and off the Golden/Wrath switches using a single command in the console, and they give a little more information as well.

// allows me to turn on and off the clicking of the Wrath Cookies

var wrathCookieSwitchOn = false;
var cookieSwitchOn = false;

var wrathSwitch = function() {
    wrathCookieSwitchOn = !wrathCookieSwitchOn;
    console.log("WrathCookieSwitchOn is set to " + wrathCookieSwitchOn);
}

var flipCookieSwitch = function(){
    cookieSwitchOn = !cookieSwitchOn;
    if (cookieSwitchOn) {
        var SpecialCookieClicker = 
            setInterval(function() {
                Game.shimmers.forEach(function(shimmer) {
                    (shimmer.type == "golden") && (shimmer.wrath == 0 || wrathCookieSwitchOn) ? shimmer.pop() : 0;
                })
            }, 500);
        console.log("Golden Cookie Clicker is on and wrathCookieSwitchOn is set to " + wrathCookieSwitchOn);
    } else {
        clearInterval(SpecialCookieClicker);
        console.log("Golden Cookie Clicker is Turned off");
    }
};
flipCookieSwitch();

I was thinking about overloading the function so that I could flip the Wrath Switch as well, but I haven't decided how I want to do that yet.

Does the code make sense to you? Is there anything I could make better or more efficient?

Link to the Game itself, it is addicting and YOU HAVE BEEN WARNED

none of the scripts that I have posted here are considered cheating by the game

\$\endgroup\$
  • 2
    \$\begingroup\$ You mean, none of the scripts are detected as cheating by the game. Whether you or other people consider them cheating is a completely separate matter. \$\endgroup\$ – Snowbody Dec 19 '17 at 18:31
  • \$\begingroup\$ you get an achievement for cheating, I probably should elaborate that...lol. you don't get the achievement for cheating when using these scripts. \$\endgroup\$ – Malachi Dec 19 '17 at 18:43
  • \$\begingroup\$ Do you typically run these functions in the console, a bookmark-let and/or a grease monkey script? \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Dec 29 '17 at 3:07
  • \$\begingroup\$ Normally run it via the console. I suppose that you could use a bookmark, but I think it is quicker if you use the console on your second monitor. \$\endgroup\$ – Malachi Dec 29 '17 at 3:37
2
\$\begingroup\$

Responses

Does the code make sense to you?

Yes, for the most part. I am still becoming acquainted with the game play and hacks.

anything I could make better or more efficient?

In general, using an integer 0/1 instead of boolean would lead to faster performance1. To change the value, the bitwise NOT operator (i.e. ~) can be used. Note that applying NOT on a signed integer will yield -` but that is still a truthy value. While it was asked more than 6 years ago, this post has some interesting discussion on that matter.

enter image description here

var wrathCookieSwitchOn = 0;
var cookieSwitchOn = 0;

var wrathSwitch = function() {
  wrathCookieSwitchOn = ~wrathCookieSwitchOn;
  console.log("WrathCookieSwitchOn is set to " + wrathCookieSwitchOn);
}

function checkWrathCookieSwitchOn() {
  if (wrathCookieSwitchOn) {
    console.log('wrathCookieSwitchOn',wrathCookieSwitchOn);
  }
}
wrathSwitch();
checkWrathCookieSwitchOn();
wrathSwitch();
checkWrathCookieSwitchOn();

Other thoughts

Ternary vs Short-circuiting logical operators

As I mentioned in an answer to your previous post, the ternary could be simplified from:

(shimmer.type == "golden") && (shimmer.wrath == 0 || wrathCookieSwitchOn) ? shimmer.pop() : 0;

To:

(shimmer.type == "golden") && (shimmer.wrath == 0 || wrathCookieSwitchOn) && shimmer.pop();

While it only saves about 2 keystrokes, it is essentially like removing an else clause from an if statement.

Variable declaration

The declaration of the timer variable SpecialCookieClicker is within the true block of the if statement. While hoisting allows the variable to exist throughout the function, "it is recommended to always declare variables at the top of their scope"2 and then assign the value later (e.g..

var flipCookieSwitch = function(){
    var SpecialCookieClicker; //declare here
    cookieSwitchOn = !cookieSwitchOn;
    if (cookieSwitchOn) {
        SpecialCookieClicker = setInterval(/*function */, 500); //assign here
    }
}

While it should be the case that SpecialCookieClicker will always be assigned before the else block, it may be wise to guard against the scenario of an undefined value (e.g. due to cosmic rays) and ensure it is at least truthy before calling clearInterval().

SpecialCookieClicker && clearInterval(SpecialCookieClicker); 

Variable names

The word On in variables wrathCookieSwitchOn and cookieSwitchOn convey the sense that the switch is on when the variable is true. Without the word On it could still be considered semantically the same - so one could argue that the names could be shortened to wrathCookieSwitch and cookieSwitch without any loss of semantics

ES-6

Presuming you aren't using IE, you should be able to utilize ES-6 features, like const for values that shouldn't be re-assigned and let for variables that can change. Then the one-liner for that function to pop shimmers can be simplified with an arrow function:

Game.shimmers.forEach((shimmer) => (shimmer.type == "golden") && (shimmer.wrath == 0 || wrathCookieSwitchOn) && shimmer.pop(), 500);

1https://jsperf.com/bool-vs-int/8

2https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.