From a3c1199da9b6d26e0b83cfd283c15770316b4e85 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sat, 2 May 2020 22:41:58 +0200 Subject: [PATCH] Add a small dice rolling example game --- examples/dieroll/main.cpp | 88 ++++++++++++++++++++++++++++++++++++ examples/dieroll/main.wren | 47 +++++++++++++++++++ examples/dieroll/meson.build | 5 ++ examples/meson.build | 1 + meson.build | 14 ++++-- meson_options.txt | 1 + 6 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 examples/dieroll/main.cpp create mode 100644 examples/dieroll/main.wren create mode 100644 examples/dieroll/meson.build create mode 100644 examples/meson.build diff --git a/examples/dieroll/main.cpp b/examples/dieroll/main.cpp new file mode 100644 index 0000000..ff4ad4e --- /dev/null +++ b/examples/dieroll/main.cpp @@ -0,0 +1,88 @@ +#include "wrenpp/vm_fun.hpp" +#include "wrenpp/def_configuration.hpp" +#include +#include +#include +#include +#include +#include + +namespace { + struct CustomData { + CustomData (std::random_device::result_type r) : + generator(r), + distrib(1, 6) + { + } + + std::mt19937 generator; + std::uniform_int_distribution distrib; + }; + + void user_input(wren::VM& vm) { + std::string retval; + std::cin >> retval; + set(vm, 0, retval); + } + + void random_num(wren::VM& vm) { + auto* const cust_data = vm.user_data(); + set(vm, 0, cust_data->distrib(cust_data->generator)); + } + + class MyConfig : public wren::DefConfiguration { + public: + wren::foreign_method_t foreign_method_fn ( + wren::VM* vm, + const char* module_ptr, + const char* class_name_ptr, + bool is_static, + const char* signature_ptr + ) { + std::string_view module(module_ptr); + std::string_view class_name(class_name_ptr); + std::string_view signature(signature_ptr); + //std::cout << "requested: \"" << module << "\" \"" << + // class_name << "\" \"" << signature << "\" static: " << + // std::boolalpha << is_static << '\n'; + + if ("main" == module) { + if ("Game" == class_name) { + if ("user_input()" == signature and is_static) + return &user_input; + else if ("random()" == signature and is_static) + return &random_num; + } + } + return nullptr; + } + + //char* load_module_fn (wren::VM* vm, const char* name) { + // std::cout << "asked to load module \"" << name << "\"\n"; + // return nullptr; + //} + }; + + std::string load_file (const std::string& path) { + std::ifstream ifs(path); + ifs >> std::noskipws; + + return std::string( + std::istreambuf_iterator(ifs), + std::istreambuf_iterator() + ); + } +} //unnamed namespace + +int main() { + std::random_device rand_dev; + CustomData custom_data(rand_dev()); + + MyConfig config; + wren::VM vm(&config, &custom_data); + interpret(vm, "main", load_file("main.wren")); + wren::call(vm, {"main", "the_game"}, "play_game"); + + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + return 0; +} diff --git a/examples/dieroll/main.wren b/examples/dieroll/main.wren new file mode 100644 index 0000000..1d5a3e7 --- /dev/null +++ b/examples/dieroll/main.wren @@ -0,0 +1,47 @@ +import "random" for Random + +class Game { + construct new() { + _player_money = 6 + _my_money = 6 + } + + foreign static user_input() + //foreign static random() + + play_game() { + System.print("Welcome to wrengame!") + + while (true) { + System.print("/me rolls a die... *roll* *roll*") + System.print("Your balance is %(_player_money)€ and I have %(_my_money)€, pay 1€ and guess the number!") + + var roll = Random.new().int(6) + 1 + var guess = Num.fromString(Game.user_input()) + _player_money = _player_money - 1 + _my_money = _my_money - 2 + + if (roll == guess) { + System.print("I rolled %(roll)! You win 3€!") + _player_money = _player_money + 3 + } else { + _my_money = _my_money + 3 + System.print("I rolled %(roll), you lose!") + } + + if (_my_money < 2 || _player_money < 1) { + break + } + } + + if (_my_money < _player_money) { + System.print("Game finished, you win!") + } else { + System.print("Game finished, I win!") + } + System.print("Final balance is %(_player_money)€ for you and %(_my_money)€ for me") + System.print("Let's play again soon!") + } +} + +var the_game = Game.new() diff --git a/examples/dieroll/meson.build b/examples/dieroll/meson.build new file mode 100644 index 0000000..b3098a8 --- /dev/null +++ b/examples/dieroll/meson.build @@ -0,0 +1,5 @@ +executable('dieroll', + 'main.cpp', + dependencies: wrenpp_dep, + install: false, +) diff --git a/examples/meson.build b/examples/meson.build new file mode 100644 index 0000000..9640f44 --- /dev/null +++ b/examples/meson.build @@ -0,0 +1 @@ +subdir('dieroll') diff --git a/meson.build b/meson.build index b0657e4..5bcb2d8 100644 --- a/meson.build +++ b/meson.build @@ -65,8 +65,12 @@ wrenpp_dep = declare_dependency( include_directories: public_incl, ) -executable(meson.project_name() + 'sample', - 'src/main.cpp', - dependencies: wrenpp_dep, - install: false, -) +if get_option('build_examples') + executable(meson.project_name() + 'sample', + 'src/main.cpp', + dependencies: wrenpp_dep, + install: false, + ) + + subdir('examples') +endif diff --git a/meson_options.txt b/meson_options.txt index bb96719..9b314b6 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,4 +1,5 @@ option('build_testing', type: 'boolean', value: false) +option('build_examples', type: 'boolean', value: false) option('wren_with_cli', type: 'boolean', value: true, yield: true) option('wren_with_rand', type: 'boolean', value: false, yield: true) option('wren_with_meta', type: 'boolean', value: false, yield: true)