Skip to content
Snippets Groups Projects
rps.c 4.58 KiB
Newer Older
antnh6@gmail.com's avatar
antnh6@gmail.com committed
#include <rps.h>
antnh6@gmail.com's avatar
antnh6@gmail.com committed
#include <bwio.h>
#include <types.h>
antnh6@gmail.com's avatar
antnh6@gmail.com committed

Tom Feng's avatar
Tom Feng committed
void GamePlay(RPSChoice Client1Move, RPSChoice Client2Move,*RPSMsg reply){
  if (Client1Move != None && Client2Move != None) {
    Diff = (int) Client1Move - (int)Client2Move;
    if (Diff == 0) {
      reply->Msg = Draw;
      bwprintf(COM2, "We have a Draw.");
      Reply(Client1, (void *)replyMsg, sizeof(ReplyMsg));
      Reply(Client2, (void *)replyMsg, sizeof(ReplyMsg));
    } 
    else if (Diff == 1 || Diff == -2) {
      bwprintf(COM2, "Client1 Wins.");
      reply->Msg = Win;
      Reply(Client1, (void *)replyMsg, sizeof(ReplyMsg));
      reply->Msg = Lose;
      Reply(Client2, (void *)replyMsg, sizeof(ReplyMsg));
    }
    else {
      bwprintf(COM2, "Client2 Wins.");
      reply->Msg = Lose;
      Reply(Client1, (void *)replyMsg, sizeof(ReplyMsg));
      reply->Msg = Win;
      Reply(Client2, (void *)replyMsg, sizeof(ReplyMsg));
    }
  }
}
antnh6@gmail.com's avatar
antnh6@gmail.com committed

Tom Feng's avatar
Tom Feng committed
int RPSServer(){
  char Name[] = "RPSServer";
  Result = RegisterAs(&Name);
  if(Result == FAILURE) {
    bwprintf(COM2, "%s: Cannot register %s\n\r",Name,Name);
    return FAILURE;
  }

  int MyID = MyTid();
  int MyIndex = GetMemoryIndex(MyID);
  Buffer SendQ;
  int QStorage[MAX_NUM_TD];
  SendQ.Storage = (void *)QStorage;
  SendQ.size = MAX_NUM_TD;
  SendQ.Lenght = 0;
  SendQ.Head = 0;
  SendQ.Tail = 0;
  AllSendQ[MyIndex] = &SendQ;
  
  int TaskID, Result, Client1, Client2, Diff;
  RPSMessage ReceiveMsg, ReplyMsg;
  RPSChoice Client1Move = None, Client2Move = None;
  
  int bOtherQuit = 0;
  FOREVER {
    Result = BufferFIFO(&SendQ,(void *)&TaskID);
    if(Result == 0){
      Result = Receive(TaskID, &ReceiveMsg, sizeof(ReceiveMsg));
      if(bOtherQuit != 0){
	ReplyMsg.Msg = OtherQuit;
	Reply(TaskID, &ReplyMsg, sizeof(ReplyMsg));
      }else{
	switch (ReceiveMsg.Msg) {
	  
	case Quit:
	  ReplyMsg.Msg = Bye;
	  OtherGuyQuit = (Client2 > 0);
	  Reply(TaskID,&ReplyMsg,sizeof(ReplyMsg));
	  // xor Client1 with TaskID, if same, result 0, then set to 0
	  Client1 = (Client1 ^ TaskID)? Client1: 0;
	  Client2 = (Client2 ^ TaskID)? Client2: 0;
	  break;
	case SignUp:
	  if (Client1 == 0 && Client2 != Client1) Client1 = TaskID;
	  else Client2 = TaskID;
	  ReplyMsg.Msg = LetsPlay;
	  Reply(TaskID, &ReplyMsg, sizeof(ReplyMsg));
	  break;
	  
	case Play:
	// only one player
	  if (Client1 == 0 || Client2 == 0) {
	    ReplyMsg.Msg = OnePlayer;
	    Reply(TaskID, &ReplyMsg, sizeof(ReplyMsg));
	    break;
	  }
	  if (Client1 == TaskID) Client1Move = ReceiveMsg.Choice;
	  else Client2Move = ReceiveMsg.Choice;
	  GamePlay(Client1Move, Client2Move, &ReplyMsg);
	  Client1Move = Client2Move = None;
	  break;
	  
	default:
	  break;
antnh6@gmail.com's avatar
antnh6@gmail.com committed
	}
Tom Feng's avatar
Tom Feng committed
      }
    }else Pass();
  }
  return SUCCESS;
Tom Feng's avatar
Tom Feng committed
int Player(int ID){
  char Name[] = "RPSClient0";
  Name[9] = ID;
  Result = RegisterAs(&Name);
  if(Result == FAILURE) {
    bwprintf(COM2, "%s: Cannot register %s\n\r",Name);
    return FAILURE;
  }
  
  char Input = 'U'; // for Undefined
  RPSMsgType RPSResult;
  int MyTID, ServerID, Result;
  RPSMessage SendMsg, ReplyMsg;
  
  MyTID = MyTid();
  bwprintf(COM2, "%s with TaskID(%d) is about to Play\n\r", Name, MyTID);
  ServerTID = WhoIs("RPSServer");
  if (ServerTID < 0) return FAILURE;
  
  SendMsg.Msg = SignUp;
  Result = Send(ServerTID, &SendMsg, sizeof(SendMsg), &ReplyMsg, sizeof(ReplyMsg));
  
  if (Result != sizeof(ReplyMsg)) {
    bwprintf(COM2, "Sign Up Request to Server did not go through.");
    return Result;
  }
  
  //if (ReplyMsg.MsgType == ServerFull) //TODOTODOTODOTODO
    
    FOREVER {
      bwprintf(COM2, "Please enter r/R for Rock, p/P for Paper, s/S for Scissors.\n\r");
      while (bwgetc(Input)) {
	Input = bwgetc(COM2);
	if (Input == 'r' || Input == 'R') {
	  SendMsg.Choice = Rock;
	  break;
antnh6@gmail.com's avatar
antnh6@gmail.com committed
	}
Tom Feng's avatar
Tom Feng committed
	else if (Input == 'p' || Input == 'P') {
	  SendMsg.Choice = Paper;
	  break;
antnh6@gmail.com's avatar
antnh6@gmail.com committed
	}
Tom Feng's avatar
Tom Feng committed
	else if (Input == 's' || Input == 'S') {
	  SendMsg.Choice = Scissors;
	  break;
	}else if(Input == 'q' || Input == 'Q'){
	  SendMsg.Msg = Quit;
antnh6@gmail.com's avatar
antnh6@gmail.com committed
	}
Tom Feng's avatar
Tom Feng committed
	else {
	  Input = 'U';
	  bwprintf(COM2, "Please enter r/R for Rock, p/P for Paper, s/S for Scissors.\n\r");
antnh6@gmail.com's avatar
antnh6@gmail.com committed
	}
Tom Feng's avatar
Tom Feng committed
      }
      
      SendMsg.Msg = Play;
      Result = Send(ServerTID, &SendMsg, sizeof(SendMsg), &ReplyMsg, sizeof(ReplyMsg));
      if (Result != sizeof(ReplyMsg)) {
	if(SendMsg.Msg == Quit){
	  bwprintf(COM2, "Request for QUIT from %s did not go through gracefully.", Name);
	}else{
	  bwprintf(COM2, "%s: Game obstructed!",Name);
antnh6@gmail.com's avatar
antnh6@gmail.com committed
	}
Tom Feng's avatar
Tom Feng committed
	return FAILURE;
      }else{
	if(SendMsg.Msg == Quit){
	  bwprintf(COM2, "%s exitting...",Name);
	  GameResult = ReplyMsg.Msg;
antnh6@gmail.com's avatar
antnh6@gmail.com committed
	}
Tom Feng's avatar
Tom Feng committed
      }
    }
  return SUCCESS;
}
Tom Feng's avatar
Tom Feng committed
int RPSClient1(){
  Player(1);
  Exit();
}
Tom Feng's avatar
Tom Feng committed
int RPSClient2(){
  Player(2);
  Exit();