Add a small dice rolling example game
This commit is contained in:
parent
3a8285a518
commit
a3c1199da9
6 changed files with 151 additions and 5 deletions
88
examples/dieroll/main.cpp
Normal file
88
examples/dieroll/main.cpp
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
#include "wrenpp/vm_fun.hpp"
|
||||||
|
#include "wrenpp/def_configuration.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <random>
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
struct CustomData {
|
||||||
|
CustomData (std::random_device::result_type r) :
|
||||||
|
generator(r),
|
||||||
|
distrib(1, 6)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::mt19937 generator;
|
||||||
|
std::uniform_int_distribution<int> 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<CustomData>();
|
||||||
|
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<char>(ifs),
|
||||||
|
std::istreambuf_iterator<char>()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} //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<void>(vm, {"main", "the_game"}, "play_game");
|
||||||
|
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||||
|
return 0;
|
||||||
|
}
|
47
examples/dieroll/main.wren
Normal file
47
examples/dieroll/main.wren
Normal file
|
@ -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()
|
5
examples/dieroll/meson.build
Normal file
5
examples/dieroll/meson.build
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
executable('dieroll',
|
||||||
|
'main.cpp',
|
||||||
|
dependencies: wrenpp_dep,
|
||||||
|
install: false,
|
||||||
|
)
|
1
examples/meson.build
Normal file
1
examples/meson.build
Normal file
|
@ -0,0 +1 @@
|
||||||
|
subdir('dieroll')
|
|
@ -65,8 +65,12 @@ wrenpp_dep = declare_dependency(
|
||||||
include_directories: public_incl,
|
include_directories: public_incl,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if get_option('build_examples')
|
||||||
executable(meson.project_name() + 'sample',
|
executable(meson.project_name() + 'sample',
|
||||||
'src/main.cpp',
|
'src/main.cpp',
|
||||||
dependencies: wrenpp_dep,
|
dependencies: wrenpp_dep,
|
||||||
install: false,
|
install: false,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
subdir('examples')
|
||||||
|
endif
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
option('build_testing', type: 'boolean', value: false)
|
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_cli', type: 'boolean', value: true, yield: true)
|
||||||
option('wren_with_rand', type: 'boolean', value: false, yield: true)
|
option('wren_with_rand', type: 'boolean', value: false, yield: true)
|
||||||
option('wren_with_meta', type: 'boolean', value: false, yield: true)
|
option('wren_with_meta', type: 'boolean', value: false, yield: true)
|
||||||
|
|
Loading…
Add table
Reference in a new issue