Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // missionary and cannibals.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include<iostream>
- using namespace std;
- int ml=3, cl=3, mr=0,cr=0; //initilize everythng on left
- int boat_pos=0; //0=left, 1=right
- void display()
- {
- cout<<"\nThe Position is ";
- for (int i=0;i<ml;i++)
- cout<<"M";
- for (int i=0;i<cl;i++)
- cout<<"C";
- if(boat_pos) cout<<"| \\__/|" ;
- else cout<<"|\\__/ |" ;
- for (int i=0;i<mr;i++)
- cout<<"M";
- for (int i=0;i<cr;i++)
- cout<<"C";
- }
- int check()
- {
- if(ml<cl && ml!=0)
- {
- cout<<"\nMore cannibals on left! \nGAME OVER";
- system("pause");
- exit(1);
- }
- if(mr<cr && mr!=0)
- {
- cout<<"\nMore cannibals on right! \nGAME OVER";
- system("pause");
- exit(1);
- }
- if(mr==3 && cr==3)
- {
- cout<<"\nYou Win!!!";
- system("pause");
- exit(1);
- }
- return 1; //returns 1 if none of the conditions is true
- }
- void move()
- {
- int p1, p2,ppl; //person1 and person2
- cout<<"\nEnter the first person in the boat: 1. Missionary 2.cannibal";
- cin>>p1;
- cout<<"enter the second person in the boat 1. Missionary 2.cannibal 3:Empty";
- cin>>p2;
- ppl=p1*10+p2;
- if(boat_pos) //boat to right
- {
- switch(ppl)
- {
- case 10:
- if(mr<1)
- cout<<"\nerror. no missionaries of right";
- else
- { ml+=1; mr--; }
- break;
- case 11:
- if(mr<2)
- cout<<"\nerror. not enough people";
- else
- { ml+=2; mr-=2;}
- break;
- case 20:
- if(cr<1)
- cout<<"\nerror. ot enough people";
- else
- {cl+=1; cr--;}
- break;
- case 21:
- case 12:
- if(cr<1 || mr<1)
- cout<<"\nerror. not enough people";
- else
- { cl+=1; ml+=1; cr--; mr--;}
- break;
- case 22:
- if(cr<2)
- cout<<"\nerror. not enough people";
- else
- {cl+=2; cr-=2;}
- break;
- default:
- cout<<"\nWRONG CH";
- }
- }
- else //boat to left
- {
- switch(ppl)
- {
- case 10:
- if(ml<1)
- cout<<"\nerror. no missionaries of right";
- else
- {mr+=1; ml--;}
- break;
- case 11:
- if(ml<2)
- cout<<"\nerror. not enough people";
- else
- { mr+=2; ml-=2;}
- break;
- case 20:
- if(cl<1)
- cout<<"\nerror. ot enough people";
- else
- { cr+=1; cl--;}
- break;
- case 21:
- case 12:
- if(cl<1 || ml<1)
- cout<<"\nerror. not enough people";
- else
- { cr+=1; mr+=1; ml-=1;cl-=1;}
- break;
- case 22:
- if(cl<2)
- cout<<"\nerror. not enough people";
- else
- { cr+=2; cl-=2;}
- break;
- default:
- cout<<"\nWRONG CHOICE";
- }
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- cout<<"\nMissionaries and cannibals Game:\n";
- int status=1;
- cout<<"\n\n";
- while(status)
- {
- display();
- move();
- cout<<"\n\n";
- status=check();
- boat_pos=!boat_pos;
- }
- return 0;
- }
- /*
- Missionaries and cannibals Game:
- The Position is MMMCCC|\__/ |
- Enter the first person in the boat: 1. Missionary 2.cannibal2
- enter the second person in the boat 1. Missionary 2.cannibal 3:Empty2
- The Position is MMMC| \__/|CC
- Enter the first person in the boat: 1. Missionary 2.cannibal2
- enter the second person in the boat 1. Missionary 2.cannibal 3:Empty0
- The Position is MMMCC|\__/ |C
- Enter the first person in the boat: 1. Missionary 2.cannibal2
- enter the second person in the boat 1. Missionary 2.cannibal 3:Empty2
- The Position is MMM| \__/|CCC
- Enter the first person in the boat: 1. Missionary 2.cannibal2
- enter the second person in the boat 1. Missionary 2.cannibal 3:Empty0
- The Position is MMMC|\__/ |CC
- Enter the first person in the boat: 1. Missionary 2.cannibal1
- enter the second person in the boat 1. Missionary 2.cannibal 3:Empty1
- The Position is MC| \__/|MMCC
- Enter the first person in the boat: 1. Missionary 2.cannibal1
- enter the second person in the boat 1. Missionary 2.cannibal 3:Empty2
- The Position is MMCC|\__/ |MC
- Enter the first person in the boat: 1. Missionary 2.cannibal1
- enter the second person in the boat 1. Missionary 2.cannibal 3:Empty1
- The Position is CC| \__/|MMMC
- Enter the first person in the boat: 1. Missionary 2.cannibal2
- enter the second person in the boat 1. Missionary 2.cannibal 3:Empty0
- The Position is CCC|\__/ |MMM
- Enter the first person in the boat: 1. Missionary 2.cannibal2
- enter the second person in the boat 1. Missionary 2.cannibal 3:Empty2
- The Position is C| \__/|MMMCC
- Enter the first person in the boat: 1. Missionary 2.cannibal2
- enter the second person in the boat 1. Missionary 2.cannibal 3:Empty0
- The Position is CC|\__/ |MMMC
- Enter the first person in the boat: 1. Missionary 2.cannibal2
- enter the second person in the boat 1. Missionary 2.cannibal 3:Empty2
- You Win!!!Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment