Fox, chicken, grain

Fox, chicken, grain

A classic logic puzzle.

A farmer has a fox, a chicken and a bag of grain. Left alone without the farmer, the fox would eat the chicken and the chicken would eat the grain. The farmer must transport all three to the other side of a river but can only take one item in his rowboat at a time. How does he get all three to the other side?


The puzzle variables fox, chicken, grain and farmer can be 0 which means they are on this side of the river, or 1 which means they are on the other side of the river. E.g.

Assessment


These objectives get progressively harder. Attempt as much of the program as you can in the order presented below. Remember to use a comment to describe a subpprogram, selection or iteration.

Use this boilerplate code as a starting point:

Success Criteria

Initialising the variables

In the main program:

  • Output to the screen the statement, “A fox, chicken and a bag of grain wait by the side of a river.”
  • Assign four integer variables called, `fox`, `chicken`, `grain` and `farmer` to be 0.
  • Ask the user to input fox, chicken, grain or farmer. This is to be stored in a variable called `choice`.

3 marks

The output should now look like this:

A fox, chicken and a bag of grain wait by the side of a river.

Which item will you take in your rowboat to the other side?

fox, chicken, grain or farmer?

Choose:

Row across the river

In the main program write selection statements to change the variables:

  • If the choice was farmer and the farmer is 0 then assign the farmer to be 1.
  • If the choice was fox and the farmer is 0 and the fox is 0 then assign the farmer and fox to both be 1.
  • If the choice was chicken and the farmer is 0 and the chicken is 0 then assign the farmer and chicken to both be 1.
  • If the choice was grain and the farmer is 0 and the grain is 0 then assign the farmer and grain to both be 1.

4 marks

Show the state of the game

  • Define a subprogram called, `output` that takes no parameters.
  • Output what is on this side of the river (a variable is 0).
  • Output what is on the other side of the river (a variable is 1).
  • In the main program call the subprogram `output` to show the outcome of the user choice.

4 marks

The output should now look like this:

A fox, chicken and a bag of grain wait by the side of a river.

Which item will you take in your rowboat to the other side?

fox, chicken, grain or farmer?

Choose: chicken

———————–

This side of the river:

Fox

Grain


The other side of the river:

Farmer

Chicken

———————–

Losing the puzzle

  • Define a subprogram called `wrong_move` that takes no parameters.
  • If the farmer is 1 and the fox and chicken are both 0, output “The fox ate the chicken.”
  • If the farmer is 1 and the chicken and grain are both 0, output “The chicken ate the grain.”
  • Return True if the puzzle is lost, or False if it is not.
  • In the main program call the subprogram `wrong_move` to check if the puzzle is lost.

5 marks

The output should now look like this:

A fox, chicken and a bag of grain wait by the side of a river.

Which item will you take in your rowboat to the other side?

fox, chicken, grain or farmer?

Choose: grain

———————–

This side of the river:

Fox

Chicken


The other side of the river:

Farmer

Grain

———————–

The fox ate the chicken.

Apply the logic to the other side of the river:

  • Add statements to the main program to enable the farmer with the fox, chicken or grain to return to this side of the river.
  • Add statements to the `wrong_move` subprogram to check if the fox ate the chicken or the chicken ate the grain on the other side of the river.

2 marks

Puzzle is solved

  • Define a subprogram called `puzzle_solved` that takes no parameters.
  • If the fox, chicken and grain are all 1, output “You solved the puzzle.”
  • Return True if the puzzle is solved or False if not.
  • Add a call to `puzzle_solved` in the main program to check if the puzzle has been solved.
  • Add an iteration to the main program so that the puzzle continues until it is either lost or solved.

5 marks

Good programming practices

  • The subprograms and main program sections are signified with comments.
  • The subprograms have a comment to describe their purpose.

2 marks

Maximum mark: 25

If you score less than 20 you need more practice at levels 1-3 before you continue to the next level.

Help

Use these resources as a reference to help you meet the success criteria.

Programming guide:

Evaluate


Run the unit tests below to check that your program has met the success criteria.

A fox, chicken and a bag of grain wait by the side of a river.

Which item will you take in your rowboat to the other side?

fox, chicken, grain or farmer?

Choose: fox

———————–

This side of the river:

Chicken

Grain


The other side of the river:

Farmer

Fox

———————–

The chicken ate the grain.

A fox, chicken and a bag of grain wait by the side of a river.

Which item will you take in your rowboat to the other side?

fox, chicken, grain or farmer?

Choose: grain

———————–

This side of the river:

Fox

Chicken


The other side of the river:

Farmer

Grain

———————–

The fox ate the chicken.

A fox, chicken and a bag of grain wait by the side of a river.

Which item will you take in your rowboat to the other side?

fox, chicken, grain or farmer?

Choose: chicken

———————–

This side of the river:

Fox

Grain


The other side of the river:

Farmer

Chicken

———————–

Which item will you take in your rowboat to the other side?

fox, chicken, grain or farmer?

Choose: farmer

———————–

This side of the river:

Farmer

Fox

Grain


The other side of the river:

Chicken

———————–

Which item will you take in your rowboat to the other side?

fox, chicken, grain or farmer?

Choose: grain

———————–

This side of the river:

Fox


The other side of the river:

Farmer

Chicken

Grain

———————–

Which item will you take in your rowboat to the other side?

fox, chicken, grain or farmer?

Choose: chicken

———————–

This side of the river:

Farmer

Fox

Chicken


The other side of the river:

Grain

———————–

Which item will you take in your rowboat to the other side?

fox, chicken, grain or farmer?

Choose: fox

———————–

This side of the river:

Chicken


The other side of the river:

Farmer

Fox

Grain

———————–

Which item will you take in your rowboat to the other side?

fox, chicken, grain or farmer?

Choose: farmer

———————–

This side of the river:

Farmer

Chicken


The other side of the river:

Fox

Grain

———————–

Which item will you take in your rowboat to the other side?

fox, chicken, grain or farmer?

Choose: chicken

———————–

This side of the river:


The other side of the river:

Farmer

Fox

Chicken

Grain

———————–

You solved the puzzle.

Craig'n'Dave logo

Craig ‘n’ Dave

Bett Awards 2024 Finalist