Guest User

Main1

a guest
Jun 15th, 2010
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #include "command.h"
  5.  
  6. #define SIZE 5
  7.  
  8. int check_init(command, int *, char [ ], char [ ][21], int);
  9. void err_message(int);
  10.  
  11. int main( ) {
  12.    char data[SIZE][61] = {
  13.                            "go;north;south;east;west;start;left;right;home",
  14.                            "look",
  15.                            "pick up;gold;platinum;crystal;magic sword",
  16.                            "drop;gold;platinum;crystal;magic sword",
  17.                            "attack;crystal;magic sword"
  18.                          };
  19.  
  20.    char keywords[SIZE][41] = { "go", "look", "pick up", "drop", "attack" };
  21.  
  22.    char modifiers[SIZE][SIZE][21] =
  23.       { { "north",   "south",       "east",    "west",       "start"  },
  24.         { "",        "",            "",        "",            ""      },
  25.         { "gold",    "platinum",    "crystal", "magic sword", ""      },
  26.         { "gold",    "platinum",    "crystal", "magic sword", ""      },
  27.         { "crystal", "magic sword", "",        "",            ""      }
  28.       };
  29.  
  30.    char phrases[SIZE*3][81] = {
  31.       "  Hello Robot! Please GO \t\t  over   to the \n other   side.",
  32.       "Hello Robot  can you     \n\n\t do    something?     ",
  33.       "Robot \t go over to the Right, then over to the left, then start",
  34.  
  35.       "Look over to the \n\n \t   \t  right",
  36.       "Robot, can you see what I see?",
  37.       "Did you see that? LOOK over   \n there! No not there, LOOK here!\n",
  38.  
  39.       "Can you pick-up the platinum    gold   and crystal  over there?",
  40.       "Over there, can you Pick Up the \n\n  \t GOLD   \n?",
  41.       "OK, I will get the crystal gold platinum and pick UP the Magic Sword",
  42.  
  43.       "  drop the crystal     ",
  44.       "Robot, please drop the \n\n  \t\t PLATINUM   ",
  45.       "Look can you see any gold platinum or crystal ?",
  46.  
  47.       "   Attack with the crystal and the magic sword",
  48.       "You must look around, move up then attack with the MAGIC sword",
  49.       "ATTACK ATTACK ATTACK! First with a crystal, then with a Magic Sword!"
  50.    };
  51.  
  52.    int rvalues[SIZE*3] = { 1, 0, 6,  1, 0, 1,  0, 2, 5,  4, 3, 0,   2, 3, 1 };
  53.  
  54.    int modc[SIZE] = { 5, 0, 4, 4, 2 };
  55.  
  56.    class command robots[SIZE];
  57.  
  58.    char t_data[61];
  59.    int i, j, valid=1, rc, count=0, pos=0;
  60.  
  61.    for(i=0; i<SIZE && valid; i++) {
  62.       if(i==0) {
  63.          robots[i].init( );  // initializing with keyword "go", and modifiers
  64.                               // "north", "south", "east", "west", and "start"
  65.       }
  66.       else {
  67.          strcpy(t_data, data[i]);
  68.          robots[i].init(t_data);
  69.       }
  70.       rc = check_init(robots[i], &count, keywords[i], modifiers[i], modc[i]);
  71.  
  72.       if(rc) {
  73.          valid = 0;
  74.       }
  75.       else {
  76.          printf("Passed test %d...\n", count);
  77.          printf("Press the enter key to continue...");
  78.          getchar( );
  79.       }
  80.    }
  81.  
  82.    if(valid) {
  83.       count++;
  84.       for(i=0; i<SIZE && valid; i++) {
  85.          for(j=0; j<3 && valid; j++) {
  86.             rc = robots[i].valid_command(phrases[pos]);
  87.             if(rc != rvalues[pos]) {
  88.                printf("Program failed when testing the valid_command( )");
  89.                printf(" function...\n");
  90.                printf("Phrase: '%s'\n", phrases[pos]);
  91.                printf("Your return value-----> %d\n", rc);
  92.                printf("Actual return value---> %d\n", rvalues[pos]);
  93.                valid = 0;
  94.             }
  95.             else {
  96.                printf("Passed test %d...\n", count);
  97.                printf("Press the enter key to continue...");
  98.                getchar( );
  99.             }
  100.             count++;
  101.             pos++;
  102.          }
  103.       }
  104.    }
  105.  
  106.    if(valid) {
  107.       printf("\nCongratualtions!!! You passed all tests.\n");
  108.       printf("You may hand in your assignment.\n");
  109.    }
  110.    else {
  111.       err_message(count);
  112.    }
  113.    return 0;
  114. }
  115.  
  116. int check_init(command x, int *p, char key[ ], char mod[ ][21], int mc) {
  117.    int rc, num, invalid;
  118.    char keyword[41], modifier[21];
  119.  
  120.    x.get_keyword(keyword);
  121.    invalid = strcmp(key, keyword);
  122.  
  123.    if(invalid) {
  124.       rc = 1;
  125.    }
  126.  
  127.    if(!invalid) {
  128.       for(num=0; num<SIZE && !invalid; num++) {
  129.          x.get_modifier(modifier, num);
  130.          invalid = strcmp(modifier, mod[num]);
  131.          if(invalid) {
  132.             rc = 2 + num;
  133.          }
  134.       }
  135.    }
  136.    if(!invalid) {
  137.       rc = x.modifier_count( );
  138.       if(rc != mc) {
  139.          rc = 7;
  140.          invalid = 1;
  141.       }
  142.    }
  143.    if(invalid) {
  144.       printf("Program failed when testing the init(%s) function\n",
  145.              *p == 0 ? " " : "char data[ ]");
  146.       printf("Failed on test ");
  147.       switch(rc) {
  148.          case 1:
  149.             printf("%d on keyword %d '%s'\n", *p+1, *p+1, key);
  150.             printf("Your keyword-----> '%s'\n", keyword);
  151.             printf("Actual keyword---> '%s'\n", key);
  152.             break;
  153.          case 2:
  154.          case 3:
  155.          case 4:
  156.          case 5:
  157.          case 6:
  158.             printf("%d on modifier %d '%s'\n", *p+1, num, mod[num-1]);
  159.             printf("Your modifier-----> '%s'\n", modifier);
  160.             printf("Actual modifier---> '%s'\n", mod[num-1]);
  161.             break;
  162.          case 7:
  163.             printf("%d on modifier count...\n", *p+1);
  164.             printf("Your modifier count-----> %d\n", x.modifier_count( ));
  165.             printf("Actual modifier count---> %d\n", mc);
  166.             break;
  167.       }
  168.    }
  169.    (*p)++;
  170.    return invalid; // returns (0) true or (1) false
  171. }
  172.  
  173. void err_message(int total) {
  174.    printf("\nYour program passed %d test(s)...\n", total);
  175.    switch(total) {
  176.       case 0:
  177.       case 1:
  178.       case 2:
  179.       case 3:
  180.       case 4:
  181.       case 5:
  182.          printf("Your program still needs considerable work!\n");
  183.          break;
  184.       case 6:
  185.       case 7:
  186.       case 8:
  187.       case 9:
  188.       case 10:
  189.       case 11:
  190.       case 12:
  191.       case 13:
  192.       case 14:
  193.       case 15:
  194.          printf("Your program still needs some work!\n");
  195.          break;
  196.       case 16:
  197.       case 17:
  198.       case 18:
  199.       case 19:
  200.          printf("Your program is almost complete!\n");
  201.          break;
  202.    }
  203.    printf("Keep at it!\n\n");
  204. }
Advertisement
Add Comment
Please, Sign In to add comment