npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

othello-game-logic

v0.0.15

Published

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

Downloads

5

Readme

othello-game-logic

Build Status

インストール

npm i othello-game-logic

これは何?

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

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

State

以下の大きなJSONがゲームのステートになって送られてくる。

{
  // ゲームの局面
  // "init": 始まる前
  // "white": 白のターン
  // "black": 黒のターン
  // "win-white": 白の勝ち
  // "win-black": 黒の勝ち
  // "draw": 引き分け
  gameState: "init"

  // 盤面の状態
  board: [ // 8✕8の2次元配列 (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のインスタンスを作成します

var { createStore } = require("othello-game-logic");
var sotre = createStore();

subscribe: ステートが変化したタイミングでステートを受け取るイベントハンドラを設定します

store.subscribe((state) => {
  // stateが送られてきます
});

dispatch: ステートを変更するためにActionを投げます

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"に変更します。

store.dispatch(ActionCreator.gameStart());

putStone

指定した場所に石を置きます。

store.dispatch(ActionCreator.putStone(0, 1, "white"));

skip

石が置ける場所がない時に、順番をスキップします。

store.dispatch(ActionCreator.skip());

reset

ゲームが終了した時に、gameStateを"init"に戻し、盤面も初期状態に戻します。

store.dispatch(ActionCreator.reset());

Reducer

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

Reactでの使い方の例

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>
    );
  }
}