CIT 591 Assignment 8:
Saddle Points
Fall 2011, David Matuszek
Min |
||||||||||||||||||||
|
||||||||||||||||||||
| Max |
|
|
||||||||||||||||||
Consider the following very simple "game." There is a two-dimensional array of numbers, and two players, Maxie and Minnie. At each turn, Max chooses some row and Min chooses some column. Where they intersect is the amount of money Min pays to Max (if it's negative, Max pays Min).
For example, if Max chooses row 1 and Min chooses column 1, then Max wins $14 from Min. But if Max chooses row 3 and Min chooses column 2, then Max pays Min $10. In general: Max wants larger numbers, Min wants smaller numbers.
How should they play? In many games like this, the players want to keep their opponent from knowing their next move. But in some games (including this one), there is a best move which should always be taken.
Name your project SaddlePoints, your package saddlePoints, and your class SaddlePoints. Note how these are capitalized.
|
||||||||||||||||||||||||||||||||||||||||||||||||
For each player, we pick the "best of the worst." In this example, the worst Max can do if he picks row 0 is -9; if row 1, then 5; if row 2, then -8; and if row 3, then -10. Of these possible worst outcomes, Max prefers the largest, which is 5 (row 1).
Similarly, Min wants smaller numbers. The worst she can do if she picks column 0 is 10; if column 1, then 17; and if column 2, then 5. Of these possible worst outcomes, Min prefers the smallest, which is 5 (column 2).
Since the "best of the worst" (the maximum of the minimums) for Max is 5, and the "best of the worst" (the minimum of the maximums) for Min is also 5, then Max should always choose row 1, and Min should always choose column 2. This is called a saddle point.
Here's why: If Min does choose column 2, then Max will do worse by choosing any other row. And if Max does choose row 1, then Min will do worse by choosing any other column.
It is possible for there to be more than one saddle point, in which case they will all have the same value (for example, they will all be 5), and we don't care which one we find.
Here are the computational methods you should have. You should unit test each of these methods.
|
Here are the I/O methods for which you do not need unit tests:
|
To save you some typing, I have provided the file SaddlePoints.java, in which all the methods have been entered as stubs, and the comments have been mostly filled out. Start Eclipse, create the project and the package, then just paste this file in.
Here's how to save yourself a bunch of typing: Let Eclipse write your code for you!
int
getLargestValue(int[] array) { return
9999; }File → New...
→ JUnit Test Case,
choose New JUnit 4 Test, enter a name for your test class, make sure the setUp() method
is checked, then click Next>. Check the
methods for which you want to create test cases. Then click Finish.Of course, Eclipse doesn't know what to put in the test methods; that part is still up to you.
The first time you try to run your program, Eclipse will tell you that JUnit isn't on your "build path," and will offer to add it. Let Eclipse add it. Depending on your installation, Eclipse may already know where to find it, or it may ask you to find it.
You don't have to create your unit tests this way. The test file is just a normal Java file--if you want to add more tests, just imitate the tests that are already there. Also remember that the test methods are normal Java, and you can put whatever code you want in them.
Here's the general structure you need. This is a separate class, so Eclipse will put it in a separate file.
package saddlepoints;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
/**
* @author Your name goes here
* @author Your partner's name goes here
*
*/
public class SaddlePointsTest {
SaddlePoints sp = new SaddlePoints(); // create an instance variable
// If you use the same variables in multiple tests,
// declare them here
@Before
public void setUp() throws Exception {
// If you use the same variables in multiple tests,
// assign values to them here
}
@Test
public void testSomeMethod() {
// Put tests here
// Put sp. in front of every call to a method in SaddlePoints
}
}
Here are some of the more useful JUnit methods:
assertEquals(expectedValue, actualValue);assertArrayEquals(expectedArray, actualArray);assertTrue(condition);assertFalse(condition);fail();The method createRandomArray(numberOfRows, numberOfColumns, minValue, maxValue) should return a different value every time, but you can still test it. Testing whether it's "really random" is difficult, but the following tests are pretty easy:
You will need to import java.util.Random;
You will need to create a random number generator (an instance of the class Random). You only need one of these.
To get numbers in a reasonable range, you will need to call one of the methods defined in Random. I'm not going to tell you everything; you should look at either
Arrays in Java are like lists in Python, and are indexed the same way. However, the size of an array is specified when you create the array, and you can't add or delete elements. (You can, however, assign a different array to the variable.) You can declare arrays like this:
type [] name = new type[size];type [][] name = new type[rows][columns];Declared in this way, all locations in the new array contain null, zero, or false, depending on the type of the array. You can put specific values in an array when you declare it (but not later) like this:
type [] name = {value1, ..., valueN};type [][] name = {{value1_1, ..., value1_N}, ... {valueN_1, ..., valueN_N}};To use a literal array in a statement, rather than in a declaration, the syntax is a little different:
name = type [] {value1, ..., valueN};name = type [][] {{value1_1, ..., value1_N}, ... {valueN_1, ..., valueN_N}};For testing purposes, you can use the above game,
int[][] with = {{-9, 12, -6},
{ 7, 14, 5},
{10, -8, 3},
{ 6, 17,-10}};
which has a saddle point, and the game
int[][] without = {{ 1, -2, 3},
{-6, 5, -4},
{ 7, -8, 9}};
which does not.
Before 6 AM, Friday November 11, 2011, via Sakai. Zip and turn in only one copy of the assignment for your team, making sure that both your names are on it.