The showOutcome() function will be created to, as the name implies, show the outcome of the game. The first parameter will be the player who's button was pressed. The second parameter is a boolean to indicate if the button press was a winning press (true) or a misdraw (false).

By generalizing the outcome code, to be able to show the proper NeoPixels for either player and for either outcome, we make our code more compact. Otherwise we would need to duplicate a lot of code in more than one place. It also makes it easier to change the behavior of the code in the future.

The complete code for the showOutcome() function will be shown at the end. Here, we'll go through it in pieces. First, we need to declare it:

void showOutcome(int player, bool winner) {

We aren't going to return anything, so the return parameter is void. We take in two parameters: the player who's button was pressed as an int, and whether this was a winning game or not as a bool.

Next, we create a couple of variables to be used locally within the function.

  int p1, p2;
  uint32_t color;

Then we turn off all of the NeoPixels, just to insure a known state.

  // Turn them all off

Then we set the pixel color depending on the game outcome, green for a winning game, red for a misdraw.

  // Set pixel color
  if (winner) {
    color = 0x00FF00;
  } else {
    color = 0xFF0000;

Then we set the range of NeoPixels to be lit up based on which payer pressed their button.

  // Set pixel range for player
  switch (player) {
    case 1:
      p1 = 0;
      p2 = 4;
    case 2:
      p1 = 5;
      p2 = 9;
      p1 = 0;
      p2 = 9;

Now we have a color for the NeoPixels, and which ones to turn on, so do that.

  // Show which player won/lost
  for (int p=p1; p<=p2; p++) {
    CircuitPlayground.setPixelColor(p, color);

And why not play a little tune. A happy one if this was a winning game, a more error sounding one if it was a misdraw.

  // Play a little tune
  if (winner) {
    CircuitPlayground.playTone(800, 200);
    CircuitPlayground.playTone(900, 200);
    CircuitPlayground.playTone(1400, 200);
    CircuitPlayground.playTone(1100, 200);
  } else {
    CircuitPlayground.playTone(200, 1000);

And we are done with the game, so we'll just sit here forever until the reset button is pressed to start a new game.

  // Sit here forever
  while (true) {};

And don't forget the closing curly bracket to finish off the showOutcome() function.

OK. Let's put it all together.

This guide was first published on Dec 29, 2016. It was last updated on Mar 08, 2024.

This page (Show Outcome) was last updated on Dec 09, 2016.

Text editor powered by tinymce.