ucsb-cs56-projects / cs56-games-memory-card

-
2 stars 11 forks source link

Add basic classes to support multiple image collections. #15

Open pconrad opened 11 years ago

pconrad commented 11 years ago

Add some utility classes that will make it possible, instead of just one collection of images, you can have multiple collections of images.

This can be done at first WITHOUT changing the existing game, but as a standalone piece of code.

To avoid merge conflicts with others working on the main code:

edu.ucsb.cs56.projects.games.memorycard.cardsets

Copy build.xml to a separate file called build2.xml and when you type ant, use:

ant -f build2.xml compile 
ant -f build2.xml run
ant -f build2.xml clean
etc.

So that you don't have merge conflicts with build.xml. Similarly, if you want to put anything into README.md, instead of changing README.md, add a file README2.md that has your information in it.

= Background =

At the moment, the following lines have hard coded image file names:

  // The first 8 images. These will be used
    // for a 4 by 4 game.
    private String[] images8 = {
    "/images/200.jpg", "/images/201.jpg",
    "/images/202.jpg", "/images/203.jpg",
    "/images/204.jpg", "/images/205.jpg",
    "/images/206.jpg", "/images/207.jpg",
    };
    // The next ten images. These, in addition
    // to the first 8 will be used for a
    // 6 by 6 game.
    private String[] images10 = {
    "/images/208.jpg", "/images/209.jpg",
    "/images/210.jpg", "/images/211.jpg",
    "/images/212.jpg", "/images/213.jpg",
    "/images/214.jpg", "/images/215.jpg",
        "/images/216.jpg", "/images/217.jpg",

    };

That means there is only one set of memory cards that can be used with the game. That's dumb.

We should be able to play the game with any set of images that are of the correct size (pixel wise, width x height.)

It would be nice if we could bring up the game, and scroll through a gallery of images and select which ones we want to use---or have the computer randomly select images for us to use. Some selections might be harder than others, and this could be incorporated into a "leveling up" mechanism later on.

This issue doesn't get all the way there.. instead it creates some classes that form the foundation of adding that at a later stage.

= What we are doing instead of hard coding =

As an alternative, make a directory structure like this. (For the time being, this can exist side by side with the existing code.)

/cardsets
/cardsets/ragefaces
/cardsets/ragefaces/200.jpg
/cardsets/ragefaces/201.jpg
...
/cardsets/flowers
/cardsets/flowers/daisy.jpg
/cardsets/flowers/petunia.jpg
...
/cardsets/playingcards/
/cardsets/playingcards/AceSpades.jpg
/cardsets/playingcards/2Spades.jpg
...

The idea is that each of these directories will contain at least 18 images, all of the correct size (pixel wise.) Any image that is not of the correct size will be ignored (and a warning message will be printed on standard output.) Any subdirectory that does not contain at least 18 images of the correct size will be ignored (and a message will be printed on standard output.)

Create a class called MemoryCardSet that extends ArrayList. Add an extra attribute String directory, and a getDirectory() method. The constructor can take a directory such as "/cardsets/playingcards". It will then use code like that found here:

http://stackoverflow.com/questions/3923129/get-a-list-of-resources-from-classpath-directory

to traverse that directory. Have a static method called "isSuitableImageFileForMemoryCard(String filename") that returns true if a file is an image file of the correct size to be a memory card, and returns false if it is NOT an image, or if is the wrong size.

Pass every element of the directory to that method, and print a message on standard output for each one that is NOT suitable. For each one that IS suitable, add it to "this" (the ArrayList being constructed). If you don't reach at least 18 images before the directory is finished, throw an InsufficentImagesException from the constructor. The code invoking the constructor shoudl catch that exception and print a message on standard output that that directory didn't have enough images in it.

The next level up in the code is a class MemoryCardSetCollection which extends ArrayList. The constructor for this takes a directory, e.g. "/cardsets" and checking each directory under that, trying to construct a MemoryCardSet for each one (e.g. one for /cardsets/ragefaces, one for /cardsets/flowers, one for /cardsets/playingcards, etc.)

Make JUnit tests for both of these classes. You can create a separate directory called "test_cardsets" that has under it various directories with different numbers of files, sizes of images, things that are NOT images, etc. to test that your methods work. Check that the proper exceptions are thrown, etc. Some of your directories should be legal cardsets, and others should NOT be legal cardsets.

This issue is ONLY for creating these classes and the JUnit tests to check for legal vs. non-legal cardsets. Once that is done, the next step is to incorporate into the memory game.

~estimated 300

asyung commented 8 years ago

F16 OK (300 pts)