Starting from:

$30

IT210- Fundamentals of Programming Lab 6 Solved

You will be working as an individual on this lab and build a player (a function that you write) to play Heap of Beans. This game is played with two players who take turns removing beans from a heap of beans on the table. Each player in turn is permitted to remove 1, 2, or 3 beans for a turn. The player who takes the last bean loses. Your goal is to write the best function (producing the largest number of wins) to play the part of one player in this game. Your function must be named player and expect one argument, the number of beans currently remaining in the heap. It will return the number of beans (an integer) to be removed from the heap this turn. Its basic structure is like this:

## Play "Heap of Beans". Use the best strategy you can devise to beat an opponent.

# 1, 2, or 3 beans can be removed in a turn, but never more than the number of beans

# currently on the table.

# @param beans – the number of beans currently on the table

# @return – the number of beans to be removed this turn

#

def player(beans):

.

.

.

return beansToTake

I have provided a zip file on D2L that contains 3 files to help you to test your program: beans.py is the main testing program (shown below) that will allow you to compete against other programs (including yourself if you want) and two very simple programs that are very poor competitors and that you should usually beat. The test program uses a few python features you haven't seen yet, but you should get a pretty good idea of how it works by reading it. The program I use for testing is similar, but will automatically match you with other competitors



Your program, the test driver and the competitor programs should all be in the same folder. To use the test driver, on line 4, change lab6 to the name of your file (without the .py). On line 3 select simple1, simple2 or your file name as the competitor.

More products