Code Monkey home page Code Monkey logo

reversi-example's Introduction

オセロプログラムサンプル

C#でオセロアプリケーションを,CUIとGUIで実装します.

GUIはBlazorを利用してWEBブラウザアプリケーションとしてビルドします.

また,CIとしてGithub Actions使用してGithub Pagesにデプロイします.

※ BlazorとはJavascriptの代わりにC#とWebAssemblyを用いてブラウザアプリケーションを開発するフレームワークです.

Blazor 公式

オセロDEMOページはこちら

https://le-nn.github.io/reversi-example/

環境

プラットフォーム .NET 7
言語 C# 11
CI Github Actions
デプロイ先 Github Pages

起動

GUI

Visual Studio でReversi.Blazorプロジェクトを起動 もしくはReversi.Blazorディレクトリをターミナルで開いて以下のコマンドを実行

dotnet run

開発用サーバーが起動するので、Consoleに表示されたURLへブラウザからアクセス

CUI

Visual Studio でReversi.ConsoleAppプロジェクトを起動 もしくはReversi.ConsoleAppディレクトリをターミナルで開いて以下のコマンドを実行

dotnet run

プロジェクト構成

project description
Reversi.Core オセロのゲームロジック
Reversi.Blazor WEBブラウザで操作するアプリケーション
Reversi.ConsoleApp ターミナル上のCUIで操作するアプリケーション

拡張

ReversiGameクラスがオセロゲームを提供します。

駒の配置などの入力はIInteractorインターフェースを実装したクラスをReversiGameクラスのコンストラクタに指定します. これによりUIの種類やプラットフォームに依存せず, また,ロジックを変更することなく、 拡張や利用をすることができます. ユーザーのインタラクションのみならず、NPCのAIなども実装することができます.

public interface IInteractor {
    string Name { get; }

    Task<Position> InputAsync(Board board, Piece turn);
}

入力が必要になるとInputAsyncが呼び出されるので、配置する場所を戻り値として返します。

利用と実装例

var player1 = new ConsoleInteractor("Player1");
var player2 = new ConsoleInteractor("Player2");
var game = new ReversiGame(player1, player2); 

class ConsoleInteractor : IInteractor {
    public string Name { get; }

    public ConsoleInteractor(string name) {
        this.Name = name;
    }

    public Task<Position> InputAsync(Board board, Piece turn) {
        // turnは現在のターンの駒の種類
        // boardは現在のボードの状態

        // ...
        // 駒を配置する処理
        // ...

        var position = new Position(..., ...); // 配置する位置

        return position;
    }
}

参考実装

コンソールアプリの場合 ConsoleInteractor.cs

Blazorの場合 PlayerInteractor.cs

reversi-example's People

Contributors

le-nn avatar rdghm1234 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.