# othello-game-logic

> [![Build Status](https://travis-ci.org/SatoshiKawabata/othello-game-logic.svg?branch=master)](https://travis-ci.org/SatoshiKawabata/othello-game-logic)

Latest version **0.0.15** (published 2018-09-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install othello-game-logic
pnpm add othello-game-logic
yarn add othello-game-logic
bun add othello-game-logic
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.15 |
| Published | 2018-09-14 |
| First published | 2018-06-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 43.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | SatoshiKawabata |
| Maintainers | satoshikawabata |

## Links

- npm: https://www.npmjs.com/package/othello-game-logic
- Repository: https://github.com/SatoshiKawabata/othello-game-logic
- Homepage: https://github.com/SatoshiKawabata/othello-game-logic#readme
- Issues: https://github.com/SatoshiKawabata/othello-game-logic/issues
- npm.io page: https://npm.io/package/othello-game-logic

## Recent versions

- 0.0.15 (latest) — 2018-09-14
- 0.0.14 — 2018-09-10
- 0.0.13 — 2018-07-11
- 0.0.12 — 2018-06-22
- 0.0.11 — 2018-06-19
- 0.0.10 — 2018-06-14
- 0.0.9 — 2018-06-13
- 0.0.8 — 2018-06-13
- 0.0.7 — 2018-06-13
- 0.0.6 — 2018-06-13
- 0.0.5 — 2018-06-06
- 0.0.4 — 2018-06-06
- 0.0.3 — 2018-06-06
- 0.0.2 — 2018-06-06
- 0.0.1 — 2018-06-06

## README

# othello-game-logic
[![Build Status](https://travis-ci.org/SatoshiKawabata/othello-game-logic.svg?branch=master)](https://travis-ci.org/SatoshiKawabata/othello-game-logic)

## インストール
```sh
npm i othello-game-logic
```

## これは何？
オセロのゲームロジックです。オセロの盤面やプレイヤーの情報をステート(状態)として管理します。使い方はReduxのような感じで使います。

- Store: ゲーム全体のステートを管理
- Action: ステートの変更内容
- Reducer: ステートを変更するための関数群

## State
以下の大きなJSONがゲームのステートになって送られてくる。
```js
{
  // ゲームの局面
  // "init": 始まる前
  // "white": 白のターン
  // "black": 黒のターン
  // "win-white": 白の勝ち
  // "win-black": 黒の勝ち
  // "draw": 引き分け
  gameState: "init"

  // 盤面の状態
  board: [ // 8✕8の２次元配列 (0: 空, 1: 白, -1: 黒)
    [ 0, 0, 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 1, -1, 0, 0, 0 ],
    [ 0, 0, 0, -1, 1, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0, 0, 0, 0 ]
  ],

  // プレイヤー情報
  white: {
    placeableCells: [ // 白が置ける場所
      {x: 4, y: 2},
      {x: 5, y: 3},
      {x: 2, y: 4},
      {x: 3, y: 5}
    ],
    name: "white" // プレイヤーの名前
  },
  black: {
    placeableCells: [
      {"x": 3, "y": 2},
      {"x": 2, "y": 3},
      {"x": 5, "y": 4},
      {"x": 4, "y": 5}
    ],
    name: "black"
  }
}
```

## Store
Storeによってゲームのステートを管理します。Storeを購読(subscrie)することでステートの変化を監視したり、Actionを投げる(dispatch)ことでstateを変更できます。

### createStore: Storeのインスタンスを作成します
```js
var { createStore } = require("othello-game-logic");
var sotre = createStore();
```


### subscribe: ステートが変化したタイミングでステートを受け取るイベントハンドラを設定します
```js
store.subscribe((state) => {
  // stateが送られてきます
});
```

### dispatch: ステートを変更するためにActionを投げます
```js
var { ActionCreator } = require("othello-game-logic");
// 0, 1の場所に白い石を置くActionを投げる
store.dispatch(ActionCreator.putStone(0, 1, "white"));
```

### getState: ステートを取得します

## ActionCreator
使用できるActionが入っているオブジェクトです。Actionは`store.dispatch`メソッドに渡してやることでステートを変更します。

### startGame
ゲームをスタートさせます。`gameState`を"init"から"black"に変更します。
```js
store.dispatch(ActionCreator.gameStart());
```

### putStone
指定した場所に石を置きます。
```js
store.dispatch(ActionCreator.putStone(0, 1, "white"));
```

### skip
石が置ける場所がない時に、順番をスキップします。
```js
store.dispatch(ActionCreator.skip());
```

### reset
ゲームが終了した時に、`gameState`を"init"に戻し、盤面も初期状態に戻します。
```js
store.dispatch(ActionCreator.reset());
```


## Reducer
ステートを変更するための関数群です。具体的なオセロのロジックの処理はここに書いてあります。stateとactionを渡してstateを返す関数です。

## Reactでの使い方の例
```js
var {createStore, ActionCreator} = require("othello-game-logic");
class App extends React.Component {
  constructor(props: any) {
    super(props);

    this.store = createStore();
    this.store.subscribe((state) => {
      // reactのsetStateでstateを渡してやる
      this.setState(state);
    });
    this.state = this.store.getState();
  }

  render() {
    return (
      <div>
        <button onClick={() => {
          // ゲームをスタートさせます
          this.store.dispatch(ActionCreator.startGame())
        }}>Game Start</button>
      </div>
    );
  }
}
```

---
_Source: https://npm.io/package/othello-game-logic · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
