Skip to content

Commit

Permalink
feat: added connect-four and switched to CMake.js
Browse files Browse the repository at this point in the history
chore: switch `node-gyp` with `cmake.js`
  • Loading branch information
kyranet committed Feb 10, 2021
1 parent a7da938 commit b4a00c4
Show file tree
Hide file tree
Showing 12 changed files with 964 additions and 168 deletions.
23 changes: 22 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"files.associations": {
".nycrc": "json",
".env": "ini",
"*.ts.example": "typescript",
"*.lock": "yaml",
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
Expand Down Expand Up @@ -48,7 +52,24 @@
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"xstring": "cpp",
"compare": "cpp",
"concepts": "cpp",
"list": "cpp",
"xhash": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xtr1common": "cpp",
"xutility": "cpp",
"ios": "cpp",
"iostream": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"bitset": "cpp"
},
"C_Cpp.default.includePath": ["/usr/include/node", "${workspaceFolder}/include", "${workspaceFolder}/node_modules/node-addon-api"]
}
38 changes: 38 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
cmake_policy(SET CMP0042 NEW)
set(CMAKE_CXX_STANDARD 11)

project (skyra-ai
VERSION 1.1.0
DESCRIPTION "Advanced Gaming AI for Skyra"
HOMEPAGE_URL "https://github.com/skyra-project/ai"
LANGUAGES CXX)

include_directories(${CMAKE_JS_INC})
add_definitions(-DNAPI_VERSION=3)
add_definitions(-DNAPI_DISABLE_CPP_EXCEPTIONS)

set(SOURCE_FILES
src/main.cc
src/games/TicTacToe.cc
src/games/ConnectFour.cc)

add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})

# Include N-API wrappers
execute_process(COMMAND node -p "require('node-addon-api').include"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE NODE_ADDON_API_DIR
)

string(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})

target_include_directories(${PROJECT_NAME}
PUBLIC
${CMAKE_SOURCE_DIR}/include
${NODE_ADDON_API_DIR}
${CMAKE_JS_INC}
)
35 changes: 0 additions & 35 deletions binding.gyp

This file was deleted.

22 changes: 22 additions & 0 deletions include/games/ConnectFour.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <cstdint>
#include <array>
#include "utils/Players.hpp"

namespace connect_four
{
constexpr uint_fast8_t board_width = 7;
constexpr uint_fast8_t board_height = 6;
constexpr uint_fast8_t board_cells = board_width * board_height;

typedef std::array<Players, board_cells> ai_cells;
typedef std::array<uint_fast8_t, board_width> ai_remaining;

struct ai_board {
ai_cells cells;
ai_remaining remaining;
};

int_fast8_t position(ai_board &board, int_fast8_t remaining, int_fast8_t maximum_depth) noexcept;
} // namespace connect_four
2 changes: 2 additions & 0 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import bindings from 'bindings';
const addon = bindings('skyra-ai') as Addon;

export const ticTacToe = addon.TicTacToe;
export const connectFour = addon.ConnectFour;

export interface Addon {
TicTacToe(data: Uint8Array): number;
ConnectFour(data: Uint8Array, maximumDepth?: number): number;
}
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
"test": "jest",
"clean": "rimraf build dist",
"build": "run-p build:node build:cpp",
"build:cpp": "run-s gyp**",
"build:cpp": "cmake-js compile",
"build:node": "rollup -c rollup.config.ts",
"gypconfigure": "node-gyp configure",
"gypbuild": "node-gyp build",
"sversion": "standard-version",
"commit": "git-cz",
"cz": "git-cz",
"update": "yarn upgrade-interactive --latest",
"install": "node-gyp rebuild",
"install": "cmake-js compile",
"prepublishOnly": "yarn build && pinst --disable",
"postinstall": "husky install .github/husky",
"postpublish": "pinst --enable"
Expand All @@ -39,11 +37,11 @@
"@types/bindings": "^1.5.0",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.25",
"cmake-js": "^6.1.0",
"cz-conventional-changelog": "^3.3.0",
"husky": "^5.0.9",
"jest": "^26.6.3",
"jest-circus": "^26.6.3",
"node-gyp": "^7.1.2",
"npm-run-all": "^4.1.5",
"pinst": "^2.1.4",
"prettier": "^2.2.1",
Expand Down Expand Up @@ -72,7 +70,6 @@
},
"keywords": [
"discord.js",
"klasa",
"bot",
"standalone"
],
Expand Down
Loading

0 comments on commit b4a00c4

Please sign in to comment.