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.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Reaction timer game + hall of fame

Hi,

 

I'm currently working on a project for school, in which I have to do a reaction timer game, including an hall of fame of the top ten best scores.

So, the user enters his name and if his score is in the top ten, his score and name should be displayed on two different array (at the same index).

 

The reaction timer game part is working fine, but I'm stuck with the hall of fame part. Specially, with how to shift down all the values already in the time array when a new one should be in the top ten, and how to match it with the array containing the names.

 

As you could see from the files attached, I tried to detect if the value should be displayed or not using a For Loop doing : For 10n, it detects if the score should be displayed or not, using "yes, if new value> array(n) and new value<array(n+1)".

Then, I was using a Replace Array block, but this isn't what I want as it just replace the (previous) value that was detected from the condition above. (I realised that after some time, as it was the first time I was using this block.)

So in the "test" VI, I tried to shift down the values after the one that should be displayed but it doesn't work. And even if it was working, I wouldn't know how to match it with the name array anyway. So yeah, I have no clue right now.

 

/!\ It runs on LabView 2015 /!\

 

I attached two VIs, "project" contains every parts of the program and the "test" VI, is just a piece of "project" where I tried several options in order to figure out how to make the shift down work.

 

I hope you could help me !

Download All
0 Kudos
Message 1 of 6
(2,669 Views)

If you can get the index you want, you can use Insert into Array (you'll end up with an 11 element array) and then Array Subset (or Delete from Array) to get back to 10 elements.

 

You could consider using a cluster so then you'd only need a single array of (Name, Score), but that might make searching a little more complicated using your current scheme.

Instead, since you'd want to search/sort by score, but probably display Name on the left, you could arrange manually (disable autosizing) with Score as the first element in the cluster.

Then, unconditionally use Build Array to add the new entry, sort (sorting an array of clusters sorts first by the first element, e.g. score if you have (Score, Name) and just drag the elements 'back-to-front'), then delete the last element (which can be the new element, if it is the lowest score).

 

You can also try using Threshold 1D Array to work out the correct index, but I often find it a bit fiddly. It will do what you want, but you'll probably have to look at the help files to check the exact behaviour in order to round (up/down) correctly.

 

Edit: Having taken a quick look at the project.vi (thanks for attaching, by the way, makes it much easier to check things than screenshots or even worse photographs of a screen) I'd also suggest perhaps taking a look at Event Structures - I guess maybe this is a few lessons in the future for you but it might simplify some of your handling and reduce the CPU usage in your "greedy loops" - these are loops with no wait or speed limits, which then iterate very very quickly burning up 100% CPU on a single core. If you're willing to put even a 1ms wait in the loop, it would drastically reduce CPU usage (and probably have no real effect on the results, since measurement precision is 1ms anyway).


GCentral
0 Kudos
Message 2 of 6
(2,643 Views)

cbutcher wrote;

 

"

You could consider using a cluster so then you'd only need a single array of (Name, Score), but that might make searching a little more complicated using your current scheme.

"

That would be my first thought.

 

Pass an array of clusters where the first element is the reaction time and the second is the name and unbundle out the name and compare it with the current user name. If the name matches use the max min from compare palette and use min to replace the reaction time.

 

That updated array then get passed to a sort 1-d array (will sort on first element in cluster ...) and then grab the first ten elements to update the display.

 

Done!

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 3 of 6
(2,625 Views)

Oh gosh... I didn't realize, using the Insert to Array would do it so easily, thanks !

I'm not sure if I'll be using clusters, I'm not really used to those right now but I'll definitly take a look at it.

0 Kudos
Message 4 of 6
(2,620 Views)

OK, I'm thinking of the "pinball"-type games in Arcades where they have a Hall of Fame, a list of Names and Scores, with the possibility that the same name occurs multiple times in the Hall of Fame (since you want the top scores, right?).

 

Ben mentioned a Cluster, with Name and Score, and building an Array of Clusters.  Someone (Ben, again?) also suggested sorting the Array (a pretty fast operation until you get many thousands of entries) and displaying the first 10 (why not keep all the scores?  Do you expect you'll have a MegaScore?  So it takes, maybe, 40 MegaBytes?  Ooooh!).

 

One thing to know about sorting Clusters -- the sort starts with the first element, so you'll want to make the Cluster "Score, Name", but probably display it as Name, Score.

 

Bob Schor

0 Kudos
Message 5 of 6
(2,561 Views)

Right you are Bob!

 

Cluster order only affects cluster appearance if we set the cluster to arrange vertically or horizontally. And since the topic does not come up that often...

 

A cluster sort will be sorted first by the first element in the cluster but in the event of ties, the next item in the cluster is used for tie-breakers and then the third etc.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 6 of 6
(2,557 Views)