
Product
Introducing Custom Pull Request Alert Comment Headers
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
@brajkowski/connect4-logic
Advanced tools
This library provides APIs for manipulating and querying a standard (7x6) game of connect 4.
This library provides APIs for manipulating and querying a standard (7x6) game of connect 4.
Using npm:
$ npm i --save @brajkowski/connect4-logic
The main class that should be used to manipulate and query the underlying game state is Logic
.
To begin, instantiate an implementation of Logic
(currently BitboardLogic
is the only implementation):
const logic: Logic = new BitboardLogic();
From there, you may perform queries on the game state such as:
let column = 0;
let result: boolean = logic.canPlaceChip(column);
and place chips that belong to a player:
let landedOnRow = logic.placeChip(Player.One, column); // returns 0.
landedOnRow = logic.placeChip(Player.Two, column); // returns 1.
These operations prevent placing chips in non-existent columns (ie: out-of-bounds columns) as well as overflowing a column (ie: a column can only hold 6 chips).
A basic game loop may look like:
const logic: Logic = new BitboardLogic();
let activePlayer: Player = Player.One;
function gameLoop() {
while (true) {
let column = promptActivePlayerForMove();
if (!logic.canPlaceChip(column)) {
continue;
}
logic.placeChip(activePlayer, column);
if (logic.didWin(activePlayer)) {
break;
}
swapActivePlayer();
}
}
The Logic
class provides default connect 4 behavior (ie: chips fall to the bottom of a column and stack), but custom behavior can also be achieved by using the PlayerState
class:
let row = 2;
let column = 3;
logic.getPlayerState(Player.One).occupyPosition(row, column);
The PlayerState
operations allow for any chip placement, but are bounds-checked for valid row and column values and prevent duplicate chip placement (ie: trying to place another chip in a position that is already occupied).
Using npm:
$ npm run build
will produce the compiled library under /dist
.
The test suite may also be executed with npm:
$ npm test
FAQs
This library provides APIs for manipulating and querying a standard (7x6) game of connect 4.
The npm package @brajkowski/connect4-logic receives a total of 0 weekly downloads. As such, @brajkowski/connect4-logic popularity was classified as not popular.
We found that @brajkowski/connect4-logic demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.