Ignore:
Timestamp:
Jan 6, 2024, 8:14:15 AM (6 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
4d689e2
Parents:
40002c5
Message:

formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/exceptions/hotpotato.cfa

    r40002c5 rf0c9c9b  
    55
    66struct Potato {
    7         PRNG & prng;
    8         unsigned int deadline;                                                          // when timer goes off
    9         unsigned int timer;                                                                     // up counter to deadline
     7        PRNG & prng;
     8        unsigned int deadline;                                                          // when timer goes off
     9        unsigned int timer;                                                                     // up counter to deadline
    1010}; // Potato
    1111
     
    1616        &potato.prng = &prng;
    1717        reset( potato, maxTicks );
    18 } // Potato
     18                } // Potato
    1919
    2020coroutine Player {
    21         PRNG & prng;
    22         int id;                                                                                         // player identity
    23         Potato & potato;                                                                        // potato being tossed
    24         Player * partner[2];                                                            // left and right player
     21        PRNG & prng;
     22        int id;                                                                                         // player identity
     23        Potato & potato;                                                                        // potato being tossed
     24        Player * partner[2];                                                            // left and right player
    2525}; // Player
    2626
     
    2929        player.id = id;
    3030        &player.potato = &potato;
    31 } // Player
     31                } // Player
    3232
    3333Player & umpire;
     
    3939
    4040void reset( Potato & potato, unsigned int maxTicks ) with(potato) {
    41   if ( maxTicks < 2 ) abort( "Hot Potato initialized with less than 2 ticks" ); // optional
     41        if ( maxTicks < 2 ) abort( "Hot Potato initialized with less than 2 ticks" ); // optional
    4242        deadline = prng( prng, 1, maxTicks );
    4343        timer = 0;
     
    6060enum { LEFT = 0, RIGHT = 1 };
    6161
    62 static void vote( Player & player, Election & election ) {                                      // cause partner to vote
    63     resumeAt( player, election );
     62static void vote( Player & player, Election & election ) { // cause partner to vote
     63        resumeAt( player, election );
    6464        resume( player );
    6565} // vote
     
    7676
    7777void main( Player & player ) with(player) {
    78     suspend;                                                                    // return immediately after establishing starter
     78        suspend;                                                                                        // return immediately after establishing starter
    7979        try {
    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
     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
    9494        } catchResume( Terminate * v ) {
    9595                v->victim->partner[LEFT]->partner[RIGHT] = v->victim->partner[RIGHT]; // unlink node
    9696                v->victim->partner[RIGHT]->partner[LEFT] = v->victim->partner[LEFT];
    97         delete( v->victim );
     97                delete( v->victim );
    9898                reset( potato );
    9999                sout | "U " | nonl;                                                             // start new game
     
    102102                sout | "election";
    103103                sout | " -> " | id | nonl;
    104                 if ( id > getId( umpire ) ) &umpire = &player; // set umpire to highest id so far
     104                if ( id > getId( umpire ) ) &umpire = &player;  // set umpire to highest id so far
    105105                vote( *partner[RIGHT], *election );
    106106        } catchResume ( Explode * ) {
    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
     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
    118118} // main
    119119
     
    151151                  case 1: ;                                                                             // defaults
    152152                  default:                                                                              // too many arguments
    153                         throw ExceptionInst( cmd_error );
     153                          throw ExceptionInst( cmd_error );
    154154                } // choose
    155155        } catch( exception_t * ) {                                                      // catch any
    156156                exit | "Usage: " | argv[0]
    157                          | " [ games (>=0) | 'd' (default " | DefaultGames
    158                          | ") [ players (>=2) | 'd' (random " | MinNoPlayers | "-" | MaxNoPlayers
    159                          | ") [ seed (>0) | 'd' (random) ] ] ]";
     157                        | " [ games (>=0) | 'd' (default " | DefaultGames
     158                        | ") [ players (>=2) | 'd' (random " | MinNoPlayers | "-" | MaxNoPlayers
     159                        | ") [ seed (>0) | 'd' (random) ] ] ]";
    160160        } // try
    161161        sout | numGames | numPlayers | seed;
Note: See TracChangeset for help on using the changeset viewer.