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?
In this puzzle, the position of the fox, chicken, grain and farmer can be represented with a number. Zero means they are on this side of the river, or one which means they are on the other side of the river. E.g.
`farmer = 0`
Means assign the farmer to be on this side of the river.
`grain = 1`
Means assign the grain to be on the other side of the river.
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 subprogram, selection or iteration.
In the main program:
fox, chicken, grain and farmer to be 0.choice.3 marks
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:
In the main program write selection statements to change the variables:
4 marks
Note, if you are not confident with subprograms you can do this in the main program instead.
output that takes no parameters.
output to show the outcome of the user choice.4 marks
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
-----------------------
Note, if you are not confident with subprograms you can do this in the main program instead.
wrong_move that takes no parameters.
wrong_move to check if the puzzle is lost.5 marks
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.
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
Note, if you are not confident with subprograms you can do this in the main program instead.
puzzle_solved that takes no parameters.
puzzle_solved in the main program to check if the puzzle has been solved.5 marks
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.