Heap of Beans: We are going to play a game called the heap of beans. You start with a heap of beans (we will start with 16 beans) and two players. Each player can remove 1, 2, or 3 beans from the pile...


Heap of Beans:


We are going to play a game called the heap of beans. You start with a heap of beans (we will start with 16 beans) and two players. Each player can remove 1, 2, or 3 beans from the pile of 16. Each player is required to take some number of beans from the pile during each turn. The players take turns removing beans, and the player who takes the last bean from the pile is the loser. You can try this game with a partner using 16 pieces of paper as beans.


Each player is to be represented by a function, so we will have two functions. Each function takes a single argument, representing the present number of beans, and returns the number of beans remaining after the player function takes its turn. During the operation of the player function, the function reports (prints) how many beans were removed. Note that the function decides how many beans to remove. It takes no input from the user.


You also need a main program. The main program initializes the heap of beans to 16, and then alternately calls the first player function and the second player function until one of them gets the last bean. The main then reports who the loser is and exits


(a)    Write a simple player function such as one that always takes exactly one bean. It isn’t very interesting, but it lets you test your program.


(b)   Now for the fun part. Write a “smart player” function and test it against other students’ functions in your class. A class tournament would be best—who has the best strategy?

Nov 27, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here