Changeset f988834 for tests/exceptions/hotpotato_checked.cfa
- Timestamp:
- Jan 19, 2024, 2:44:41 AM (20 months ago)
- Branches:
- master
- Children:
- ac939461
- Parents:
- 59c8dff (diff), e8b3717 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tests/exceptions/hotpotato_checked.cfa
r59c8dff rf988834 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; … … 66 66 static void terminate( Player & player ) { // resume umpire 67 67 resume( player ); 68 69 68 checked_poll(); 69 sout | "THIS SHOULD NOT BE REACHED"; 70 70 } // terminate 71 71 … … 74 74 partner[RIGHT] = &rp; 75 75 resume( player ); // establish main as starter for termination 76 76 checked_poll(); 77 77 } // init 78 78 … … 82 82 83 83 void toss( Player & player ) { // tossed the potato 84 84 resume( player ); 85 85 checked_poll(); 86 86 } // toss … … 88 88 void main( Player & player ) with(player) { 89 89 try { 90 enable_ehm();// allow delivery of nonlocal exceptions91 suspend;// return immediately after establishing starter92 93 94 95 96 97 98 99 100 101 countdown( potato );// player is eliminated if countdown() returned true102 103 104 105 toss( *partner[ side ] );// random toss left/right106 107 90 enable_ehm(); // allow delivery of nonlocal exceptions 91 suspend; // return immediately after establishing starter 92 checked_poll(); 93 94 for ( ;; ) { 95 checked_poll(); 96 if ( partner[LEFT] == &player ) { // stop when only one player 97 sout | id | " wins the Match!"; 98 return; 99 } // exit 100 101 countdown( potato ); // player is eliminated if countdown() returned true 102 103 size_t side = prng( prng, 2 ); 104 sout | id | " -> " | nonl; 105 toss( *partner[ side ] ); // random toss left/right 106 } // for 107 disable_ehm(); 108 108 } catchResume( Terminate * v ) { 109 109 v->victim->partner[LEFT]->partner[RIGHT] = v->victim->partner[RIGHT]; // unlink node 110 110 v->victim->partner[RIGHT]->partner[LEFT] = v->victim->partner[LEFT]; 111 111 delete( v->victim ); 112 112 reset( potato ); 113 113 sout | "U " | nonl; // start new game … … 116 116 sout | "election"; 117 117 sout | " -> " | id | nonl; 118 if ( id > getId( umpire ) ) &umpire = &player; 119 120 disable_ehm();// disable ehm since we can't handle execption thrown in vote here and want to poll later118 if ( id > getId( umpire ) ) &umpire = &player; // set umpire to highest id so far 119 resumeAt( *partner[RIGHT], *election ); 120 disable_ehm(); // disable ehm since we can't handle execption thrown in vote here and want to poll later 121 121 vote( *partner[RIGHT] ); 122 enable_ehm();// enable after vote122 enable_ehm(); // enable after vote 123 123 } catchResume( Explode * ) { 124 125 126 127 id = -1;// remove from election128 129 vote( *partner[RIGHT] );// start election130 131 132 133 134 135 136 137 assert( false );// no return138 124 sout | id | " is eliminated"; 125 if ( &player == &umpire ) { 126 try { 127 id = -1; // remove from election 128 resumeAt( *partner[RIGHT], ExceptionInst( Election ) ); 129 vote( *partner[RIGHT] ); // start election 130 checked_poll(); 131 } catchResume( Election * election ) { 132 sout | " : umpire " | getId( umpire ); 133 } // try 134 } // if 135 resumeAt( umpire, ExceptionInst( Terminate, &player ) ); 136 terminate( umpire ); 137 assert( false ); // no return 138 } // try 139 139 } // main 140 140 … … 172 172 case 1: ; // defaults 173 173 default: // too many arguments 174 throw ExceptionInst( cmd_error );174 throw ExceptionInst( cmd_error ); 175 175 } // choose 176 176 } catch( exception_t * ) { // catch any 177 177 exit | "Usage: " | argv[0] 178 179 180 178 | " [ games (>=0) | 'd' (default " | DefaultGames 179 | ") [ players (>=2) | 'd' (random " | MinNoPlayers | "-" | MaxNoPlayers 180 | ") [ seed (>0) | 'd' (random) ] ] ]"; 181 181 } // try 182 182 sout | numGames | numPlayers | seed;
Note:
See TracChangeset
for help on using the changeset viewer.