Homework Assignment 2

A First Experience Implementing Algorithms

Top-Level View

We will often compare algorithms using experiments in code. Sometimes, we will compare usage of resources such as time and space. For this assignment, we get our first experience implementing an experiment by comparing the playing ability of two algorithms for the End Game.

Write the following functions.

1. Write a function greedy(board) that implements the greedy approach to choosing a move. The input is a game board of n integers. The output is the position of the selected number: 1 or n (or 0 or n-1, if your language is 0-based).

2. Write a function zoomin(board) that implements the zoom-in approach to choosing a move. This function has the same input/output signature as greedy().

3. Write a function random_board(N,Z) that returns a random game board. The inputs are N, the size of the board, and Z, the maximum value on the board. The output is a game board of size N where each number is in the range [1..Z].

4. Write a function game(P1,P2,board) that plays a single game between two players on a given board. The inputs are P1, the player who moves first, P2, the player who moves second, and board, and the game board. The players are functions such as greedy() and zoomin(). The output is the result of the game from P1's perspective: 1.0 for a win, 0.5 for a tie, or 0.0 for a loss.

5. Write a function experiment(P1,P2,num_trials) that runs a multi-game match between two players. The inputs are P1 and P2, the two players, and num_trials, the number of games in the match. The output is the total number of game points won by P1 in the match, ranging between 0 and num_trials, inclusive.

6. Write a driver function that takes a single command-line argument, n, the number of games to play in an experiment. This function calls experiment() twice, once with greedy() playing first against zoomin() and once with zoomin() playing first against greedy(). In both cases, the players play n games. Choose the board size for each game at random from the range [2.100]. The driver prints the results of the two matches.

Run your program for numbers of games that are powers of 10 from 10 to at least 1,000,000, and put the results in a table.

Create a readme.txt file that tells me:

• anything I need to know to compile and run your programs,
• what you found to be the hardest part of the assignment, and

Implementation Notes

As noted in the syllabus and Session 1, unless otherwise specified by the assignment, you may write your programs in any language that I can compile and run on student.cs or my standard Mac OS X machines.

You may represent a game board in any way suitable to your language: as an array, a list, a vector, ..., of n integers.

If you use Java, you should at least put all the methods in a class named EndGameExperiment. Make the driver a method named run() that takes the the number of trials as an argument.

Or... If you use Java or some other object-oriented language, there are much better OO designs for this problem. You may certainly create separate objects where appropriate. If you'd like to talk about an OO design, let me know!

Deliverables

By the due time and date, submit a zip file named homework02 containing: