From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Random 2D Color Array for Game of Concentration in LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

This VI generates an array of random colors, for a given number of rows and columns.

 

Description

This VI is just a fun VI that I created while teaching LabVIEW Basics I.  It works well for highlighting subVIs, array manipulation and shift registers, all key concepts in Basics I.  I'm sure there are more efficient ways of accomplishing what I am doing here, so if you have a suggestion, let me know.

 

This VI generates a list of random colors and then inserts the color twice in a 2D array.  The user is able to specify the size of the output array and the array will resize based on the user's dimensions.  Each color is inserted twice so the resulting 2D array would resemble a game of concentration or memory with all of the cards flipped up.

 

 

Requirements

LabVIEW 2012 (or compatible)

 

Steps to Implement or Execute Code

  1. Input the number of Rows and Columns for your game of concentration
  2. Run the VI
  3. The number of Rows and Columns will be adjusted to get an even number of fields in the array
  4. The application will create pairs of cards with random color and distribute the cards randomly over the field to create a game of concentration

 

Additional Information or References


**This document has been updated to meet the current required format for the NI Code Exchange.**

ColeR
Field Engineer

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
altenbach
Knight of NI Knight of NI
Knight of NI
on

>  I'm sure there are more efficient ways of accomplishing what I am doing here, so if you have a suggestion, let me know.

Well, here's a much simpler version that does the same, however we would not learn about loops and shift registers and such . (The

last two functions are riffle and reshape)

I have a couple of suggestions, for example:

  • where you generate the random colors the valid output is 0...255, however, you multiply a 0..1 random number by 256 and then round to nearest, meaning there is a nonzero chance that the result is out of range. You might want to round to -inf instead.
  • Similar problem when you randomize the indices. (index 0 and max have half the probability of the other values)
  • Resetting the color array to defaults is useless, because it will get overwritten by the new data within nanoseconds. Why even do it?
  • The rows/columns control should have reasonable defaults when the program is loaded (not zero!).
  • The color array should be an indicator, eliminating the local variable.
  • Even to implement a game, it should remain an indicator, because we don't want the user to change the colors. To play we would use a mouse-down event on the indicator and programmatically uncover a square, for example (similar to my tic-tac-toe here).
  • Generating 24bit colors could make certain colors too similar. I would probably spaces them a bit more.