#include // Defines strlen(). #include "player.h" // Definition of player task. #include using namespace std; //-------------------------------------------------------------------------------------------------------------------- // Passes the message to the next task in the chain of tasks. //-------------------------------------------------------------------------------------------------------------------- void Player::SendMessage( char *m ) { message = m; gameOver += 1; // Message has gone to another player, so note it. osacquire acq( cout ); cout << "Player " << id << " received message '"; if ( message != NULL ) { cout << message; perturbMessage(); cout << "' and changed it to '" << message; } else { cout << "NULL"; } cout << "' on pass " << gameOver << endl; } //-------------------------------------------------------------------------------------------------------------------- // Blocks itself waiting for messages until the game is over. //-------------------------------------------------------------------------------------------------------------------- void Player::main() { // Wait until get identity of next task in the chain. osacquire( cout ) << "Player " << id << " about to block waiting for initialization info" << endl; _Accept( SetPlayer ); osacquire( cout ) << "Player " << id << " awake after waiting for initialization info" << endl; for ( ; gameOver <= 11 ; ) { // Is the game over yet? osacquire( cout ) << "Player " << id << " about to block waiting for message" << endl; _Accept(SendMessage); // Receive and perturb the message. Pass it along if not done. osacquire( cout ) << "Player " << id << " about to block waiting for message" << endl; if ( message != NULL && gameOver <= 11 ) { osacquire( cout ) << "Player " << id << " send message to next player" << endl; nextPlayer->SendMessage(message); osacquire( cout ) << "Player " << id << " sent message to next player" << endl; } } if ( id < 4 ) { // Wake up blocked tasks. osacquire( cout ) << "Player " << id << " send empty message to next player" << endl; nextPlayer->SendMessage( NULL ); osacquire( cout ) << "Player " << id << " sent empty message to next player" << endl; } osacquire( cout ) << "Player " << id << " terminating" << endl; } //-------------------------------------------------------------------------------------------------------------------- // Randomly perturbs the message. //-------------------------------------------------------------------------------------------------------------------- void Player::perturbMessage() { const char lowEndRange = ' '; // Low end of valid character range for message (ASCII 32). const char highEndRange = '~'; // High end of valid character range for message (ASCII 126). const char amountPerturb = highEndRange-lowEndRange; const int indexRange = strlen(message); // Number of characters in the string. int index; // Index of character to change. if ( indexRange > 0 ) { index = random() % indexRange; message[index] = (char) (lowEndRange + (random() % amountPerturb) + 1); } // if } // Player::perturbMessage