Here are a few guidelines:
- You somehow need to store where the cannibals/missionaries are (on what bank). Of course you could have 6 different variables but a slightly better approach would be to have 2 arrays, one for each bank (or one for the missionaries and one for the cannibals). The data structure could look like this:
Code:
#define CANNIBALS 3
#define MISSIONARIES 3
#define CANNIBAL 1
#define MISSIONARY 2
char leftRiverBank[CANNIBALS + MISSIONARIES] = {0};
char rightRiverBank[CANNIBALS + MISSIONARIES] = {0};
/* put x missionaries on the left river bank */
int i;
for (i = 0; i < MISSIONARIES; i++)
{
leftRiverBank[i] = MISSIONARY;
}
/* put y cannibals on the right river bank */
for (i = 0; i < CANNIBALS i++)
{
rightRiverBank[i] = CANNIBAL
}
You also need several utility functions, like a function that checks if there is a missionary/cannibal on a side of the river or a function that moves them across with the boat etc.