upsidedown

Missionary and cannibals

Aug 8th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. // missionary and cannibals.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. int ml=3, cl=3, mr=0,cr=0; //initilize everythng on left
  9. int boat_pos=0; //0=left, 1=right
  10.  
  11. void display()
  12. {
  13.     cout<<"\nThe Position is   ";
  14.     for (int i=0;i<ml;i++)
  15.         cout<<"M";
  16.     for (int i=0;i<cl;i++)
  17.         cout<<"C";
  18.     if(boat_pos) cout<<"|      \\__/|" ;
  19.     else cout<<"|\\__/       |" ;
  20.     for (int i=0;i<mr;i++)
  21.         cout<<"M";
  22.     for (int i=0;i<cr;i++)
  23.         cout<<"C";
  24. }
  25.  
  26. int check()
  27. {
  28.     if(ml<cl && ml!=0)
  29.         {
  30.             cout<<"\nMore cannibals on left! \nGAME OVER";
  31.             system("pause");
  32.             exit(1);
  33.         }
  34.     if(mr<cr && mr!=0)
  35.     {
  36.         cout<<"\nMore cannibals on right! \nGAME OVER";
  37.         system("pause");
  38.         exit(1);
  39.     }
  40.  
  41.     if(mr==3 && cr==3)
  42.     {
  43.         cout<<"\nYou Win!!!";
  44.         system("pause");
  45.         exit(1);
  46.     }
  47.  
  48.     return 1;  //returns 1 if none of the conditions is true
  49. }
  50.  
  51.  
  52. void move()
  53. {
  54.     int p1, p2,ppl; //person1 and person2
  55.     cout<<"\nEnter the first person in the boat: 1. Missionary  2.cannibal";
  56.     cin>>p1;
  57.     cout<<"enter the second person in the boat 1. Missionary  2.cannibal 3:Empty";
  58.     cin>>p2;
  59.    
  60.     ppl=p1*10+p2;
  61.  
  62.     if(boat_pos) //boat to right
  63.     {
  64.       switch(ppl)
  65.       {
  66.       case 10:
  67.           if(mr<1)
  68.               cout<<"\nerror. no missionaries of right";
  69.           else
  70.         { ml+=1;  mr--;  }
  71.         break;
  72.  
  73.       case 11:
  74.           if(mr<2)
  75.               cout<<"\nerror. not enough people";
  76.           else
  77.           { ml+=2; mr-=2;}
  78.           break;
  79.       case 20:
  80.           if(cr<1)
  81.               cout<<"\nerror. ot enough people";
  82.           else
  83.           {cl+=1; cr--;}
  84.         break;
  85.  
  86.       case 21:
  87.       case 12:
  88.          
  89.           if(cr<1 || mr<1)
  90.               cout<<"\nerror. not enough people";
  91.           else
  92.           { cl+=1; ml+=1; cr--; mr--;}
  93.         break;
  94.  
  95.       case 22:
  96.             if(cr<2)
  97.               cout<<"\nerror. not enough people";
  98.           else
  99.           {cl+=2; cr-=2;}
  100.           break;
  101.  
  102.       default:
  103.           cout<<"\nWRONG CH";
  104.       }
  105.  
  106.  
  107.     }
  108.  
  109.  
  110.     else //boat to left
  111.     {
  112.       switch(ppl)
  113.       {
  114.       case 10:
  115.           if(ml<1)
  116.               cout<<"\nerror. no missionaries of right";
  117.           else
  118.           {mr+=1; ml--;}
  119.         break;
  120.  
  121.       case 11:
  122.           if(ml<2)
  123.               cout<<"\nerror. not enough people";
  124.           else
  125.           { mr+=2; ml-=2;}
  126.           break;
  127.       case 20:
  128.           if(cl<1)
  129.               cout<<"\nerror. ot enough people";
  130.           else
  131.           { cr+=1; cl--;}
  132.         break;
  133.  
  134.       case 21:
  135.       case 12:
  136.          
  137.           if(cl<1 || ml<1)
  138.               cout<<"\nerror. not enough people";
  139.           else
  140.           { cr+=1; mr+=1; ml-=1;cl-=1;}
  141.         break;
  142.  
  143.       case 22:
  144.             if(cl<2)
  145.               cout<<"\nerror. not enough people";
  146.           else
  147.           {  cr+=2; cl-=2;}
  148.           break;
  149.  
  150.       default:
  151.           cout<<"\nWRONG CHOICE";
  152.       }
  153.  
  154.  
  155.     }
  156. }
  157.  
  158.  
  159.    
  160.  
  161.  
  162. int _tmain(int argc, _TCHAR* argv[])
  163. {
  164.     cout<<"\nMissionaries and cannibals Game:\n";
  165.     int status=1;
  166.        
  167.     cout<<"\n\n";
  168.     while(status)
  169.     {
  170.         display();
  171.         move();
  172.         cout<<"\n\n";
  173.         status=check();
  174.         boat_pos=!boat_pos;
  175.     }
  176.  
  177.    
  178.     return 0;
  179. }
  180.  
  181.  
  182. /*
  183.  
  184. Missionaries and cannibals Game:
  185.  
  186.  
  187.  
  188. The Position is   MMMCCC|\__/       |
  189. Enter the first person in the boat: 1. Missionary  2.cannibal2
  190. enter the second person in the boat 1. Missionary  2.cannibal 3:Empty2
  191.  
  192.  
  193.  
  194. The Position is   MMMC|      \__/|CC
  195. Enter the first person in the boat: 1. Missionary  2.cannibal2
  196. enter the second person in the boat 1. Missionary  2.cannibal 3:Empty0
  197.  
  198.  
  199.  
  200. The Position is   MMMCC|\__/       |C
  201. Enter the first person in the boat: 1. Missionary  2.cannibal2
  202. enter the second person in the boat 1. Missionary  2.cannibal 3:Empty2
  203.  
  204.  
  205.  
  206. The Position is   MMM|      \__/|CCC
  207. Enter the first person in the boat: 1. Missionary  2.cannibal2
  208. enter the second person in the boat 1. Missionary  2.cannibal 3:Empty0
  209.  
  210.  
  211.  
  212. The Position is   MMMC|\__/       |CC
  213. Enter the first person in the boat: 1. Missionary  2.cannibal1
  214. enter the second person in the boat 1. Missionary  2.cannibal 3:Empty1
  215.  
  216.  
  217.  
  218. The Position is   MC|      \__/|MMCC
  219. Enter the first person in the boat: 1. Missionary  2.cannibal1
  220. enter the second person in the boat 1. Missionary  2.cannibal 3:Empty2
  221.  
  222.  
  223.  
  224. The Position is   MMCC|\__/       |MC
  225. Enter the first person in the boat: 1. Missionary  2.cannibal1
  226. enter the second person in the boat 1. Missionary  2.cannibal 3:Empty1
  227.  
  228.  
  229.  
  230. The Position is   CC|      \__/|MMMC
  231. Enter the first person in the boat: 1. Missionary  2.cannibal2
  232. enter the second person in the boat 1. Missionary  2.cannibal 3:Empty0
  233.  
  234.  
  235.  
  236. The Position is   CCC|\__/       |MMM
  237. Enter the first person in the boat: 1. Missionary  2.cannibal2
  238. enter the second person in the boat 1. Missionary  2.cannibal 3:Empty2
  239.  
  240.  
  241.  
  242. The Position is   C|      \__/|MMMCC
  243. Enter the first person in the boat: 1. Missionary  2.cannibal2
  244. enter the second person in the boat 1. Missionary  2.cannibal 3:Empty0
  245.  
  246.  
  247.  
  248. The Position is   CC|\__/       |MMMC
  249. Enter the first person in the boat: 1. Missionary  2.cannibal2
  250. enter the second person in the boat 1. Missionary  2.cannibal 3:Empty2
  251.  
  252.  
  253.  
  254. You Win!!!Press any key to continue . . .
  255.  
  256.  
  257. */
Advertisement
Add Comment
Please, Sign In to add comment