home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / LCNOW2.ZIP / EXAMPLES / FARMER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-02  |  5.9 KB  |  261 lines

  1. /*
  2.  * F A R M E R
  3.  *
  4.  * The "farmer crossing a river" problem
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <conio.h>
  10.  
  11. #define BEL     7
  12. #define K_ESC   27
  13. #define NBYTES  80
  14.  
  15. /*
  16.  * Player data structure (bitfield) definition.
  17.  */
  18. struct player_st {
  19.        unsigned farmer : 1;
  20.        unsigned fox : 1;
  21.        unsigned hen : 1;
  22.        unsigned corn : 1;
  23. };
  24.  
  25. struct player_st Player;        /* "generic" bitfield variable */
  26.  
  27. /*
  28.  * Function prototypes
  29.  */
  30. void Instruct(void);
  31. int GetMove(int);
  32. void DoMove(int);
  33. int CheckMap(int);
  34. int MadeIt(void);
  35. void YouLose(void);
  36. void PrintMap(void);
  37.  
  38.  
  39. int
  40. main()
  41. {
  42.        int bank;               /* 0 means farmer is on the left bank */
  43.        int move;               /* key code for the requested move */
  44.        int trips;              /* number of trips across the river */
  45.        char reply[NBYTES];     /* user response */
  46.  
  47.        /*
  48.         * Set initial conditions.  All items are on the
  49.         * west bank and no trips have been made.
  50.         */
  51.        Player.farmer = Player.fox = Player.hen = Player.corn = 0;
  52.        bank = Player.farmer;
  53.        trips = 0;
  54.  
  55.        printf("\n=========== FARMER ===========\n");
  56.        printf("Press Esc at any time to quit.\n\n");
  57.        printf("Do you need instructions? (Enter Y or N): ");
  58.        gets(reply);
  59.        if (reply[0] == 'y' || reply[0] == 'Y' || reply[0] == '\0')
  60.                Instruct();
  61.        PrintMap();
  62.        while (1) {
  63.                move = GetMove(bank);
  64.                DoMove(move);
  65.                ++trips;
  66.                bank = Player.farmer;
  67.                PrintMap();
  68.                if (CheckMap(bank))
  69.                        YouLose();
  70.                if (MadeIt())
  71.                        break;
  72.        }
  73.        printf("Congratulations.  You made it safely!\n");
  74.        printf("The number of trips was %d\n", trips);
  75.  
  76.        return (0);
  77. }
  78.  
  79.  
  80. /*
  81.  * Instruct()
  82.  *
  83.  * Display the rules of the game.
  84.  */
  85.  
  86. void
  87. Instruct()
  88. {
  89.        puts("A farmer must cross a river in a boat.");
  90.        puts("He has a fox, a hen, and a bushel of corn.");
  91.        puts("The farmer can only take one thing at a");
  92.        puts("time in the boat.  The fox can't be left");
  93.        puts("alone with the hen because he'll eat it.");
  94.        puts("Likewise, the hen can't be left with the corn.");
  95.        puts("When the farmer is present, the animals are");
  96.        puts("well behaved.  You are the farmer.  Attempt");
  97.        puts("to get from the west bank of the river to");
  98.        puts("the east bank with your baggage in tow while");
  99.        puts("making the least number of trips.");
  100. }
  101.  
  102.  
  103. int
  104. GetMove(bank)
  105. int bank;
  106. {
  107.        int key;
  108.  
  109.        /*
  110.         * Prompt the user with only available commands.
  111.         */
  112.        printf("\nCommand: A(lone) ");
  113.        if (Player.fox == bank)
  114.                printf("F(ox) ");
  115.        if (Player.hen == bank)
  116.                printf("H(en) ");
  117.        if (Player.corn == bank)
  118.                printf("C(orn) ");
  119.        printf(": ");
  120.        while (1) {
  121.                key = toupper(getch());
  122.                if (key == 'A')
  123.                        break;
  124.                else if (key == 'F' && Player.fox == bank)
  125.                        break;
  126.                else if (key == 'H' && Player.hen == bank)
  127.                        break;
  128.                else if (key == 'C' && Player.corn == bank)
  129.                        break;
  130.                else if (key == K_ESC) {
  131.                        putchar('\n');
  132.                        exit (0);
  133.                }
  134.                else
  135.                        putchar(BEL);   /* bad command */
  136.        }
  137.        putchar('\n');
  138.        return (key);
  139. }
  140.  
  141.  
  142. void
  143. DoMove(move)
  144. int move;
  145. {
  146.        switch (move) {
  147.        case 'A':
  148.                break;
  149.        case 'F':
  150.                Player.fox = !Player.fox;
  151.                break;
  152.        case 'H':
  153.                Player.hen = !Player.hen;
  154.                break;
  155.        case 'C':
  156.                Player.corn = !Player.corn;
  157.                break;
  158.        }
  159.        Player.farmer = !Player.farmer;
  160. }
  161.  
  162.  
  163. /*
  164.  * CheckMap()
  165.  *
  166.  * Verify that no hostile items are left alone.
  167.  * Return 1 if they are or 0 if not.
  168.  */
  169. int
  170. CheckMap(bank)
  171. int bank;
  172. {
  173.        int status = 0;
  174.  
  175.        if (Player.fox != bank && Player.hen != bank)
  176.                status = 1;
  177.        if (Player.hen != bank && Player.corn != bank)
  178.                status = 1;
  179.  
  180.  
  181.        return (status);
  182. }
  183.  
  184.  
  185. /*
  186.  * PrintMap()
  187.  *
  188.  * Display the current map showing the positions
  189.  * of the farmer and the other items.
  190.  */
  191. void
  192. PrintMap()
  193. {
  194.        char wc, ec;
  195.  
  196.        /* the farmer */
  197.        wc = ec = ' ';
  198.        if (Player.farmer)
  199.                ec = 'F';
  200.        else
  201.                wc = 'F';
  202.        printf("\n%c ^^^^^ %c\n", wc, ec);
  203.  
  204.        /* the fox */
  205.        wc = ec = ' ';
  206.        if (Player.fox)
  207.                ec = 'f';
  208.        else
  209.                wc = 'f';
  210.        printf("%c ^^^^^ %c\n", wc, ec);
  211.  
  212.        /* the hen */
  213.        wc = ec = ' ';
  214.        if (Player.hen)
  215.                ec = 'h';
  216.        else
  217.                wc = 'h';
  218.        printf("%c ^^^^^ %c\n", wc, ec);
  219.  
  220.        /* the corn */
  221.        wc = ec = ' ';
  222.        if (Player.corn)
  223.                ec = 'c';
  224.        else
  225.                wc = 'c';
  226.        printf("%c ^^^^^ %c\n", wc, ec);
  227. }
  228.  
  229.  
  230. /*
  231.  * MadeIt()
  232.  *
  233.  * Determine whether all items have been safely
  234.  * transported to the east bank of the river.
  235.  */
  236. int
  237. MadeIt()
  238. {
  239.        int status;
  240.  
  241.        status = 0;
  242.        if (Player.farmer && Player.fox &&
  243.            Player.hen && Player.corn)
  244.                status = 1;
  245.  
  246.        return (status);
  247. }
  248.  
  249.  
  250. void
  251. YouLose()
  252. {
  253.        printf("Sorry, you lose.  ");
  254.        if (Player.fox == Player.hen)
  255.                printf("The fox ate the hen.\n");
  256.        else if (Player.hen == Player.corn)
  257.                printf("The hen ate the corn.\n");
  258.  
  259.        exit (1);
  260. }
  261.