Next page | Contents page |

Better testing

So far, to be honest, I have mostly tested the game by running it. I always have the browser's developer console open to see error details. That has always enabled me to fix problems and move on. But now we have a need to set up special card arrangements to be able to test suit completion. It would be good if we had a regular way of doing that so that various arrangements can be tested at any time.

We need to be able to set up the columns, using this kind of thing (pseudocode):


	for each of the 10 columns
	{
		add card (7, clubs, face down)
		add card (Q, diamonds, face up)
		... several times
	}

and then continue the game as if that had been dealt.

Rather than repeatedly writing that as a series of function calls we need a data structure that can be scanned, or parsed, to do what is needed. The structure needs to be concise. I suggest this:


const TEST_DEAL_1 =
[
  "7cd,qdu",
  "asd,9sd,3cd,kcu,qcu,jcu,10cu,9cu,8cu",
  "7cu,6cu,5cu",
  "2du",
  "2dd,3sd,qdd,qdu",
  "5dd,6dd,7du,4cu,3cu,2cu,acu",
  "kdd,asu",
  "",
  "10du,4su",
  "ksu"
];

That's an array of 10 columns expressed as concisely as possible. It was quick to write and it would not be difficult to write a parser for it, which we will do next. I hope you can see that moving the 3rd column onto the 2nd and then moving the last 4 cards of the 6th column onto that would create a complete suit, which we need for our current test (from the previous page).

(I rejected JSON as a data format because it would not be so easy write quickly and accurately.)

This is how the test data can be parsed:


function arrangeCards (test)// test is an array of columns
{ if (test.length !== GAME.N_COLUMNS)
  { alert ("test data wrong length for arrangeCards()");
    return;
  }
  for (var i = 0; i < GAME.N_COLUMNS; i++)
  { var col = GAME.COLUMNS [i];
    col.empty ();
    var data = test [i].toLowerCase();
	                    // string could be any case
    if (data.length > 0)
    { var chunks = data.split (",");
      for (var j = 0; j < chunks.length; j++)
      { var chj = chunks [j], suitLetter, valueString, up;
        switch (chj.length)
        { case 3:
            valueString = chj.charAt (0);
            suitLetter = chj.charAt (1);
            up = chj.charAt (2);
            break;
          case 4:
            valueString = chj.charAt (0) + chj.charAt (1);
            suitLetter = chj.charAt (2);
            up = chj.charAt (3);
            break;
          default:
            alert ("Test item " + chj + "not length 3 or 4");
            return;
        }
        var suit, value;
        switch (suitLetter)
        { case "c": suit = SUITS.CLUB; break;
          case "d": suit = SUITS.DIAMOND; break;
          case "h": suit = SUITS.HEART; break;
          case "s": suit = SUITS.SPADE; break;
          default: 
            alert ("Unknown suit letter in " + chj);
            return;       
        }
        switch (valueString)
        { case "a": value = 1; break;
          case "j": value = 11; break;
          case "q": value = 12; break;
          case "k": value = 13; break;
          default: value = parseInt (valueString);
        }
        var card = new Card (suit, value);
        if (up === "u") card.reveal ();
        else card.hide ();
        col.addCard (card);
} } } }

Using that I was able to verify that Column.endsWithFullSuit() does correctly identify column ends as complete or not.

Next page | Contents page |