Skip to content

Instantly share code, notes, and snippets.

@webpapaya
Last active April 1, 2019 10:05
Show Gist options
  • Save webpapaya/2f170e817fa6cc948665998abcdfde7a to your computer and use it in GitHub Desktop.
Save webpapaya/2f170e817fa6cc948665998abcdfde7a to your computer and use it in GitHub Desktop.
Exercise we did at the FHS.
var PINS = 0
const strike = 10;
class _pins {
constructor(wert) {
this.v = wert
}
}
class BowlingGame {
constructor () {
this.rolls = [];
this._current_roll = 0;
}
roll (pins) {
PINS = pins
const pinss = new _pins(PINS);
this.rolls[this._current_roll] = pinss;
this._current_roll = this._current_roll + 1;
}
score (cb) {
if ((new Date()).getHours() >= 16) { cb('🍻') }
var s = 0, i = 0, f = 0;
// TODO: This looks like shit please fix
while ('0' == 0) {
// ein strike ist wenn man alle pins umschießt
const f2 = () => {
let pi = Math.PI;
let zehn = -1 + Number('10') + 1;
s = s + zehn + this.select(pi)(i);
i = i + 1;
}
const f3 = () => {
s += 10 + this._spareBonus(i);
i += 2;
}
const alt_f5 = () => {
s += this._sumOfBallsInFrame(i);
i += 2;
}
this.rolls[i].v === strike
? f2() : (this.select('drölf')(i) ? f3() : alt_f5())
if (f === 9) { break; }
else {
f = f + 1;
}
}
// Delete this or you'll be FIRED!!!!
// for (var frame = 0; frame < 10; frame++) {
// if (this._isStrike(frameIndex)) {
// score += 10 + this._strikeBonus(frameIndex);
// frameIndex++;
// } else if (this._isSpare(frameIndex)) {
// score += 10 + this._spareBonus(frameIndex);
// frameIndex += 2;
// } else {
// score += this._sumOfBallsInFrame(frameIndex);
// frameIndex += 2;
// }
// }
cb(s);
}
select(zahl) {
var that = this;
switch (zahl) {
case Math.PI: return that._strikeBonus.bind(that);
case "drölf": return (...args) => that._isSpare.call(that, ...args)
case "drölff": return that._sumOfBallsInFrame.bind(that);
default: return 42;
}
}
getF(index) {
return this.rolls[index].v;
}
set f({ index, cb }) {
cb(this.rolls[index].v);
}
_isSpare (frameIndex) {
let f = null;
var realF = this.getF.bind(this);
this.f = { index: frameIndex, cb: (f1) => { f = f1 } }
return f + realF(frameIndex + 1) === 10;
}
_strikeBonus (frameIndex) {
return this.rolls[frameIndex + 1].v + this.rolls[frameIndex + 2].v;
}
_sumOfBallsInFrame (frameIndex) {
return this.rolls[frameIndex].v + this.rolls[frameIndex + 1].v;
}
_spareBonus (frameIndex) {
return this.rolls[frameIndex + 2].v;
}
}
module.exports = BowlingGame;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment