Final Conclusions

Over the past semester, I have learned more about the development in the Android OS than I ever thought I would. Nine projects over a semester is an accomplishment I am rather proud of. I feel that if I had invested a little more effort, I could have easily done 12 or more.

While doing this independent study, I also began learning to program for iOS. Learning both technologies at once allowed me to compare and contrast them, and I have decided to blog some of that here. First of all, Android’s documentation is without a doubt the best I’ve ever seen. Thorough examples, a large user-driven support community, and the fact that Java is a widely used language all are great strengths of the Android platform. Objective-C and the Cocoa framework are, however, part of a far more sophisticated environment. Animation and handling touch event callbacks are cleaner and more straightforward with iOS, and complex animation is not as thoroughly documented in Android. Indeed, most Android apps don’t take advantage of UI-enhancing animations, and this is most unfortunate. It is part of what makes iPhone apps more pleasing to the eye.

Another great feature of programming for iOS is XCode/Interface Builder. The two integrate so wonderfully with one another and automatically handle the separation of code into MVC. Lightning fast autocompletion as you type makes XCode far superior to Eclipse. To top it all off, iOS also boasts a speedy, responsive emulator to test code in.

One of the major drawbacks of making Android apps is that many different versions of Android must be supported across a number of different screen resolutions and hardware requirements, and this makes versatile app development into a bit of a nightmare. Thankfully, the target device for this undertaking is the Motorola Droid, so I designed all of these apps with that in mind. They will, of course, run on other Android devices, but the layout will likely be funky.

I wish I could have found more time to work on my graphical implementation of TopSpin. I feel that this is a very commercially viable game, and, assuming Time is generous, I will continue to develop it over the coming year.

Posted in Uncategorized | Leave a comment

Ghost

This game has so much potential to be awesomely fun. Ghost is a word game where the user and computer take turns constructing a word. The goal is to force the other player to finish the word. If you can do that, you win.

The user interface of this game is a tombstone on which the word is written. After making your turn, the Ghost appears, makes his addition, and disappears. For the background behind the tombstone, a simple black-to-purple gradient was used.

Screenshot of the game in actionThe program ddms, used to capture a screenshot of the emulator, seems to have trouble with text. I assure the reader that is the beginning of the word “ailanthus”.

My favorite part of this game was designing the graphics. I used Illustrator to create the Ghost images and the icon, then I used Photoshop to create the tombstone background. As usual, if people just NEED to know how I did it, they can ask and receive knowledge.

The data structure used in this project is called a Digital Search Tree. Each letter of a word is entered into this structure sequentially which allows for extremely fast look-up when testing whether the tree contains some word. The downside is that the number of pointers floating around causes the heap size to grow out of control. On a computer, this isn’t a big issue because we have virtually unlimited RAM (comparatively speaking). On the Android, however, we have only 100 MB. And it goes quickly. If we allow the application to attempt to read in the entire english-words.txt file and enter its data as a Digital Search Tree, the heap will reach the maximum size allowed by Android (16 MB), and it will be killed. To get around this, we broke english-words.txt up into a.txt, b.txt, c.txt, etc. The user is prompted for a letter, and upon entering this letter, the application reads in everything in <letter>.txt. The heap is no longer a tangled mess of pointers, and the load time is reduced considerably, allowing the user to go on with his or her merry playing.

This illustrates the importance of the Model-View-Controller pattern. Because our Views are kept separate from the Models and the Controllers, we can rewrite the way the application handles the words without affecting how a Digital Search Tree functions.

The icon is possibly the simplest I’ve done so far, but it is tied for my favorite with Go Fish. A few paths joined with a circular gradient. A couple more filled paths and then a few stroked paths, and you’re done.

Icon

The ghost was a little more work, but I took loads of inspiration from the Nintendo ghosts from Super Mario. Originally, I had wanted to animate the Ghost around the screen and fade in and out as the computer pretended to think of the next letter (it really just randomly chooses), but this proved to be more difficult than initially anticipated. Currently, it simply fades in, chooses a letter, then fades out to indicate it’s the player’s turn again. In the future, I would like to continue working on this game because I believe it has good commercial potential. And we all know about the seriously lacking state of games on the Android Market…

Ghost

Posted in Uncategorized | Leave a comment

Go Fish

Where to even begin with this one…I suppose I should begin by introducing the concepts of this project. So Dr. Drake’s implementation uses a subclass of the ArrayList class called GoFishHand. I subclassed his subclass and called it MyGoFishHand. The main functionality this added was the ability to click a card in your hand when the computer asks for it and watch it animate out and get added to the computer’s hand. It’s slightly discontinuous, but more on that in a moment. I also overrode the toString() method to MyGoFishHand.

Moving right along, we introduce the Android concept of Galleries in this project. A Gallery uses a custom subclass of the BaseAdapter class to map images to a position in the Gallery widget. In this case, we use a pointer to the MyGoFishHand object to map the rank and suit of each Card in MyGoFishHand to an image of a card in the drawable resources in the CardAdapter’s (our subclass of BaseAdapter) getView() method.

We use this same concept in the Gallery of the choices of card ranks the user can ask the computer for. The difference is that we use a different BaseAdapter subclass which we called ChoiceAdapter which maps each rank to images of  cards which I cut in GIMP (since Adobe decided to cancel my order for Photoshop and Illustrator) which look something like this:

For the computer’s hand, we subclassed Android’s ImageView class and added an updateImages() method which redraws their hand with the updated number of cards in their MyGoFishHand object using the Bitmap overlay trick which I had wanted to move away from. The DeckView is also just a simple ImageView subclass which draws itself as a face-down card if the deck has cards. This class differs, however, from when it was used in War. Instead of using a Queue, it uses a Deck object as its data structure model.

Another Android concept in this project is Animation. When the computer asks for a card, and the user has it in his hand, the user must click all the appropriate cards in order to give them to the computer. This is the reason for the subclass of Dr. Drake’s GoFishHand. If we use his give() method, all cards in our hand of the requested rank are removed instantly, and it isn’t clear which cards are still in the user’s hand and which have already been given to the computer. Using my give() method, a card is selected, aniamted off-screen, and given to the computer’s hand. Here, the updateImages() method is called to update the computer’s hand view.

There is still a bug in the latest revision of this game (currently 111 or 112). It seems almost random when it happens, though bugs are seldom, if ever, random. When giving up your cards to the computer, sometimes when you click a card and before the animation starts, the entire Gallery containing your cards will disappear. You can sometimes bring it back (if that was the only card of that suit in your hand) by pressing the GoFish button. After doing so, your Gallery will reappear with the selected card transposed on top of another card in the deck and then animates downward. Then all is normal again. If anyone checks out the code and finds a solution, please post it as a comment.

The real pride I take in this application is the icon. After finding a picture of a clownfish, I fired up Illustrator and drew a vector version. The first revision had one big white bubble around the fish, but I didn’t like it when I exported it, so I drew some smaller bubbles and made them blue and put them around the fish. This definitely took the longest of any icon so far, and that’s why it gets a much larger resolution image posted here rather than the 72×72 version. As usual, the grueling details of the creation of this icon is outside the scope of this article, but if someone really wants to know, I’ll be happy to walk them through it.

Posted in Uncategorized | Leave a comment

War!

In the game of War, two players each have a stack of cards. Each player turns up the top card, and the player with the higher ranking card takes both cards. If the two cards are the same rank, then a war is declared. In war, both players lay three cards face-down and then lay down a fourth face-up. If these two cards are the same, this process is repeated until the cards are different or someone runs out of cards. The player with the higher card gets all of the cards involved in the war. The winner is the player who gets all the cards. I go to such great length to explain this game because if the reader is like me, then he or she has never heard of this card game until now.

This project introduces ArrayQueues which are an implementation of the Queue interface because we draw cards from the top but add them to the bottom of our decks. I incorporated some of Dr. Drake’s code (specifically, the settledByWar() and the give() methods–two beautiful works of art) into my WarActivity to handle wars with more elegance than I initially coded.

Again I used the trick of converting image resources to Bitmaps when there was a war and stacking card edges to give the illusion of a pile of cards which was animated offscreen when someone won a war. The face-up cards were drawn in the CardView rather than down in the WarViews. The reasons are that it was too troublesome to interpolate part of a card image among the card edges and it was also difficult for the user to see which cards are being compared in a war. Moving all face-up cards to the CardView allows the player the chance to know what card(s) he (or she) is losing or gaining.

One unsolved mystery in this project is how to control the size of the generated Bitmap images. For an unknown reason, when Bitmaps are generated and displayed, they are much smaller than the image resource which was decoded. If someone knows how to control this, please feel free to post a solution.

I also decided that this would be the project which introduces simple animation. After all, Toast popping up all over the place to notify the player(s) who just won some cards gets annoying. So using XML, I declared a translation animation with a delay of 2 seconds to allow the players to absorb who actually won before animating the cards offscreen to the winner. I chose the acceleration interpolator because it is quite pleasing to the eye. A couple of issues arose here, however. First of all, the zIndex XML attribute did not seem to work. This attribute is to force an animation to occur at a specific layer of the layout, in this case, above the players’ decks. However, I found that the cards still animated over and under the DeckViews regardless of the value of zIndex. After playing around with numbers, I rearranged my layout declaration so that the DeckViews were declared first and the CardViews after so that they would slide over the DeckViews by default.

Once this issue was hammered out, I tackled the odd issue of the animated View being redrawn in its original position upon completion of the animation. An AnimationListener must be used to hide the animated View (if it went off-screen) upon completion of the animation to achieve the desired effect.

Again I had no idea what to do for an icon, so I flipped some cards around, altered their perspective, and with a sharp “schwack, schwack” sound, I slapped it together in Photoshop. It should be noted that the rough edges don’t show up on the Droid screen, but they result from changing the perspective of a PNG image rather than cutting the icon in Illustrator from nothing.

Posted in Uncategorized | Leave a comment

Idiot’s Delight

My faith in public domain works was not shattered during this project because (praise the nondenominational, nonspecific deity who may or may not exist) Dr. Goadrich and I were able to find a set of images of a deck of cards licensed for public use. The filenames, however, contain only numbers. This makes Java very unhappy since each filename becomes a reference to a resource ID which becomes a variable name and, as such, must follow the same rules as variable names. To get around this and achieve the same ordering of cards, I wrote a quick Perl script to prepend an ‘x’ to each filename. Java was happy once more. Faced with the tedious task of composing an array of each of these resource IDs, I decided to use Perl once more to generate a comma-separated list of strings which fit the pattern “R.drawable.x%d” where %d is an integer from 1 to 52. I copied and pasted this into an array called cardIDs. This is a data member of each CardView object which is just a GUI wrapper for a Card or a Stack of Cards, as was the case here. The ordering of the cards was a little off from what I would have wanted, but I was able to work with it. The aces were first, followed then by the rest of the cards in descending order. I could have ordered the cardIDs array differently, but I chose to keep it in the same order as the images themselves.

Within CardView, the updateImages() method checks the rank of the card, and if it’s an ace, its image is selected appropriately based on its suit. For all the other cards, there is a pattern to the files: after every fourth card, a lower rank begins, and the cards’ suits follow the pattern clubs, spades, hearts, diamonds. So if we multiply the rank of the card (less 1) by 4, then add the suit (plus 1, mod 4), we get the position of the card if the resource IDs were in ascending order. So we subtract this number from 52 to arrive at the appropriate image resource ID for the given Card at the top of the Stack of each CardView.

Parallel to the development of CardView, I wrote the deckListener OnClickListener which listens on the DeckView object for clicks. The DeckView simply displays the backside of a card. If clicked and not empty, it deals out four Cards to each of the Stacks within the CardViews and notifies them to update the images. If the DeckView’s deck is empty, however, it will throw an IllegalMoveException. This we catch in the onClick() method of deckListener and make Toast to tell the user that the deck is empty.

Once the CardViews were displaying the correct images, the next step was to allow a maximum of two CardViews to be selected at any given time. Because this number is specific to this particular game, I decided this should be handled from within the Activity. So CardView was given isSelected() and toggleSelected() methods. Then in IdiotsDelightActivity, I created an OnClickListener to be applied to each of the CardViews called stackListener. In the spirit of teaching, I used a Stack (java.util.Stack, not Dr. Drake’s) called clicked to keep track of the currently selected CardViews. There are numerous conditions for which to check in the onClick() method of stackListener. (1) Is the CardView’s Stack empty? (2) Are there two CardViews selected, and (3) was the CardView that was just clicked already selected? (4) Is there a CardView selected, and was it just clicked? (5) If we couldn’t answer any of these positively, we must add the CardView that was just clicked to the clicked Stack.

(It looks much sharper on a Droid screen, I promise)

After selection rules were in place, actually removing cards from the Stacks within the CardViews followed. A big, card-sized Button which says “Remove Cards!” is what the player pushes after selecting two cards to try and remove one or both. This registers with the OnClickListener called removeListener in IdiotsDelightActivity. It checks that two cards are selected (if not, Toast pops up notifying the player), then tries to remove a low card. If that throws an IllegalMoveException, we catch it and try to remove a pair. If that throws an IllegalMoveException, we catch it and make some Toast that tells the user there isn’t a removal rule for those two cards. We can do this in one method because of the rules governing the removal of cards and because we cannot have two cards that are the same suit AND the same rank when only playing with one deck.

Dr. Goadrich and I disagreed a bit on the selection graphics. I wanted the cards to be opaque until selected, and he wanted the cards to be translucent until selection. He won out, but perhaps it would have been better to have a halo effect when a card is selected so that they will always be opaque. This is, however, a minor detail and can be easily changed in the CardView class. And it is here that the immense value of the MVC programming practice is driven home. With a few minor changes, the entirety of CardView can be reused in later projects requiring a GUI deck of cards.

I designed the icon last because I didn’t really have much inspiration for it, so I just fired up Photoshop rather than Illustrator, opened four card images, changed their perspectives a bit, distorted them to look a bit thinner, and layered them into one 72×72 transparent background, scaling it for lower resolution screens to 48×48 and 36×36. It turned out prettier than expected, so I decided to keep it.

Posted in Uncategorized | Tagged , , , , | 1 Comment

Beetle Game

I would like to begin by saying how frustrating it is that I must create a tail image for the beetle and also manage its visual presence in code. If the reader is unaware that beetles do not have tails, he or she should spend some time outdoors getting to know nature a little better. This was only the tip of the iceberg, though.

With some of the issues from Domineering in mind, we decided not to use the BeetleGame class and instead to keep track of the current player using the player integer (0 or 1) within our classes rather than Dr. Drake’s. Because the Beetle objects keep track of their current state, as well as provide methods through which to alter the state of the Beetle, the only required changes were some methods to access the class’s data members: getBody(), getEyes(), getFeelers(), getHead(), getLegs(), and getTail().

However, the Beetle object provides only a CLI for displaying the current player’s bug. So in order to use the Dr. Drake’s Beetle, I created a wrapper which is a subclass of ImageView that contains a reference to said object which exists within BeetleGameActivity‘s scope. Rather than override the onDraw() method, I decided to create a public method so that another class can manage all the interactions and notify the ImageView to update. I called it updateImages(). Using this forum post, I made this method construct and paint a Bitmap image based on the “true” data members of the Beetle object which we can access through the above access methods. After each data member has been scrutinized, the method finally sets the image of the ImageView using the setImageBitmap() method of any ImageView to the Bitmap which was created from the image resources (each of which contains a single part of the beetle).

Next was the DieView. This contains a reference to Dr. Drake’s Die object which lives in BeetleGameActivity. Originally, the DieView was to make calls to the other Views when the Die changed its state to update the BeetleViews accordingly. Here the first big problem arrived. Sibling Views cannot use the amazingly handy findViewById() method inherited from certain Android classes to obtain handles to one another. They may only use this method to obtain handles to their children. So instead we restructured the entire project to more closely adhere to MVC principles. The BeetleViews are now stored in an array of length two inside BeetleGameActivity (along with the Beetles, the DieView, and the Die) so that the player variable may be used to access the appropriate View depending on whose turn it is. I used the setColorFilter() method to change the color of the other player’s Beetle to grey when the turn changes and the opposing clearColorFilter() method to restore color to the appropriate player’s Beetle. The Beetles are also stored in an array of length two so that we can access and alter the correct Beetle using the player variable.


I decided (against Dr. Goadrich’s insistence on run-time 2D graphics) to use 200×200 images, each of which has a beetle part in the correct place so that the only thing for the device to do is display the images in the customized BeetleView and in a specific order (defined in the updateImages() method of BeetleView) to give a layered effect. When these images were displayed alongside the DieView (more on that in a moment), the image was cropped, and only the upper left corner was visible. However, my idea to overlay the images worked. The problem was that the BeetleView‘s layout attributes were “wrap_content” and had no scaleType set. So after changing the layout_width and layout_height to “200sp”, I experimented with different scaleType values to determine which best aligns and fits the images in the ImageView, ultimately deciding on “fitEnd”.

The result is a blog post which looks a little too much like Christmas and a Beetle icon which does not, in fact, have a tail!

Posted in Uncategorized | Tagged , , , | Leave a comment

Domineering

The difficulty I foresaw in this undertaking was a graphical data structure which displays the state of the game board. We didn’t want to implement a heavy game engine for such a simple game, and we didn’t want to make the user interface overly complex. Thanks to the GridView class from the Android API, we can create the illusion of having a board while making use of the already existing onItemClicked method which has as a parameter the position (as an index on the grid) of the user’s finger which touched the View. This is most helpful as it alleviates the extraneous arithmetic involved in determining the index from a MotionEvent alone.

The graphics are relatively simple. I created the players’ tiles in Adobe Illustrator using rounded rectangle paths with no stroke and circular gradient fills of red and blue. Nothing impressive, just something to say “I dominate this spot.” Using the ImageAdapter class (custom subclass of BaseAdapter from the above tutorial) which has an array of Integer references to drawables, I used a less colorful tile to indicate which spaces are free for the current player. The images in the ImageAdapter’s Integer array map to specific indices on the GridView. Using the OnItemClickListener in the DomineeringActivity, we can alter the cells in the GridView using the position parameter of OnItemClick and a pointer to the ImageAdapter in the GridView. After altering the underlying map, the adapter is notified that its data set changed and is refreshed accordingly.

The problem with all of this is justifying the presence of an additional object (Domineering object) which contains an additional copy of the state of the game already described by the map contained in the ImageAdapter class. The only additional functionality required is a way to keep track of the players’ turns (which is easily done with an integer % 2). So Dr. Goadrich’s idea was to replace the array of resource IDs inside the ImageAdapter with the array holding the state of the board inside the Domineering object and use an if-statement to set the appropriate tile color. He also did away with my colored distinction between vertical and horizontal using red and blue tiles. All the players’ tiles are blue now.

I had to slightly augment Dr. Drake’s code in this application by adding a few accessor methods. isCovered(int row, int column) returns as a boolean whether or not the space at (row, column) is covered, getPlayer() returns the player of the current turn as a boolean, and nextPlayer() sets player equal to !player. I also brought the player variable into class-wide scope so that other methods besides play() could change whose turn it is.

The last thing I did was include an options menu that displays help options or lets the user clear the board and start a new game. We do this with a couple of Android method overrides, namely, onCreateOptionsMenu(Menu menu) and onOptionsItemSelected(MenuItem item). The first allows us to inflate the options menu which we defined elsewhere in an XML file, and the second allows us to react to the selection of an item on said menu. The layout of the XML file is relatively simple. The root element is a menu, and each entry between those tags is a menu-item which has an ID, an icon, a title, and a boolean that allows us to select the item once the list is displayed.

The icon for this game is just a 3×3 board with a simple case where vertical wins. If you’ll notice, however, the colors are arranged according to 2/3-rule. 2 blue for 1 tan column, then 2 blue for 1 tan in the bottom row. 2/3 always makes for a much more balanced image, as odd as that sounds.

Posted in Uncategorized | Tagged , | 1 Comment

Licensing and Code Alteration

As we begin work on an implementation of Domineering, we have encountered the issue of licensing we had hoped to avoid.  Others could probably state this more technically than I, but here is our issue.

  • We want our code to be open source, under the GPL, so anyone can use and modify it as they see fit for their courses.
  • The text we are using has all the code for the textbook projects posted on the author’s website for anyone to download, but it has no license attached at all. This code is definitely copyright to the author, but beyond that, since it is also listed in the textbook from a publisher, the license is murky at best.
  • So if we use this code in our projects, we’ll be adding a license that the author didn’t grant.

Our solution so far has been to write projects that do not make any changes to the underlying code, and most are written to make this feasible. So we include the Activities, Views and other object we create, but do not check in to our GPL repository any of the code from the text.

With Domineering, the original code as written does not include a method for a user of the Domineering class to get the state of an individual square on the board. This is not a problem in the book, because it is only the toString method which is used to visualize the game, and it has access to data members implicitly.  For our Android project, we will need to have access to this internal state, so we can properly display the game to the user.

So, we have a few choices.

  1. Add in a new accessor method to gain the state, which involves us adding the new code to the repository somehow.
  2. Somewhere tell users to add the new method when they use the project.
  3. Extend the Domineering class, add the method to our new DomineeringGraphics class, and add back in the private data member for the board.
  4. Skip this project.

I am reluctant to follow 4, since this is a great example of using arrays in Java and would work well as an Android app.  3 seems the best with licensing issues, but if an instructor wanted to assign exercises 2.18 and 2.19 in the text, which require the students to alter the Domineering class to fix edge cases and allow for variable size boards, then our example becomes cumbersome.  2 is fiddly, but the user already has to alter the textbook code to add package information at the top.  1 looks like the best choice to me, if we can work out licensing issues with the publisher.

Posted in Uncategorized | Tagged | Leave a comment

LightString

A basic description of Dr. Drake’s version is a string (in the figurative and literal computer science meanings) of lights which can either be white, colored (represented by ‘R’, ‘G’, and ‘B’), colored and flashing, or off. This example is ideal for implementation on the Android OS, and the lessons it aims to teach are very important to object-oriented programming. Namely, inheritance and polymorphism.

This project brings in some new graphics which I created in Adobe Illustrator by deviating slightly from a great tutorial. The excruciating details of how I created the images are outside the scope of this blog, but if someone would really like to know, I would gladly make a second post.

The ColoredLight and FlashingColoredLight classes both have as their parent the Light class (which contains basic information relevant to all lights), and so in the spirit of learning, as well as simplicity, I decided to create a subclass of ImageView and call it BulbView. I reached this design decision because implementing an OnTouchListener for each lightbulb creates the problem of determining which image was actually clicked. I found we can obviate this by subclassing the ImageView and implementing an OnGestureListener instance which alters the image resource displayed based on the current state of the Light child. Then we just add multiple instances of this BulbView class to the LinearLayout. I also decided to encapsulate the Light child inside the BulbView class. This way, all changes to the state of the Light object are handled through touch events with the BulbViews.

The layout of this Activity largely follows that of DominoFlip in that the parent element is a RelativeLayout with a TextView at the top, followed by a LinearLayout which will contain our light bulb graphics, then another LinearLayout containing our randomize Button, a Button that turns all lights on, a Button that turns all lights off, and a ToggleButton for toggling a blinking effect. The BulbViews will be dynamically created and added to the LinearLayout at run-time in the onCreate method of LightStringDemo.

When I began writing the BulbView class and testing it, however, I discovered that the GestureListener wasn’t working! So I scoured the internet and found that the onDown method of the OnGestureListener must return true. This makes little sense to me, but I guess that in order to reach the “up” status, one must first have reached the “down” one. Regardless of whether onDown returns true or false, though, my finger still touched the screen, and it is my opinion that this should be reflected no matter the return value of onDown.

Another task where I encountered some trouble was using a thread to update the UI. The application kept force-closing when I originally wrote the thread to access the BulbViews and alter their image resources, so I read around the Android APIs that one must create a Handler object to which a Runnable is passed in the Handler’s post method. This Runnable contains the code which alters the UI.

Another new concept which was added in this project was the blinking facilitated by the Timer and the TimerTask objects. We use the initialized Timer object to schedule a TimerTask to run with no delay (0 ms) at a specific rate (every 1000 ms). So at the beginning of every second, the TimerTask posts the Runnable code to the Handler so that the BulbViews will change their Image Resources and give the appearance of turning off and on.

The final difficulty came in ensuring that the state isn’t lost when the orientation of the phone changes. After consideration, we decided it would be best if this application were forced to run in portrait mode. However, setting orientation to portrait in the manifest file wasn’t enough. If the device has a physical keyboard, the state will be lost when the keyboard slides out. In order to get around this, we found that we can specify in the manifest under our activity’s attributes ‘android:configChanges = “keyboardHidden|orientation”‘. This allows the activity to keep the same lights through all orientation changes.

The icon is a three-bulb string of lights featuring red, green, and blue bulbs.

Posted in Uncategorized | Tagged , , , | Leave a comment

Domino Flip

Domino Flip comes from a Java assignment in Dr. Peter Drake’s Data Structures and Algorithms in Java textbook where the reader is to program a domino object which, for all introductory purposes, behaves like a physical domino. The goal of this project for the student in Data Structures is to create two implementations of a single Domino interface that function with a certain opacity. That is to say that the two implementations have the same methods of interaction with the outside world but accomplish their tasks in different ways which are not visible to the outside, namely using fields versus arrays to store data. For more information on the non-Android project, please delve into Dr. Drake’s wonderful Data Structures and Algorithms book.

By using pre-existing graphics in the DieRoller project which Dr. Goadrich created, I simply used Adobe Illustrator to create a domino background image which joined two dice images. The result was a visually striking maroon- and bone-colored domino which gave me some difficulty in centering without stretching the image to fill the entire LinearLayout in which it lives. When I changed the layout_width attribute in the XML layout file to “wrap_content”, I lost the center alignment of the Layout. But if it was “fill_parent”, the image was unrealistically wide for a domino. The solution I found was to change the layout_width to “wrap_content” and set the layout_centerInParent attribute of the LinearLayout to “true”, the parent being the RelativeLayout surrounding it.

This problem didn’t exist in landscape mode because I decided to align the domino to the left side of the screen so that all the components would fit on the screen without the need to scroll.

To the two sides of the domino, I attached an implementation of View.OnTouchListener so that when a persons’s finger touches the domino, the two sides are flipped. I would have liked to use OpenGL to animate an actual flip, but I thought better of it for such a simple project. Perhaps next time.

In order to show the student the visual properties of polymorphism (i.e., that you can’t tell what implementation is being used [usually]), I added a RadioGroup which allows the user to switch between the Domino implementation which uses fields to store information and the other implementation which uses an array to store information.

And of course the user should always be able to get a new domino by clicking the random button which does exactly what one would imagine. It should be noted that if one cannot imagine, one should not be allowed within 20 feet of an internet-connected device.

Perhaps the most difficult aspect of this project was finding a simple solution to graphically displaying dominos using resources that were already at my disposal. Within that, the most difficult problem was aligning the images correctly. However, the goals of this project were indeed met in that the graphical wrapper I have written allows one to use the same underlying data structure from Dr. Drake’s book with no changes to create an Android demonstration of interfaces and polymorphism.

Lastly, the icon is simply a four-pip half-domino image created in Adobe Illustrator seen immediately below.

Posted in Uncategorized | Tagged , , | Leave a comment