Vous êtes sur la page 1sur 19

P a g e | 6

COVER/TITLE PAGE
Farmer, Wolf, Goat, Cabbage.

A C++ Programming Project
Submitted to the Faculty
Of
COP1000-70T
Eastern Florida State College

By

ME
In Partial Fulfillment of the Requirements
For the Course of
COP1000-70T Principles of Programming
May 2014

Titusville, Florida

P a g e | 7

Table of Contents: . . . . . . . 1
Time Log . . . . . . . . . . 2

Abstract of Program . . . . . . . . 3

Programs Problem and Solution . . . . . . 4

Flow Chart . . . . . . . . . 6

List of Functions . . . . . . . . . 8

List of Preprocessor Commands/Header Files,
and Libraries used . . . . . . . . 8

List of Variables . . . . . . . . . 8

Source Code . . . . . . . . . 9

P a g e | 8

Screen Shot of Executed Program . . . . . . 14

P a g e | 9


Time Log
The project Farmer Wolf Goat Cabbage was started on February 3rd, 2014. During this initial process, I came up with
Algorithms and Flowcharts for the program. After flow charting and writing some pseudo code, the programming of the C++
programming commenced.
On April 22
nd
, the source code and commenting was completed, and the final build of the project was completed on May 2nd,
2014.
The program was exhibited on April 30
th
to Professor Canton-Sanchez, M. for review.
Professor Canton-Sanchez asked for the code to be commented further, these modifications were implemented on the evening
of April 30
th
.
During this process I made myself available to help other students along with their projects.
The total number of man hours spent on this was around 16hrs.


P a g e | 10

Abstract
The program Farmer, Wolf, Goat, Cabbage is a classic computer program assigned to begging programmers to get them used to thinking
logically and to expose the students to how really dumb a computer is and how it is necessary to give them every single instruction.

This program does not take any user input and starts with the boat the farmer, wolf , goat cabbage sitting on the West shore of a river.
The program is run from a while loop in the main function where it will run until all items are moved from the West shore of the river to
the East shore of the river.
The criteria set up for this trip across the river were that, the wolf and the goat could not be left alone together, and the goat and the
cabbage could not be left alone.
The program verifies the coasts are safe before taking the next item across the river.



P a g e | 11

Problem and Solution
Question: How to move items safely across the river and only allow one item to be moved at a time?
This is done by moving the goat to the East side of the river first, leaving the wolf and the cabbage alone, since the wolf wont eat
the cabbage the shoreline is considered safe.
The farmer then rows the boat back alone to the west shore to get the cabbage and rows back to the East shore where he must pick up the
goat as not to leave the goat alone with the cabbage because the goat will eat the cabbage.
After picking up the goat from the East shore the farmer rows the boat back to the east shore to drop off the goat and pickup the
wolf, then row it back to the East shore to drop the wolf
Off alone with the cabbage.
The farmer rows back to the West shore alone to pick up the cabbage and row it back to the East shore completing the task.


The Pseudo code is as follows:
Load the boat with goat at the West shore.
Cross the river.
Unload the goat from the boat
P a g e | 12

Cross the river.
Load the boat with the cabbage
Cross the River.
Unload the cabbage and load the goat.
Cross the river with goat.
Unload the goat and load the wolf into the boat.
Cross the river.
Unload the wolf from the boat.
Cross River alone.
Load the boat with the goat.
Cross River
Unload the goat.
End Loop
End Program.




P a g e | 13

While Loop
Load Boat
End While
Cross River
Unload Boat
Cross River
Is West shore
safe?
Yes
No
Is the East
shore safe?
Yes
End Program
No

P a g e | 8

Lists:
Functions:
int main()
int loadboat(void);// Loads the boat
int crossriver(void);// crosses river
int unloadboat(void);// unloads the boat
int iscompatable(void);// determines if the food chain is safe

Header Files and Libraries:
#include "stdafx.h"
#include<iostream>
#include<math.h>
#include<string>
#include<ostream>
using namespace std;

Variables:
///// Defines the food chain /////
#define cabbage 1
#define goat 2
#define wolf 3
#define farmer 4
//////// Defines the load unload locations
#define boatwest 1
#define boateast 2
int boat[2];// Holds the farmer and the cargo
int westshore[4];// holds the cargo on the west shore
int eastshore[4];// holds the cargo on the east shore
int boatlocation;// holds the east west location of the boat
std::string cargo;
int good = 0;
int lastitem = boat[0];

P a g e | 9



// FarmerWolfGoatCabbage.cpp : Defines the entry point for the console
application.
// Program Name: Farmer, Wolf, Goat, Cabbage
// Description:
// This program solves the classic Farmer, Wolf, Goat, Cabbage problem.
// The program will load the boat then determine if the shorelone
// is safe, before embarking on it's journey to the other side of the river
// the program will then unload the boat and ensure the shoreline is safe.
// The main loop contains only the logic required to ensure the boat
// initiates a loading, crossing and unloading function and exiting
// when all the items are moved safely accross the river.

// Developer: Anthony Olive
// Date: 5/4/14
// Development Environment: Microsoft Visual Studio 2013
// food chain:
// goat eats cabbage
// wolf eats goat

#include "stdafx.h"
#include<iostream>
#include<math.h>
#include<string>
#include<ostream>
///// Defines the food chain /////
#define cabbage 1
#define goat 2
#define wolf 3
#define farmer 4
//////// Defines the load unload locations
#define boatwest 1
#define boateast 2


int loadboat(void);// Loads the boat
int crossriver(void);// crosses river
int unloadboat(void);// unloads the boat
int iscompatable(void);// determines if the food chain is safe

int boat[2];// Holds the farmer and the cargo
int westshore[4];// holds the cargo on the west shore
int eastshore[4];// holds the cargo on the east shore
int boatlocation;// holds the east west location of the boat

using namespace std;

void main()
{
boat[1] = farmer; // Place the farmer in the boat

boatlocation = boatwest; // starts the boat on the west bank

westshore[0] = cabbage; // starts the cabbage on the west bank
P a g e | 10

westshore[1] = goat; // starts the goat on the west bank
westshore[2] = wolf; // starts the wolf on the west bank

// sets the east bank to nothing
eastshore[0] = 0;
eastshore[1] = 0;
eastshore[2] = 0;

// Main loop runs until the west shore is empty
while (westshore[0] != 0 || westshore[1] != 0 || westshore[2] != 0)
{
boat[0] = loadboat();
boatlocation = crossriver();
boat[0] = unloadboat();
boatlocation = crossriver();
}
cout << "All cargo moved and everyone survived!" << endl;
system("pause");
}
////////////////////////////////////////////////////////////
/////////////////////// Load the boat //////////////////////
////////////////////////////////////////////////////////////
// This function will load the items onto the boat.
// but first it checks to see if it already has cargo, if
// it does have cargo it will unload that cargo first
// call the iscompatable() function to see if the shoreline
// is safe, if the shoreline is not safe it unloads the
// current item then loads the next item and repeats the
// safety check until a safe shoreline is obtained.
//
int loadboat(void)
{
std::string cargo;
int good = 0;
int lastitem = boat[0];

//////// if the boat has something in it unload the boat ////////////
if (boat[0] != 0)
{
boat[0] = unloadboat();
}

/////// If the boat is on the west Shore ///////////////////
if (boatlocation == boatwest)
{
// check to see if the west shore cabbage location is empty
if (westshore[0] == cabbage && boat[0] == 0)
{
boat[0] = cabbage;
westshore[0] = 0;

cout << "loading cabbage" << endl;
good = iscompatable();

// if the shore is not compatible unload cabbage
if (good != 1)
{
P a g e | 11

cout << "unloading cabbage" << endl;
boat[0] = 0;
westshore[0] = cabbage;
}
}

// check to see if the west shore goat location is empty
// and the last item is not the goat
if (westshore[1] == goat && boat[0] == 0 && lastitem != goat)
{
boat[0] = goat;
westshore[1] = 0;
cout << "loading goat" << endl;

good = iscompatable();
if (good != 1)
{
cout << "unloading cabbage" << endl;
boat[0] = 0;
westshore[1] = goat;
}
}

// check to see if the west shore wolf location is empty
if (westshore[2] == wolf && boat[0] == 0)
{
boat[0] = wolf;
westshore[2] = 0;
cout << "loading wolf" << endl;

good = iscompatable();
if (good != 1)
{
cout << "unloading wolf" << endl;
boat[0] = 0;
westshore[2] = wolf;
}
}
}
/////////// End West
////////// If the boat is on the East Bank /////////////////

if (boatlocation == boateast)
{
if (eastshore[2] == wolf && boat[0] == 0)
{
boat[0] = wolf;
eastshore[2] = 0;

good = iscompatable();
if (good != 1)
{
boat[0] = 0;
eastshore[2] = wolf;
}
}
if (eastshore[1] == goat && boat[0] == 0)
P a g e | 12

{
boat[0] = goat;
eastshore[1] = 0;

good = iscompatable();
if (good != 1)
{
boat[0] = 0;
eastshore[1] = goat;
}
}
}
///////// End East

switch (boat[0])
{
case cabbage:
cargo = "cabbage";
break;
case goat:
cargo = "goat";
break;
case wolf:
cargo = "wolf";
break;
case 0:
cargo = "nothing";
break;
default:
cargo = "unknown";
break;
}

cout << "Loading Boat with " << cargo << endl;
return boat[0];
}
////////////////////////////////////////////////////////////
/////////////////////// cross the river/////////////////////
////////////////////////////////////////////////////////////
// This function sets the location of the boat on opposite
// shore after crossing

int crossriver(void)
{
std::string cargo;

//cout << " Crossing the River" << endl;

switch (boat[0])
{
case cabbage:
cargo = "cabbage";
break;
case goat:
cargo = "goat";
break;
case wolf:
P a g e | 13

cargo = "wolf";
break;
case 0:
cargo = "nothing";
break;
default:
cargo = "unknown";
break;
}

if (boatlocation == boatwest)
{
cout << "Crossing to the east side of the river with " << cargo <<
endl;
boatlocation = boateast;
}
else
{
cout << "Crossing to the west side of the river with " << cargo <<
endl;
boatlocation = boatwest;
}

return boatlocation;
}
////////////////////////////////////////////////////////////
/////////////////////// UnLoad the boat ////////////////////
////////////////////////////////////////////////////////////
// This function will unload the items onto the shoreline then
// call the iscompatable() function to see if the shoreline
// is safe, if the shoreline is not safe it does not load the
// current item but instead loads the next item, and repeats
// the safety check until a safe shoreline is obtained.

int unloadboat(void)
{
std::string cargo;
int good = 0;
int lastitem = boat[0];


// This code block is for user display purposes only
// displays to the user what is being loaded and unloaded
switch (boat[0])
{
case cabbage:
cargo = "cabbage";
break;
case goat:
cargo = "goat";
break;
case wolf:
cargo = "wolf";
break;
case 0:
cargo = "nothing";
break;
P a g e | 14

default:
cargo = "unknown";
break;
}

cout << "UnLoading " << cargo << " from Boat" << endl;

////////// If the boat is on the East Bank /////////////////
if (boatlocation == boateast)
{
if (eastshore[0] == 0 && boat[0] == cabbage)
{
boat[0] = 0;
eastshore[0] = cabbage;

cout << "unloading cabbage" << endl;
good = iscompatable();// check to see if the east shore is
compatable

if (good == (goat + cabbage))
{
cout << "loading goat" << endl;
boat[0] = goat;
eastshore[1] = 0;
}
}

if (eastshore[1] == 0 && boat[0] == goat && lastitem != cabbage)
{
boat[0] = 0;
eastshore[1] = goat;
cout << "unloading goat" << endl;

good = iscompatable();
if (good != 1)
{
cout << "reloading goat" << endl;
boat[0] = goat;
eastshore[1] = 0;
}
}

if (eastshore[2] == 0 && boat[0] == wolf)
{
boat[0] = 0;
eastshore[2] = wolf;
cout << "unloading wolf" << endl;

good = iscompatable();
if (good != 1)
{
cout << "reloading wolf" << endl;
boat[0] = wolf;
eastshore[2] = 0;
}
}
}
P a g e | 15

///////// End East
/////// boat is on the west shore ///////////////
if (boatlocation == boatwest)
{
if (westshore[0] == 0 && boat[0] == cabbage)
{
boat[0] = 0;
westshore[0] = cabbage;
cout << "unloading cabbage" << endl;
good = iscompatable();

if (good != 1)
{
cout << "reloading cabbage" << endl;

boat[0] = cabbage;
westshore[0] = 0;
}
}

if (westshore[1] == 0 && boat[0] == goat)
{
boat[0] = 0;
westshore[1] = goat;
cout << "unloading goat" << endl;

good = iscompatable();

if (good != 1 && lastitem != goat)
{
cout << "reloading goat" << endl;
boat[0] = goat;
westshore[1] = 0;
}
}

if (westshore[2] == 0 && boat[0] == wolf)
{
boat[0] = 0;
westshore[2] = wolf;
cout << "unloading wolf" << endl;
good = iscompatable();
if (good != 1)
{
cout << "reloading wolf" << endl;
boat[0] = wolf;
westshore[2] = 0;
}
}
}
/////////// End West

return boat[0];
}

////////////////////////////////////////////////////////////////////////
//////////////// Check to see if shore is compatable ///////////////////
P a g e | 16

////////////////////////////////////////////////////////////////////////
int iscompatable(void)
{
int compatable;
//cout << " Checking compatability" << endl;

if (boatlocation == boateast)
{

if (eastshore[0] == cabbage && eastshore[1] == goat &&
eastshore[2] != wolf)
{
compatable = cabbage + goat;// = 1+2=3
}
else if (eastshore[0] != cabbage && eastshore[1] == goat &&
eastshore[2] == wolf)
{
compatable = goat + wolf;//2+3=5
}
else
{
compatable = 1;
}

switch (compatable)
{
case 0:
cout << " east shore is not compatable" << endl;
break;

case 1:
cout << " east shore is compatable" << endl;
break;

case cabbage + goat:
cout << " east shore is not compatable, goat will eat
cabbage " << endl;
break;

case wolf + goat:
cout << " east shore is not compatable, wolf will eat goat
" << endl;
break;

}
}

if (boatlocation == boatwest)
{

if (westshore[0] == cabbage && westshore[1] == goat &&
westshore[2] != wolf)
{
compatable = cabbage + goat;// = 1+2=3
}
else if (westshore[0] != cabbage && westshore[1] == goat &&
westshore[2] == wolf)
P a g e | 17

{
compatable = goat + wolf;//2+3=5
}
else
{
compatable = 1;
}

switch (compatable)
{
case 0:
cout << " west shore is not compatable" << endl;
break;

case 1:
cout << " west shore is compatable" << endl;
break;

case cabbage + goat:
cout << " west shore is not compatable, goat will eat
cabbage " << endl;
break;

case wolf + goat:
cout << " west shore is not compatable, wolf will eat goat
" << endl;
break;

}
}

return compatable;
}








P a g e | 18

Vous aimerez peut-être aussi