Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #include "omnetpp.h"
  5.  
  6. // derive Game from cSimpleModule
  7. class Router : public cSimpleModule
  8. {
  9. private:
  10. int num_sticks;
  11. int player_to_move;
  12.  
  13. protected:
  14. virtual void initialize();
  15. virtual void handleMessage(cMessage *wiad);
  16. virtual void finish();
  17. };
  18.  
  19. Define_Module(Router);
  20.  
  21. void Router::initialize()
  22. {
  23. // read parameter values from omnetpp.ini or .ned file
  24. num_sticks = par("num_sticks"); // additionally, you can use methods:
  25. // .boolValue(), .longValue(), .doubleValue(), .stringValue()
  26. player_to_move = par("first_move");
  27.  
  28.  
  29.  
  30. EV << "\nNUMBER OF STICKS: " << num_sticks;
  31. EV << "\nPLAYER "<<player_to_move << " BEGINS...\n\n";
  32.  
  33. char msgname[32];
  34. sprintf(msgname," %d sticks left",num_sticks);
  35. cMessage *msg = new cMessage(msgname);
  36. msg->setKind(num_sticks);
  37. if (player_to_move == 1)
  38. send(msg, "to_player1");
  39. else
  40. send(msg, "to_player2");
  41. }
  42.  
  43.  
  44. void Router::handleMessage(cMessage *msgin)
  45. {
  46. int recipient = msgin->getKind();
  47.  
  48. delete msgin;
  49.  
  50.  
  51. //num_sticks -= sticks_taken;
  52.  
  53. EV << "Player took " << recipient << " stick(s).\n";
  54. //EV << "Sticks left: " << num_sticks << "\n";
  55.  
  56. //if(num_sticks>0)
  57. {
  58. // switch to the other player
  59. //player_to_move = 3 - player_to_move;
  60.  
  61. char msgname[32];
  62. sprintf(msgname,"Message to %d", recipient);
  63. cMessage *msg = new cMessage(msgname);
  64. msg->setKind(num_sticks);
  65. if (recipient == 1)
  66. send(msg, "to_workstation1");
  67. else
  68. send(msg, "to_workstation2");
  69. }
  70. }
  71.  
  72.  
  73. void Router::finish()
  74. {
  75. //EV << "\nPLAYER " << player_to_move << " LOSES!!!\n";
  76. EV << "The End. \n";
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement