Three of our four classes for this project are fairly easy to get started since we already have a working Java BlackJack program.
The “Card” Class
While the Card class from our previous project is mostly correct, we have a couple of alterations to make that are necessary in order to make a link between the names of our cards and the pictures. Unfortunately we’re not able to use numbers in the names of our pictures and so we need a way to refer to the names we have used.
Start by adding a new Class to your project and copy and paste the Card code from our previous project. (This may result in some peculiar imports being included in your code so it may need tidying up, you should be able to just delete those extra imports).
Where we defined the cardName variable we had a section of code that said:
if (cardRank < 10) { cardName = Integer.toString(cardRank);
We need to change this so that each card under ten actually has a name, i.e:
if (cardRank == 2) { cardName = "Two"; } else if (cardRank == 3) { cardName = "Three"; } else if (cardRank == 4) { cardName = "Four"; } else if (cardRank == 5) { cardName = "Five"; } else if (cardRank == 6) { cardName = "Six"; } else if (cardRank == 7) { cardName = "Seven"; } else if (cardRank == 8) { cardName = "Eight"; } else if (cardRank == 9) { cardName = "Nine";
We now need to change the printCardVal method so that the String created matches our card names, i.e:
public String printCardVal() { if ((cardRank >= 2) && (cardRank <= 14)) { return cardName.toLowerCase() + suitName.toLowerCase(); } else if (cardRank > 14) { return "invalid"; } else { return "invalid"; } }
And now the Card class is perfect!
The “Deck” Class
The Deck class requires absolutely no change from our previous tutorial so go ahead and make an identical one in this project.
The “Hand” Class
Start by making a Hand class in this new project by copying over the code from the previous tutorial. Once it’s copied over, we only have to make one small change at this stage. The showHand method isn’t correct since it tries to write to the console, so for now just change that method to be something like this:
public String showHand(boolean cardOne) { return ""; }
We’re basically just acknowledging we will need a showHand method but we’re not going to define it yet.
And now we need to incorporate the BlackJack code into our Android application. This occurs inside the “StartActivity” class and so let’s jump back to it…