Changeset f0c9c9b for tests/exceptions/hotpotato.cfa
- Timestamp:
- Jan 6, 2024, 8:14:15 AM (6 months ago)
- Branches:
- master
- Children:
- 4d689e2
- Parents:
- 40002c5
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tests/exceptions/hotpotato.cfa
r40002c5 rf0c9c9b 5 5 6 6 struct Potato { 7 8 unsigned int deadline;// when timer goes off9 unsigned int timer;// up counter to deadline7 PRNG & prng; 8 unsigned int deadline; // when timer goes off 9 unsigned int timer; // up counter to deadline 10 10 }; // Potato 11 11 … … 16 16 &potato.prng = &prng; 17 17 reset( potato, maxTicks ); 18 } // Potato18 } // Potato 19 19 20 20 coroutine Player { 21 22 int id;// player identity23 Potato & potato;// potato being tossed24 Player * partner[2];// left and right player21 PRNG & prng; 22 int id; // player identity 23 Potato & potato; // potato being tossed 24 Player * partner[2]; // left and right player 25 25 }; // Player 26 26 … … 29 29 player.id = id; 30 30 &player.potato = &potato; 31 } // Player31 } // Player 32 32 33 33 Player & umpire; … … 39 39 40 40 void reset( Potato & potato, unsigned int maxTicks ) with(potato) { 41 41 if ( maxTicks < 2 ) abort( "Hot Potato initialized with less than 2 ticks" ); // optional 42 42 deadline = prng( prng, 1, maxTicks ); 43 43 timer = 0; … … 60 60 enum { LEFT = 0, RIGHT = 1 }; 61 61 62 static void vote( Player & player, Election & election ) { 63 62 static void vote( Player & player, Election & election ) { // cause partner to vote 63 resumeAt( player, election ); 64 64 resume( player ); 65 65 } // vote … … 76 76 77 77 void main( Player & player ) with(player) { 78 suspend;// return immediately after establishing starter78 suspend; // return immediately after establishing starter 79 79 try { 80 81 poll();// check for non-local exceptions before proceeding82 83 84 85 86 87 88 countdown( potato );// player is eliminated if countdown() returned true89 90 91 92 resume( *partner[ side ] );// random toss left/right93 80 for ( ;; ) { 81 poll(); // check for non-local exceptions before proceeding 82 83 if ( partner[LEFT] == &player ) { // stop when only one player 84 sout | id | " wins the Match!"; 85 return; 86 } // exit 87 88 countdown( potato ); // player is eliminated if countdown() returned true 89 90 size_t side = prng( prng, 2 ); 91 sout | id | " -> " | nonl; 92 resume( *partner[ side ] ); // random toss left/right 93 } // for 94 94 } catchResume( Terminate * v ) { 95 95 v->victim->partner[LEFT]->partner[RIGHT] = v->victim->partner[RIGHT]; // unlink node 96 96 v->victim->partner[RIGHT]->partner[LEFT] = v->victim->partner[LEFT]; 97 97 delete( v->victim ); 98 98 reset( potato ); 99 99 sout | "U " | nonl; // start new game … … 102 102 sout | "election"; 103 103 sout | " -> " | id | nonl; 104 if ( id > getId( umpire ) ) &umpire = &player; 104 if ( id > getId( umpire ) ) &umpire = &player; // set umpire to highest id so far 105 105 vote( *partner[RIGHT], *election ); 106 106 } catchResume ( Explode * ) { 107 108 109 id = -1;// remove from election110 vote( *partner[RIGHT], ExceptionInst( Election ) );// start election111 112 113 114 115 resume( umpire );// resume umpire to terminate this player116 assert( false );// no return117 107 sout | id | " is eliminated"; 108 if ( &player == &umpire ) { 109 id = -1; // remove from election 110 vote( *partner[RIGHT], ExceptionInst( Election ) ); // start election 111 try { poll(); } catchResume( Election * election ) {} // handle end of election 112 sout | " : umpire " | getId( umpire ); 113 } // if 114 resumeAt( umpire, ExceptionInst( Terminate, &player ) ); 115 resume( umpire ); // resume umpire to terminate this player 116 assert( false ); // no return 117 } // try 118 118 } // main 119 119 … … 151 151 case 1: ; // defaults 152 152 default: // too many arguments 153 throw ExceptionInst( cmd_error );153 throw ExceptionInst( cmd_error ); 154 154 } // choose 155 155 } catch( exception_t * ) { // catch any 156 156 exit | "Usage: " | argv[0] 157 158 159 157 | " [ games (>=0) | 'd' (default " | DefaultGames 158 | ") [ players (>=2) | 'd' (random " | MinNoPlayers | "-" | MaxNoPlayers 159 | ") [ seed (>0) | 'd' (random) ] ] ]"; 160 160 } // try 161 161 sout | numGames | numPlayers | seed;
Note: See TracChangeset
for help on using the changeset viewer.