source: tests/exceptions/hotpotato.cfa@ 26be854

Last change on this file since 26be854 was 26be854, checked in by caparsons <caparson@…>, 2 years ago

a bit of cleanup of the hotpotato tests

  • Property mode set to 100644
File size: 7.3 KB
Line 
1#include <fstream.hfa> // sin/sout
2#include <stdlib.hfa> // convert
3#include <string.h>
4#include <coroutine.hfa>
5
6struct Potato {
7 PRNG & prng;
8 unsigned int deadline; // when timer goes off
9 unsigned int timer; // up counter to deadline
10}; // Potato
11
12void reset( Potato & potato, unsigned int maxTicks = 10 );
13void ?{}( Potato & potato, PRNG & prng, unsigned int maxTicks = 10 );
14
15void ?{}( Potato & potato, PRNG & prng, unsigned int maxTicks ) with(potato) {
16 &potato.prng = &prng;
17 reset( potato, maxTicks );
18} // Potato
19
20coroutine Player {
21 PRNG & prng;
22 int id; // player identity
23 Potato & potato; // potato being tossed
24 Player * partner[2]; // left and right player
25}; // Player
26
27void ?{}( Player & player, PRNG & prng, unsigned int id, Potato & potato ) {
28 &player.prng = &prng;
29 player.id = id;
30 &player.potato = &potato;
31} // Player
32
33Player & umpire;
34
35ExceptionDecl( Explode );
36ExceptionDecl( Terminate, Player * victim; );
37ExceptionDecl( Election );
38ExceptionDecl( cmd_error ); // convert(...) throws out_of_range or invalid_argument
39
40void reset( Potato & potato, unsigned int maxTicks ) with(potato) {
41 if ( maxTicks < 2 ) abort( "Hot Potato initialized with less than 2 ticks" ); // optional
42 deadline = prng( prng, 1, maxTicks );
43 timer = 0;
44 sout | " POTATO goes off after " | deadline | " tick" | nosep | (deadline > 1 ? "s" : "");
45} // reset
46
47void countdown( Potato & potato ) with(potato) {
48 timer += 1;
49 if ( timer == deadline ) throwResume ExceptionInst( Explode );
50} // countdown
51
52static unsigned int rightOf( unsigned int me, unsigned int total ) {
53 return ( me + 1 ) % total;
54} // rightOf
55
56static unsigned int leftOf( unsigned int me, unsigned int total ) {
57 return ( me + total - 1) % total;
58} // leftOf
59
60enum { LEFT = 0, RIGHT = 1 };
61
62static void vote( Player & player, Election & election ) { // cause partner to vote
63 resumeAt( player, election );
64 resume( player );
65} // vote
66
67static void terminate( Player & player ) { // resume umpire
68 resume( player );
69 assert( false ); // no return
70} // terminate
71
72void init( Player & player, Player & lp, Player & rp ) with(player) { // supply partners
73 partner[LEFT] = &lp;
74 partner[RIGHT] = &rp;
75 resume( player ); // establish main as starter for termination
76} // init
77
78int getId( Player & player ) { // player id
79 return player.id;
80} // getId
81
82void toss( Player & player ) { // tossed the potato
83 resume( player );
84} // toss
85
86void main( Player & player ) with(player) {
87 suspend; // return immediately after establishing starter
88 try {
89 for ( ;; ) {
90 poll(); // check for non-local exceptions before proceeding
91
92 if ( partner[LEFT] == &player ) { // stop when only one player
93 sout | id | " wins the Match!";
94 return;
95 } // exit
96
97 countdown( potato ); // player is eliminated if countdown() returned true
98
99 size_t side = prng( prng, 2 );
100 sout | id | " -> " | nonl;
101 toss( *partner[ side ] ); // random toss left/right
102 } // for
103 } catchResume( Terminate * v ) {
104 v->victim->partner[LEFT]->partner[RIGHT] = v->victim->partner[RIGHT]; // unlink node
105 v->victim->partner[RIGHT]->partner[LEFT] = v->victim->partner[LEFT];
106 delete( v->victim );
107 reset( potato );
108 sout | "U " | nonl; // start new game
109 flush( sout );
110 } catchResume( Election * election ) {
111 sout | "election";
112 sout | " -> " | id | nonl;
113 if ( id > getId( umpire ) ) &umpire = &player; // set umpire to highest id so far
114 vote( *partner[RIGHT], *election );
115 } catchResume ( Explode * ) {
116 sout | id | " is eliminated";
117 if ( &player == &umpire ) {
118 id = -1; // remove from election
119 vote( *partner[RIGHT], ExceptionInst( Election ) ); // start election
120 try { poll(); } catchResume( Election * election ) {} // handle end of election
121 sout | " : umpire " | getId( umpire );
122 } // if
123 resumeAt( umpire, ExceptionInst( Terminate, &player ) );
124 terminate( umpire );
125 assert( false ); // no return
126 } // try
127} // main
128
129
130int main( int argc, char * argv[] ) {
131 enum {
132 MinNoPlayers = 2, // minimum players in the game
133 MaxNoPlayers = 10, // maximum players in the game
134 DefaultGames = 5, // default games to play
135 };
136 intmax_t numGames = DefaultGames; // games to play
137 intmax_t numPlayers = 0; // players for a particular game
138 intmax_t seed = 42; // random-number seed
139 bool playersSet = false;
140 char * nosummary = getenv( "NOSUMMARY" ); // print extra output
141
142 try {
143 choose ( argc ) {
144 case 4:
145 if ( strcmp( argv[3], "d" ) != 0 ) { // default ?
146 seed = convert( argv[3] ); if ( seed < 1 ) throw ExceptionInst( cmd_error ); // invalid ?
147 } // if
148 fallthrough;
149 case 3:
150 if ( strcmp( argv[2], "d" ) != 0 ) { // default ?
151 numPlayers = convert( argv[2] ); if ( numPlayers < 2 ) throw ExceptionInst( cmd_error ); // invalid ?
152 playersSet = true;
153 } // if
154 fallthrough;
155 case 2:
156 if ( strcmp( argv[1], "d" ) != 0 ) { // default ?
157 numGames = convert( argv[1] ); if ( numGames < 0 ) throw ExceptionInst( cmd_error ); // invalid ?
158 } // if
159 fallthrough;
160 case 1: ; // defaults
161 default: // too many arguments
162 throw ExceptionInst( cmd_error );
163 } // choose
164 } catch( exception_t * ) { // catch any
165 exit | "Usage: " | argv[0]
166 | " [ games (>=0) | 'd' (default " | DefaultGames
167 | ") [ players (>=2) | 'd' (random " | MinNoPlayers | "-" | MaxNoPlayers
168 | ") [ seed (>0) | 'd' (random) ] ] ]";
169 } // try
170 sout | numGames | numPlayers | seed;
171
172 PRNG mprng, hprng, pprng;
173 if ( seed != 0 ) { // specified on command line ?
174 set_seed( mprng, seed ); set_seed( hprng, seed ); set_seed( pprng, seed );
175 } // if
176
177 for ( game; 1 ~= numGames ) {
178 if ( ! playersSet ) numPlayers = prng( mprng, MinNoPlayers, MaxNoPlayers );
179 sout | numPlayers | " players in the game";
180 {
181 Potato potato{ hprng }; // hot potato to be tossed
182 Player * players[numPlayers];
183
184 for ( unsigned int i = 0; i < (unsigned int)numPlayers; i += 1 ) { // start the players
185 players[i] = malloc();
186 ?{}( *players[i], pprng, i, potato );
187 } // for
188
189 // Do not swap player[0] with itself.
190 unsigned int rposn = prng( mprng, 1, numPlayers - 1 ); // swap random position with 0
191 swap( players[0], players[rposn] );
192
193 // Tell each player its partner.
194 for ( unsigned int i = 0; i < (unsigned int)numPlayers; i += 1 ) {
195 init( *players[i], *players[leftOf(i, numPlayers)], *players[rightOf(i, numPlayers)] );
196 } // for
197
198 &umpire = players[rposn]; // designate umpire and start game
199 sout | "U " | nonl;
200 toss( *players[rposn] );
201 delete( &umpire );
202 }
203 if ( game < (unsigned int)numGames ) sout | nl | nl; // whitespace between games
204 } // for
205} // main
206
207// Local Variables: //
208// compile-command: "make hotpotato" //
209// End: //
Note: See TracBrowser for help on using the repository browser.