Thursday, 1 May 2014

missionary and cannibal using c++

#include<iostream>
using namespace std;
#include<conio.h>
#include<iomanip>
void check();
void check1();
void display();
void boatoption();
void main();
int im = 3, ic = 3, i, j, fm = 0, fc = 0, status = 0, bpass1 = 0, bpass2 = 0, flag = 0;
void reset()
{
       im = 3, ic = 3, i, j, fm = 0, fc = 0, status = 0, bpass1 = 0, bpass2 = 0, flag = 0;
}
void indisplay()
{
       cout << "INITIAL STATE\n";
       cout << "\n" << setw(40) << "               __________WATER___________<boat>    ";

       for (int i = 0; i<3; i++)
       {
              cout << " M ";
       }
       for (int i = 0; i<3; i++)
       {
              cout << " C ";

       }
}

void userchoice()
{
       int ch;
       do
       {
              cout << "\nEnter Your Choice: ";
              cout << "\n\n1----Display\n2----Use a boat\n3---go to main menu\n";
              cin >> ch;

              switch (ch)
              {
              case 1:display(); break;
              case 2:
                     boatoption(); break;
              case 3:break;
              default:cout << "\nEnter a valid choice!";
              }
       } while (ch != 3);
}
void display()
{
       cout << "\n";
       for (int i = 0; i<fm; i++)
       {
              cout << " M ";
       }
       for (int i = 0; i<fc; i++)
       {
              cout << " C ";
       }
       if (flag == 0)
              cout << " " << setw(40) << "            __________WATER___________<boat>    ";
       else
              cout << " " << setw(40) << "            <boat>__________WATER___________    ";
       for (int i = 0; i<im; i++)
       {
              cout << " M ";
       }
       for (int i = 0; i<ic; i++)
       {
              cout << " C ";
       }
       cout << "\n";

}
void boatoption()
{
       int ch;
       cout << "\nBOAT OPTION";
       cout << "\n\n1--Load a canibal\n2--Load a missionary\n3--drop a cannibal\n4--Drop a Missionary\n5--Move to the leftSide\n6--Move to the RightSide\n7--go to previous menu\n\n";
       do
       {
             
              cin >> ch;


              switch (ch)
              {
              case 1:
              {      if (status >= 2)
                     cout << "\n\nBOAT IS FULL ";
              else if (flag == 0)
              {
                     status++; bpass1++; ic--; cout << "Canibal loaded"<<endl;
              }
              else
              {
                     status++; bpass1++; fc--; cout << "Canibal loaded"<<endl;
              }
              }break;
              case 2:{             if (status >= 2)
                     cout << "\n\nBOAT IS FULL";
                        else if (flag == 0)
                        {
                              status++; bpass2++; im--; cout << "Missionary loaded"<<endl;

                        }
                        else
                        {
                              status++; bpass2++; fm--; cout << "Missionary loaded"<<endl;
                        }
              }break;
              case 3:{      if (bpass1>0)
              {
                              if (flag == 0)
                              {
                                     status--;
                                     bpass1--;
                                     ic++;
                                     flag = 0;
                                     check();

                              }

                              else
                              {
                                     status--;
                                     bpass1--;
                                     fc++;
                                     flag = 1;
                                     check();

                              }
                              cout << "\n\ncanibal dropped"<<endl;
              }
                        else
                              cout << "\n\nNO CANIBAL"<<endl;

              }break;
              case 4: {     if (bpass2>0)
              {
                                  if (flag == 0)
                                  {
                                         status--;
                                         bpass2--;
                                         im++;
                                         flag = 0;
                                         check();
                                  }
                                  else
                                  {
                                         status--;
                                         bpass2--;
                                         fm++;
                                         flag = 1;
                                         check();

                                  }
                                  cout << "\n\nmissionary dropped"<<endl;
              }
                           else
                                  cout << "\n\nNO MISSIONARY"<<endl;

              }break;
              case 5:{
                              if (status == 0)
                                     cout << "\n\nNO PASSENGER!!BOAT CANNOT BE MOVED"<<endl;
                              else
                              {
                                     if (flag == 0)
                                     {
                                            cout << "\n\nBoat moved to the LeftSide Side"<<endl;
                                            flag = 1;
                                            check1();

                                     }
                                     else
                                            cout << "\n\nBoat is on left Side of river only!!! "<<endl;
                              }
              }display(); break;

              case 6:{
                              if (status == 0)
                                     cout << "\n\nNO PASSENGER!!BOAT CANNOT BE MOVED"<<endl;
                              else
                              {
                                     if (flag == 1)
                                     {
                                            cout << "\n\nBoat moved to the Right Side"<<endl;
                                            flag = 0;
                                            check1();

                                     }
                                     else
                                            cout << "\n\nBoat is on Right Side of river only!!! "<<endl;
                              }
              }display(); break;
              case 7:userchoice(); break;
              }
       } while (ch != 7);
}
void check1()
{
       if (fc == 3 && fm == 3 && ic == 0 && im == 0)
       {
              cout << "\n\nCONGO!!YOU WON!!!"; display(); reset(); main();
       }
       else if (flag == 0)
       {
              if (fc>fm&&fm != 0)
              { { cout << "\n\nEATEN!!"; display(); reset(); main(); }

              }
       }
       else
       {
              if ((ic>im) && im != 0)

              {
                     cout << "\n\nEATEN"; display(); reset(); main();
              }
       }
}
void check()
{
       if (fc == 3 && fm == 3 && ic == 0 && im == 0)
       {
              cout << "\n\nCONGO!!YOU WON!!!"; display(); reset(); main();
       }
       else if (flag == 0)
       {
              if ((ic + bpass1)>(im + bpass2))
              { { cout << "\n\nEATEN!!"; display(); reset(); main(); }

              }
       }
       else
       {
              if (((fc + bpass1)>(fm + bpass2)) && fm != 0)

              {
                     cout << "\n\nEATEN"; display(); main();
              }
       }
}
void main()
{
       int ch;
       cout << "\n\n*************WELCOME TO THE MISSIONARIES AND CANNIBELS GAME*************\n";
       do
       {
              cout << "\n\nPress 1---Play\n      2---Exit \n";
              cin >> ch;
              indisplay();
              if (ch == 1)
              {
                     userchoice();
              }

       } while (ch != 2);


}

1 comment:

  1. # include
    # include
    # include
    # include
    # include
    using namespace std;

    //typedef enum { false=0 ,true=1 } ;


    const bool isPositiveNumber(const char*);
    const bool isNegativeNumber(const char*);
    const bool isPositiveDigit(const char*);
    const bool isNegativeDigit(const char*);
    const bool isIdentifier(const char*);
    const bool isKeyword(const char*);

    void main( )
    {


    char Token[25]={NULL};

    cout<<"Enter a token : ";
    gets(Token);

    if(isKeyword(Token)==true)
    cout<<"The given Token is a Keyword.";

    else if(isIdentifier(Token)==true)
    cout<<"The given Token is an Identifier.";

    else if(isPositiveDigit(Token)==true)
    cout<<"The given Token is a Positive Digit.";

    else if(isNegativeDigit(Token)==true)
    cout<<"The given Token is a Negative Digit.";

    else if(isPositiveNumber(Token)==true)
    cout<<"The given Token is a Positive Number.";

    else if(isNegativeNumber(Token)==true)
    cout<<"The given Token is a Negative Number.";

    else
    cout<<"Error : The given Token is invalid.";

    getch( );
    }


    const bool isPositiveNumber(const char* Token)
    {
    if((!isdigit(Token[0]) && Token[0]!='+') || Token[0]=='0')
    return false;

    if(Token[0]=='+' && (Token[1]=='0' || !isdigit(Token[1])))
    return false;

    int length=strlen(Token);

    for(int count=1;count2 || strlen(Token)==0)
    return false;

    else if(strlen(Token)==2 && (Token[0]!='+' ||
    Token[1]=='0' || !isdigit(Token[1])))
    return false;

    else if(strlen(Token)==1 && (Token[0]=='0' || !isdigit(Token[0])))
    return false;

    return true;
    }


    const bool isNegativeDigit(const char* Token)
    {
    if(strlen(Token)!=2 || Token[0]!='-' ||
    Token[1]=='0' || !isdigit(Token[1]))
    return false;

    return true;
    }


    const bool isIdentifier(const char* Token)
    {
    if(!isalpha(Token[0]) && Token[0]!='_')
    return false;

    int length=strlen(Token);

    for(int count=1;count16 || strlen(Token)==0)
    return false;

    const char Keywords[64][20]={
    "asm","auto","bool","break","case","catch",
    "char","class","const","const_cast",
    "continue","default","delete","do","double",
    "dynamic_cast","else","enum","explicit",
    "export","extern","false","float","for",
    "friend","goto","if","inline","int","long",
    "main","mutable","namespace","new",
    "operator","private","protected","public",
    "register","reinterpret_cast","return",
    "short","signed","sizeof","static",
    "static_cast","struct","switch","template",
    "this","throw","true","try","typedef",
    "typeid","typename","union","unsigned",
    "using","virtual","void","volatile",
    "wchar_t","while"
    };

    for(int count=0;count<64;count++)
    {
    if(strcmpi(Keywords[count],Token)==0)
    return true;
    }

    return false;
    }

    ReplyDelete