{"id":16539933,"url":"https://github.com/colinkiama/four-in-a-row","last_synced_at":"2026-06-24T17:31:10.697Z","repository":{"id":220002135,"uuid":"750369912","full_name":"colinkiama/four-in-a-row","owner":"colinkiama","description":"Four-In-A Row (aka \"Connect Four\" or \"Four-In-A-Line\") game logic library for JavaScript","archived":false,"fork":false,"pushed_at":"2024-03-18T09:20:22.000Z","size":35,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-02-14T06:47:25.250Z","etag":null,"topics":["board","connect","connect-4","connect-four","connect4","four","four-in-a-row","game","in","library","logic","red","row","simulate","simulation","token","tokens","yellow"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/colinkiama.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-30T14:17:11.000Z","updated_at":"2024-01-31T23:28:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"235c13a1-d8f8-40ce-b036-b3da764bdc68","html_url":"https://github.com/colinkiama/four-in-a-row","commit_stats":null,"previous_names":["colinkiama/four-in-a-row-js"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/colinkiama%2Ffour-in-a-row","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/colinkiama%2Ffour-in-a-row/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/colinkiama%2Ffour-in-a-row/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/colinkiama%2Ffour-in-a-row/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/colinkiama","download_url":"https://codeload.github.com/colinkiama/four-in-a-row/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241774031,"owners_count":20018204,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["board","connect","connect-4","connect-four","connect4","four","four-in-a-row","game","in","library","logic","red","row","simulate","simulation","token","tokens","yellow"],"created_at":"2024-10-11T18:51:08.401Z","updated_at":"2026-06-24T17:31:10.657Z","avatar_url":"https://github.com/colinkiama.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Four-In-A-Row.js\n\nFour-In-A Row (aka \"Connect Four\" or \"Four-In-A-Line\") game logic library for JavaScript. No need to write the logic for the game yourself.\n\nThis was adapted from my [Making Four-In-A-Row Using JavaScript blog series](https://colinkiama.com/blog/making-four-in-a-row-part-1/).\n\n---\n\n## Table of Contents\n\n- [Installing](#installing)\n- [Examples](#example)\n- [Public API](#public-api)\n\n## Installing\n\n### Package Manager\n\nYou can use your favourite package manager to install the library.\n\nFor example:\n\n```bash\nnpm install four-in-a-row\n```\n\n### No build step\n\nNo build step required. Add [four-in-a-row.js](dist/four-in-a-row.js) or [four-in-a-row.min.js](dist/four-in-a-row.min.js) directly to your projects.\n\n## Examples\n\n### Usage\n\n```js\nimport { Game } from \"four-in-a-row\";\n\nconst game = new Game();\nconst startingMoveResult = game.playMove(0); // Yellow Token placed at row: 0, column: 0.\nconst nextMoveResult = game.playMove(1); // Red Token placed at row: 0, column 1.\n```\n\n### Full Example\n\nFor a full example of how to use the library, check out out the [demo example](demo) which is a four-in-a-row [HTML5 Canvas](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) game.\n\n## Public API\n\n### Create a new game\n\n```js\nimport { Game } from \"four-in-a-row\";\n\n// Create a new game\nconst game = new Game();\n\n// Check current status of game\nconst initialStatus = game.status;\n\n// Get starting color\nconst startingColor = game.startingColor;\n\n// Get current turn\nconst turn = game.currentTurn;\n\n// Make a move\nconst moveResult = game.playMove(0);\n\n// Retrieve game board\nconst board = game.currentBoard;\n\n// Restart game\ngame.reset();\n```\n\n### Contstants\n\n#### `GameStatus`\n\n```js\nimport { Game, GameStatus } from 'four-in-a-row';\n\nconsole.log(GameStatus.IN_PROGRESS); // \"in-progress\"\nconsole.log(GameStatus.START); // \"start\"\nconsole.log(GameStatus.WIN); // \"win\"\nconsole.log(GameStatus.DRAW); // \"draw\"\n\nconst game = new Game();\nconsole.log(game.status === GameStatus.START); // true\n```\n\n#### `MoveStatus`\n\n```js\nimport { Game, MoveStatus } from 'four-in-a-row';\n\nconsole.log(MoveStatus.INVALID); // \"invalid\"\nconsole.log(MoveStatus.WIN); // \"win\"\nconsole.log(MoveStatus.SUCCESS); // \"success\"\nconsole.log(MoveStatus.DRAW); // \"draw\"\n\nconst game = new Game();\nconst moveResult = game.playMove(0);\nconsole.log(moveResult.status === MoveStatus.Success); // true\n```\n\n#### `PlayerColor`\n\n```js\nimport { Game, PlayerColor} from 'four-in-a-row';\n\nconsole.log(PlayerColor.NONE); // \"none\"\nconsole.log(PlayerColor.YELLOW); // \"yellow\"\nconsole.log(PlayerColor.RED); // \"red\"\n\nconst game = new Game();\nconsole.log(game.startingColor === PlayerColor.YELLOW); // true;\nconsole.log(game.currentTurn === PlayerColor.YELLOW); // true;\n\ngame.playMove(0);\nconsole.log(game.currentTurn === PlayerColor.RED); // true;\n\nconsole.log(game.currentBoard);\n// game.currentBoard\n// { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n// { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n// { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n// { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n// { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n// { 0: 1, 1: 0, 2: 0, 3: 1, 4: 0, 5: 0, 6: 0 },\n```\n\n#### `BoardDimensions`\n\n```js\nimport { BoardDimensions } from 'four-in-a-row';\n\nconsole.log(BoardDimensions.ROWS); // 6\nconsole.log(BoardDimensions.COLUMNS); // 7\nconsole.log(BoardDimensions.WIN_LINE_LENGTH); // 4\n```\n\n#### `BoardToken`\n\n```js\nimport { Game, BoardToken } from 'four-in-a-row';\n\nconsole.log(BoardToken.NONE); // 0\nconsole.log(BoardToken.YELLOW); // 1\nconsole.log(BoardToken.RED); // 2\n\nconst game = new Game();\ngame.playMove(0);\nconsole.log(game.currentBoard);\n// Output (`Uint8Array[]`- each Uint8Array element contains a `BoardToken`):\n// [\n//   { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n//   { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n//   { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n//   { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n//   { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n//   { 0: 1, 1: 0, 2: 0, 3: 1, 4: 0, 5: 0, 6: 0 },\n// ]\n```\n\n### Instance methods\n\n#### `playMove(columnIndex: number)`\n\nReturns a `MoveResult` object\n\n```js\n{\n  // Uint8Array[] - Each Uint8Array element contains a `BoardToken`\n  nextBoard: [\n    { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n    { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n    { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n    { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },\n    { 0: 0, 1: 0, 2: 0, 3: 0, 4: 2, 5: 2, 6: 2 },\n    { 0: 0, 1: 0, 2: 0, 3: 1, 4: 1, 5: 1, 6: 1 },\n  ],\n  // PlayerColor\n  winner: 'yellow',\n  // MoveStatus\n  status: 'WIN',\n  // BoardPosition[]\n  winLine:\n  [\n    { row: 5, column: 3 },\n    { row: 5, column: 4 },\n    { row: 5, column: 5 },\n    { row: 5, column: 6 },\n  ]\n}\n```\n\n#### `reset()`\n\nRestarts the game.\n\n### Game Over State Behaviour\n\nYou can find out if the game is over by checking for the for a win or draw in the `status` field of a `MoveResult` or the `status` field of a `Game`.\n\nif the game is over, calls to `playMove()` will return the last `MoveResult` (Details of the move that put the game into a game over state).\n\nYou'll have to call the `reset()` to get out of the game over state.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcolinkiama%2Ffour-in-a-row","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcolinkiama%2Ffour-in-a-row","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcolinkiama%2Ffour-in-a-row/lists"}