示例#1
0
int String::myStrCmp(const char* str1, const char* str2)
{
  int len1 = myStrLen(str1);
  int len2 = myStrLen(str2);

  if(len1 < len2)
    return -1;
  else if(len1 > len2)
    return 1;
  else
  {
    int index = 0;
    while(str1[index] != '\0')
    {
      if(str1[index] < str2[index])
        return -1;
      else if (str1[index] > str2[index])
        return 1;
      else
        continue;
    }

    return 0;
  }
}
示例#2
0
String& String::operator+=(const String& rhs)
{
  length = myStrLen(data) + myStrLen(rhs.data);
  myStrCat(data, rhs.data);

  return *this;
}
示例#3
0
void String::myStrCat(char*& str1, const char* str2)
{
  char* result = new char[myStrLen(str1) + myStrLen(str2) + 1];

  int length = 0;
  for(int i = 0; str1[i] != '\0'; i++, length++)
    result[length] = str1[i];

  for(int i = 0; str2[i] != '\0'; i++, length++)
    result[length] = str2[i];

  result[length] = '\0';

  delete[] str1;
  str1 = result;
}
示例#4
0
void String::myStrCpy(char*& destination, char* source)
{
  int sourceLength = myStrLen(source);

  this->data = new char[sourceLength + 1];
  for(int i = 0; i <= sourceLength; i++)
    destination[i] = source[i];
}
示例#5
0
String& String::operator=(const String& other)
{
  if(this != &other)
  {
    if(data != NULL)
      delete[] data;

    myStrCpy(data, other.data);
    length = myStrLen(data);
  }
  return *this;
}
示例#6
0
size_t my_printf(const char* str)
{
  size_t rx_bytes;
  size_t total = 0; 
  size_t len = 0;
  int ret = 0;
  if (str == NULL)
  {
    return (-1);
  }

  len = myStrLen(str);
  do
  {
    ret = transmit(STDOUT, str + total, len - total, &rx_bytes);
    if ( (ret != 0) )
    {
      _terminate(ret);
    }
    total += rx_bytes;
  } while (total < len);

  return (total);
}
示例#7
0
int main(int argc, char *argv[]) {
    char *str = "MALO MORI QUAM FOEDARI";
    printf("The length of the string '%s' is: %d\n", str, myStrLen(str));
    return 0;
}
示例#8
0
int main(void)
{

  PlayerState player0, player1; //2 players
  int ret = 0;
  int curPlayer = 0;
  int bStarted = 0;
  int bEnded = 0;
  Command cmd;

  //initialize
  cmd.cmdNum = CMD_UNKNOWN;
  clearPlayerState(&player0);
  clearPlayerState(&player1);

  while(1)
  {
    if (curPlayer == 0)
    {
      cgc_write(STDOUT, P0_PROMPT, myStrLen(P0_PROMPT)); 
    }
    else
    {
      cgc_write(STDOUT, P1_PROMPT, myStrLen(P1_PROMPT)); 
    }

    ret = getCommand(&cmd);

    if (ret == 0) //if its okay
    {
      if (cmd.cmdNum == CMD_EXIT)
      {
        cgc_write(STDOUT, EXIT_MSG, myStrLen(EXIT_MSG)); 
        //break;
      }
      else if (cmd.cmdNum == CMD_NEW)
      {
        cmd.cmdNum = CMD_UNKNOWN;
        clearPlayerState(&player0);
        clearPlayerState(&player1);
        curPlayer = 0;
        bStarted = 0;
        bEnded = 0;
        continue;
      }
      else if (cmd.cmdNum == CMD_HELP)
      {
        cgc_write(STDOUT, HELP_MSG, myStrLen(HELP_MSG));
      }
      else if (bEnded)
      {
        cgc_write(STDOUT, GAME_ALREADY_ENDED_MSG, myStrLen(GAME_ALREADY_ENDED_MSG));
      }
      else if (cmd.cmdNum == CMD_MAP)
      { 
        if (curPlayer == 0)
        {
          dumpBoard(&player0);
        }
        else
        {
          dumpBoard(&player1);
        }
      }
      else if (cmd.cmdNum == CMD_START)
      {
        if (bStarted)
        {
          cgc_write(STDOUT, GAME_ALREADY_STARTED_MSG, myStrLen(GAME_ALREADY_STARTED_MSG));
        }
        else
        {
          bStarted = 1;
        }
      }
      else if (cmd.cmdNum == CMD_PLACE)
      {
        if (bStarted)
        {
          cgc_write(STDOUT, GAME_ALREADY_STARTED_MSG, myStrLen(GAME_ALREADY_STARTED_MSG));
        }
        else
        {
          if (curPlayer == 0)
          {
            ret = processPlace(&player0, &cmd);
          }
          else
          {
            ret = processPlace(&player1, &cmd);
          }

          if (ret == 0)
          {
            //swap players
            curPlayer = (curPlayer == 0) ? 1 : 0;
          }
          else
          {
            cgc_write(STDOUT, BAD_COMMAND_MSG, myStrLen(BAD_COMMAND_MSG));
          }
        }
      }
      else if (cmd.cmdNum == CMD_TARGET)
      {
        if (!bStarted)
        {
          cgc_write(STDOUT, GAME_NOT_STARTED_MSG, myStrLen(GAME_NOT_STARTED_MSG));
        }
        else
        {
          if (curPlayer == 0)
          {
            ret = processTarget(&player1, &cmd);
          }
          else
          {
            ret = processTarget(&player0, &cmd);
          }

          if (ret == 0)
          {
            //see if the current player won
            if (curPlayer == 0)
            {
              if (allShipsSunk(&player1))
              {
                cgc_write(STDOUT, YOU_WIN_MSG, myStrLen(YOU_WIN_MSG));
                bStarted = 0;
                bEnded = 1;
              }
            }
            else
            {
              if (allShipsSunk(&player0))
              {
                cgc_write(STDOUT, YOU_WIN_MSG, myStrLen(YOU_WIN_MSG));
                bStarted = 0;
                bEnded = 1;
              }
            }

            //swap players
            curPlayer = (curPlayer == 0) ? 1 : 0;
          }
          else
          {
            cgc_write(STDOUT, BAD_COMMAND_MSG, myStrLen(BAD_COMMAND_MSG));
          }
        }
      }
    }    
    else if (ret == -1)
    {
      cgc_write(STDOUT, BAD_COMMAND_MSG, myStrLen(BAD_COMMAND_MSG));
    }
    else if (ret == -2) //cgc_read error
    {
      cgc_write(STDOUT, READ_ERROR_MSG, myStrLen(READ_ERROR_MSG));
      break;
    }
  }

  return (0);
}
示例#9
0
int processTarget(PlayerState* ps, Command* cmd)
{
  if ( (ps == NULL) || (cmd == NULL) )
  {
    return (-1);
  }

  //check and convert the row and column fields
  if ( !isGoodRow(cmd->row) || !isGoodCol(cmd->col) )
  {
    return (-1);
  }

  //should I make this extremely explicit?
  if ( ps->board[ROW_TO_NUM(cmd->row)][COL_TO_NUM(cmd->col)] == SHIP2 )
  {
    ps->board[ROW_TO_NUM(cmd->row)][COL_TO_NUM(cmd->col)] = HIT_CELL;
    ps->ship2Life--;
    cgc_write(STDOUT, HIT_MSG, myStrLen(HIT_MSG));
    if (ps->ship2Life == 0)
    {
      cgc_write(STDOUT, SHIP2_SUNK_MSG, myStrLen(SHIP2_SUNK_MSG));
    }
  }
  else if ( ps->board[ROW_TO_NUM(cmd->row)][COL_TO_NUM(cmd->col)] == SHIP3 )
  {
    ps->board[ROW_TO_NUM(cmd->row)][COL_TO_NUM(cmd->col)] = HIT_CELL;
    ps->ship3Life--;
    cgc_write(STDOUT, HIT_MSG, myStrLen(HIT_MSG));
    if (ps->ship3Life == 0)
    {
      cgc_write(STDOUT, SHIP3_SUNK_MSG, myStrLen(SHIP3_SUNK_MSG));
    }
  }
  else if ( ps->board[ROW_TO_NUM(cmd->row)][COL_TO_NUM(cmd->col)] == SHIP4 )
  {
    ps->board[ROW_TO_NUM(cmd->row)][COL_TO_NUM(cmd->col)] = HIT_CELL;
    ps->ship4Life--;
    cgc_write(STDOUT, HIT_MSG, myStrLen(HIT_MSG));
    if (ps->ship4Life == 0)
    {
      cgc_write(STDOUT, SHIP4_SUNK_MSG, myStrLen(SHIP4_SUNK_MSG));
    }
  }
  else if ( ps->board[ROW_TO_NUM(cmd->row)][COL_TO_NUM(cmd->col)] == SHIP5 )
  {
    ps->board[ROW_TO_NUM(cmd->row)][COL_TO_NUM(cmd->col)] = HIT_CELL;
    ps->ship5Life--;
    cgc_write(STDOUT, HIT_MSG, myStrLen(HIT_MSG));
    if (ps->ship5Life == 0)
    {
      cgc_write(STDOUT, SHIP5_SUNK_MSG, myStrLen(SHIP5_SUNK_MSG));
    }
  }
  else
  {
    cgc_write(STDOUT, MISSED_MSG, myStrLen(MISSED_MSG));
  }

  return (0);
}
示例#10
0
String::String(char* data)
{
  myStrCpy(this->data, data);
  length = myStrLen(this->data);
}