Skip to content

Instantly share code, notes, and snippets.

@jgaye
Last active June 21, 2018 18:14
Show Gist options
  • Save jgaye/006c24e84105a132565d6f743805053e to your computer and use it in GitHub Desktop.
Save jgaye/006c24e84105a132565d6f743805053e to your computer and use it in GitHub Desktop.
/* For Cookie Clicker v. 2.0106
coded on Chrome 66
Figure out the optimal buildings to build
by comparing production over price ratios
Runs every second
To stop the loop, refresh the page or type
clearInterval(cookieInterval);
in your JS console
*/
// The best building to buy is the one that cost the less Dollars per added cookies
// min ( costOfOneBuilding / prodOfOneBuilding)
// Example
// If the farm produce 5x as much as a Grandma, but only costs 4x as much
// It's better to build a Farm than a Grandma
// The Price of the Farm will increase faster that it's production
// So we will end up to an equilibrium where farmPrice/farmProd ~= grandmaPrice/grandmaProd
// Then extends to all buildings
var count = 0;
var margin = 0.05;
cookieInterval = setInterval(function(){
console.log("Loop " + count);
count += 1;
var noBuy = false;
// If there's no cursors, buy one
if (Game.ObjectsById[0].amount == 0){
Game.ObjectsById[0].buy(1);
} else {
var buildingsCostPerCookie = [];
// 15 different buildings
for (i=0; i<15; i++){
var building = Game.ObjectsById[i];
// Do not have a lower tier building at 0 !
if (building.amount != 0){
var Cpc = building.price / ((building.storedTotalCps / building.amount)*Game.globalCpsMult);
buildingsCostPerCookie.push(+Cpc.toFixed(2));
}
}
console.log(buildingsCostPerCookie);
var lowestRatio = Math.min(...buildingsCostPerCookie);
var bestBuildingId = buildingsCostPerCookie.indexOf(lowestRatio);
// I want to be able to choose what to do next when we are at an equilibrium
// The equilibirum happens when the lowestRatio is in the margin
// of the max ratio. Then nothing is bought and some manual action is required
// Good time to buy store updates or new buildings
var highestRatio = Math.max(...buildingsCostPerCookie);
if (lowestRatio > highestRatio*(1-margin)){
console.log("No outstanding building to buy");
noBuy = true;
} else if (Game.cookies < Game.ObjectsById[bestBuildingId].price){
console.log("Would build one " + Game.ObjectsById[bestBuildingId].name + " but not enough cookies");
noBuy = true;
} else {
Game.ObjectsById[bestBuildingId].buy(10);
console.log("Built one " + Game.ObjectsById[bestBuildingId].name);
}
// If nothing was built, try to build a new tier building
if (noBuy){
if (Game.cookies < Game.ObjectsById[buildingsCostPerCookie.length].price){
console.log("Would build one " + Game.ObjectsById[buildingsCostPerCookie.length].name + " but not enough cookies");
} else {
Game.ObjectsById[buildingsCostPerCookie.length].buy(1);
console.log("Built one " + Game.ObjectsById[buildingsCostPerCookie.length].name);
}
}
}
//Bonus: if there is a golden cookie (shimmer), click on it
if (Game.shimmers.length) {
Game.shimmers[0].l.click();
console.log("Clicked on a golden cookie!");
}
//Other Bonus: if there is an upgrade to buy, buy it
if (Game.UpgradesInStore.length){
if (Game.UpgradesInStore[0].basePrice < Game.cookies){
console.log("Buying " + Game.UpgradesInStore[0].name + " upgrade");
Game.UpgradesInStore[0].buy();
}
}
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment