Saturday, August 24, 2019

The Importance of Testing

Before you write any code you should have a way in your mind to test the code in order to make sure that the code does what you want. There are many types of tests but when you get right down to it, all tests are either manual or automated. Manual tests tend to be the easiest to implement as they just require some code that can be observed to be doing the correct thing. As games tend to be dynamic in nature, manual testing is very common in game development as coming up with automated tests is not always practical. Automated tests, however, are always preferable when possible to do so due to the simple fact that people do not have the time to go through hours of manual testing just to make a minor change. This means that most manual tests will only be run on rare occasions so that something may be broken for months before it is discovered making finding out why it broke difficult.

Due to the visual nature of the cards, automatic testing is not really that practical so we will resort to manual testing. More to the point, as the cards are poignant to the game, any problems with the card class will be detected immediately so manually testing them is not much of a risk. A very simple test program can be created to make sure that the card class works the way it is supposed to.

Create a new “CardTest” movie for holding the test. Copy the Card movie clip into this movie. This can be done by having a file with that movie open in another tab then in the library tab selecting that file and dragging the Card movie onto the stage. Make sure that the layer holding the card extends to the second frame. Add a layer for holding code and in the second frame create a blank keyframe in the code layer. Right-click and chose add actions to bring up the code editor for editing JavaScript on that frame. Here is the test code:

var firstCard = new spelchan.Card(this.firstCardMovie);
firstCard.setCard(1);
this.firstCardMovie.addEventListener("click", nextCard);
this.stop();

function nextCard(e) {
if (firstCard.faceShowing)
firstCard.setCard(firstCard.cardID + 1);
else
firstCard.showFace();
console.log("face: " + firstCard.getFaceValue() +
" suit: " + firstCard.getSuit());
}

While in the actions editor, make sure to add the Deck.js file to the includes by expanding the global list of scripts then selecting the include option and clicking on the + to add a script. Browse to find the script.

You can now simply click on the card to show the first card in the deck and keep clicking on the cards to loop through all the cards that make up a deck. This is a very simple test but one that confirms that the Card class is working as expected. Proponents of the technique known as test driven development would start with the test code and then write the actual code as we will see when creating the deck class that deals with handling a deck of cards.

Saturday, August 10, 2019

Creating the Card Class

We will place all the code for the Card movie in an external JavaScript file. For some strange reason, Animate CC 2017 does not let you edit external JavaScript files (hopefully an over-site that will be fixed in future versions) so another editor will have to be used for editing the JavaScript file. There are numerous editors available that support JavaScript, with Notepad++ being the editor of choice for me when I am using a windows machine.

Before we can create the card class, however, we are going to want to create a separate namespace for holding our classes so that there are no conflicts with any other code that we are using. The easiest way of creating a namespace is simply to create a root object with the name of the namespace we wish to  use. This can be done as follows:

spelchan = spelchan | {};

You should already be familiar with the new operator, which is used for creating instances of a class. The format for this is

 new constructor([param1,...,paramn]);
 
What is happening is the new operator is trying to find a constructor function named whatever name was provided with the number of parameters indicated. Some constructor functions, such as Array, are built right in to JavaScript. It is, however, possible to create new Constructor functions by simply having a function named whatever it is you want to call the constructor. Convention has it that you start constructor functions with a capital letter. This is a good idea as it distinguishes them from other functions.

Constructors can have no parameters, or as many parameters as you desire. Here is the constructor function for our card class. Notice that it takes a reference to the movie clip that it will be controlling. We could have had the class create the movie clip but by passing the movie clip we make it easier to attach objects from the Animate CC editor to our class.

spelchan.Card = function(cardMovie) {
this.cardMovie = cardMovie;
this.cardID = 0;
this.faceShowing = false;
this.cardMovie.gotoAndStop(9+this.cardID);
}

As a class consists of data and the methods used to manipulate the data, the above constructor sets up a holder for the card movie, the id of the card, and a Boolean flag that indicates if the card is being shown. By default, the card is blank and is not being shown to the user. To set the card and show the card, we need to have some methods. This is where JavaScript gets kind of weird.

All classes are given a prototype object that holds functions and other variables that are available to any instance of that class. To add a function to a class, you need to add the function to the prototype object. This can be done two ways. First, you can create the function directly in the constructor by assigning the function name to the code for a function. The other way is to assign a function to the prototype, by using classname.prototype.functionname = function(...) { ... };

Let us implement the setCard and getCard methods. When the card’s value is changed, the card has to go to the proper frame of the movie and stop. However, if the card’s face is not showing, we show the back image instead. Getting the card value is simply the matter of returning the cardID variable.

// n is the cardID for the card
spelchan.Card.prototype.setCard = function(n)
{
if ((n < 1) || (n > 52))
this.cardID = 0;
else
this.cardID = n;
if ((this.cardID == 0) || (this.faceShowing == false))
this.cardMovie.gotoAndStop(1);
else
this.cardMovie.gotoAndStop(9+this.cardID);
}

// returns the cardID for the card
spelchan.Card.prototype.getCard = function()
{
return this.cardID;
}

We now have the ability to change the card but as the face is not being shown, we have no way of verifying this. Showing a card is simply a matter of setting the faceShowing variable to true and going to the appropriate frame. The only complication is the fact that we need to handle a card that has yet been assigned a value. Hiding the face is even easier as we set faceShowing to false then stop on the blank card frame.

// shows the face side of the card
spelchan.Card.prototype.showFace = function()
{
this.faceShowing = true;
if (this.cardID == 0)
this.cardMovie.gotoAndStop(1);
else
this.cardMovie.gotoAndStop(9+cardID);
}

// shows the back of the card
spelchan.Card.prototype.hideFace = function()
{
this.faceShowing = false;
this.cardMovie.gotoAndStop(1);
}

This is all that is needed for a card class, but creating a couple of utility methods for getting the face of the card and the suit of the card is easy enough to do. This is done using math. The modulus function, which uses the % operator,  gives you the remainder of a division. Had we started counting from 0, this would work great, but as we didn’t subtracting one from the card id lets us use modulus to get the face value from 0 to 12 and adding 1 corrects this. A similar technique can be used to determine which of the four suits are being used by seeing which group of 13 the card falls into.