Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include "omnetpp.h"
- // derive Game from cSimpleModule
- class Router : public cSimpleModule
- {
- private:
- int num_sticks;
- int player_to_move;
- protected:
- virtual void initialize();
- virtual void handleMessage(cMessage *wiad);
- virtual void finish();
- };
- Define_Module(Router);
- void Router::initialize()
- {
- // read parameter values from omnetpp.ini or .ned file
- num_sticks = par("num_sticks"); // additionally, you can use methods:
- // .boolValue(), .longValue(), .doubleValue(), .stringValue()
- player_to_move = par("first_move");
- EV << "\nNUMBER OF STICKS: " << num_sticks;
- EV << "\nPLAYER "<<player_to_move << " BEGINS...\n\n";
- char msgname[32];
- sprintf(msgname," %d sticks left",num_sticks);
- cMessage *msg = new cMessage(msgname);
- msg->setKind(num_sticks);
- if (player_to_move == 1)
- send(msg, "to_player1");
- else
- send(msg, "to_player2");
- }
- void Router::handleMessage(cMessage *msgin)
- {
- int recipient = msgin->getKind();
- delete msgin;
- //num_sticks -= sticks_taken;
- EV << "Player took " << recipient << " stick(s).\n";
- //EV << "Sticks left: " << num_sticks << "\n";
- //if(num_sticks>0)
- {
- // switch to the other player
- //player_to_move = 3 - player_to_move;
- char msgname[32];
- sprintf(msgname,"Message to %d", recipient);
- cMessage *msg = new cMessage(msgname);
- msg->setKind(num_sticks);
- if (recipient == 1)
- send(msg, "to_workstation1");
- else
- send(msg, "to_workstation2");
- }
- }
- void Router::finish()
- {
- //EV << "\nPLAYER " << player_to_move << " LOSES!!!\n";
- EV << "The End. \n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement