Skip to content

Commit

Permalink
fourteen one rules
Browse files Browse the repository at this point in the history
  • Loading branch information
tailuge committed Oct 20, 2023
1 parent d194cea commit 50457dd
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 16 deletions.
4 changes: 2 additions & 2 deletions dist/diagram.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/controller/rules/fourteenone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Rack } from "../../utils/rack"
import { NineBall } from "./nineball"
import { Rules } from "./rules"

export class FourteenOne extends NineBall implements Rules {
override rack() {
return Rack.triangle()
}
}
4 changes: 4 additions & 0 deletions src/controller/rules/rulefactory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FourteenOne } from "./fourteenone"
import { NineBall } from "./nineball"
import { ThreeCushion } from "./threecushion"

Expand All @@ -6,6 +7,9 @@ export class RuleFactory {
if (ruletype === "threecushion") {
return new ThreeCushion(container)
}
if (ruletype === "fourteenone") {
return new FourteenOne(container)
}
return new NineBall(container)
}
}
45 changes: 33 additions & 12 deletions src/utils/rack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export class Rack {
static readonly noise = (R * 0.05) / 0.5
static readonly gap = 2 * R + 2 * Rack.noise
static readonly up = new Vector3(0, 0, -1)
static readonly spot = new Vector3(-TableGeometry.X / 2, 0.0, 0)
static readonly across = new Vector3(0, Rack.gap, 0)
static readonly diagonal = Rack.across
.clone()
.applyAxisAngle(Rack.up, (Math.PI * 1) / 3)

private static jitter(pos) {
return roundVec(
Expand All @@ -23,38 +28,54 @@ export class Rack {
)
}

static readonly spot = new Vector3(-TableGeometry.X / 2, 0.0, 0)

static cueBall(pos) {
return new Ball(pos, 0xfaebd7)
}

static diamond() {
const across = new Vector3(0, Rack.gap, 0)
const diagonal = across.clone().applyAxisAngle(Rack.up, (Math.PI * 1) / 3)
const pos = new Vector3(TableGeometry.tableX / 2, 0, 0)
const diamond: Ball[] = []
diamond.push(Rack.cueBall(Rack.spot))
diamond.push(new Ball(Rack.jitter(pos), 0xe0de36))
pos.add(diagonal)
pos.add(Rack.diagonal)
diamond.push(new Ball(Rack.jitter(pos), 0xff9d00))
pos.sub(across)
pos.sub(Rack.across)
diamond.push(new Ball(Rack.jitter(pos), 0x521911))
pos.add(diagonal)
pos.add(Rack.diagonal)
diamond.push(new Ball(Rack.jitter(pos), 0x595200))
pos.sub(across)
pos.sub(Rack.across)
diamond.push(new Ball(Rack.jitter(pos), 0xff0000))
pos.addScaledVector(across, 2)
pos.addScaledVector(Rack.across, 2)
diamond.push(new Ball(Rack.jitter(pos), 0x050505))
pos.add(diagonal).sub(across)
pos.add(Rack.diagonal).sub(Rack.across)
diamond.push(new Ball(Rack.jitter(pos), 0x0a74c2))
pos.sub(across)
pos.sub(Rack.across)
diamond.push(new Ball(Rack.jitter(pos), 0x087300))
pos.add(diagonal)
pos.add(Rack.diagonal)
diamond.push(new Ball(Rack.jitter(pos), 0x3e009c))
return diamond
}

static triangle() {
const triangle = Rack.diamond()
const pos = new Vector3(TableGeometry.tableX / 2, 0, 0)
pos.addScaledVector(Rack.diagonal, 3)
triangle.push(new Ball(Rack.jitter(pos)))
pos.add(Rack.diagonal)
triangle.push(new Ball(Rack.jitter(pos)))
pos.sub(Rack.across)
triangle.push(new Ball(Rack.jitter(pos)))
pos.sub(Rack.across)
pos.sub(Rack.across)
triangle.push(new Ball(Rack.jitter(pos)))
pos.sub(Rack.across)
triangle.push(new Ball(Rack.jitter(pos)))
pos.sub(Rack.diagonal)
pos.add(Rack.across)
triangle.push(new Ball(Rack.jitter(pos)))
return triangle
}

static three() {
const threeballs: Ball[] = []
const dx = round(TableGeometry.X / 2)
Expand Down
31 changes: 31 additions & 0 deletions test/rules/fourteenone.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import "mocha"
import { expect } from "chai"
import { Container } from "../../src/container/container"
import { Aim } from "../../src/controller/aim"
import { PlayShot } from "../../src/controller/playshot"
import { StationaryEvent } from "../../src/events/stationaryevent"
import { GameEvent } from "../../src/events/gameevent"
import { Outcome } from "../../src/model/outcome"
import { RuleFactory } from "../../src/controller/rules/rulefactory"
import { WatchAim } from "../../src/controller/watchaim"
import { initDom } from "../view/dom"

initDom()

describe("FourteenOne", () => {
let container: Container
let broadcastEvents: GameEvent[]
const rule = "fourteenone"

beforeEach(function (done) {
container = new Container(undefined, (_) => {}, false, rule)
broadcastEvents = []
container.broadcast = (x) => broadcastEvents.push(x)
done()
})

it("Fourteenone has 16 balls", (done) => {
expect(container.table.balls).to.be.length(16)
done()
})
})

0 comments on commit 50457dd

Please sign in to comment.