1
2 // Copyright Keld Helsgaun 2000, Oliver Kowalke 2014.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <boost/coroutine/all.hpp>
8
9 #include <cstdlib>
10 #include <iostream>
11
12 #include <boost/bind.hpp>
13 #include <boost/move/move.hpp>
14 #include <boost/random/random_device.hpp>
15 #include <boost/random/uniform_int_distribution.hpp>
16
17 typedef boost::coroutines::symmetric_coroutine< void > coro_t;
18
19 class player
20 {
21 private:
die()22 int die()
23 {
24 boost::random::uniform_int_distribution<> dist( 1, 6);
25 return dist( gen);
26 }
27
run_(coro_t::yield_type & yield)28 void run_( coro_t::yield_type & yield)
29 {
30 int sum = 0;
31 while ( ( sum += die() ) < 100)
32 yield( nxt->coro);
33 std::cout << "player " << id << " winns" << std::endl;
34 }
35
36 player( player const&);
37 player & operator=( player const&);
38
39 public:
40 int id;
41 player * nxt;
42 coro_t::call_type coro;
43 boost::random::random_device gen;
44
player(int id_)45 player( int id_) :
46 id( id_), nxt( 0),
47 coro( boost::bind( & player::run_, this, _1) ),
48 gen()
49 {}
50
run()51 void run()
52 { coro(); }
53 };
54
main(int argc,char * argv[])55 int main( int argc, char * argv[])
56 {
57 player * first = new player( 1);
58 player * p = first;
59 for ( int i = 2; i <= 4; ++i)
60 {
61 p->nxt = new player( i);
62 p = p->nxt;
63 }
64 p->nxt = first;
65 first->run();
66
67 std::cout << "Done" << std::endl;
68
69 return EXIT_SUCCESS;
70 }
71