Recent

Author Topic: Best data structure to use?  (Read 13070 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1125
  • Professional amateur ;-P
Re: Best data structure to use?
« Reply #30 on: March 21, 2021, 07:59:10 pm »
Hey avk,

This benchmark is contributed by forum user @BeniBela.

Yes Benibela deservers most of the credit.
But you did find it, remembered it and then caressed the keyboard, oh so gently, to let me know about it, so you also deserve some credit, non? Né-çe pas? wink-wink, nudge-nudge... :D

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Best data structure to use?
« Reply #31 on: March 21, 2021, 08:08:16 pm »
Actually, my crafts are also involved in the benchmark, so it's not surprising that I remember it.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1125
  • Professional amateur ;-P
Re: Best data structure to use?
« Reply #32 on: March 21, 2021, 08:23:34 pm »
Hey avk,

Actually, my crafts are also involved in the benchmark, so it's not surprising that I remember it.

So it was a joint venture then?!

Well, congratulations for both :)

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Best data structure to use?
« Reply #33 on: March 22, 2021, 02:23:42 am »
  If you use
{$ModeSwitch AdvancedRecords}

Then you can put methods inside of records like classes and also Class operators to make the looks of the code more stream lined.

I did read up on this a bit. 

Also speed isn't really too important but I could see it becoming an issue if the "database" gets large.  I have it set up though so you can use different packs of words as separate disk files.

Thanks for your input.

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Best data structure to use?
« Reply #34 on: March 22, 2021, 02:26:16 am »
Hi Rich,

Just to be clear here: Different categories can have the same word, but the played field has to be relative to the category.
OR
I can have the word Mouse under Category Animals and Mouse under Category House Pests, but if I played Animals/Mouse, then House Pest/Mouse is not played, right?

No - it's okay for mouse to be played again in a different category.  If someone guesses correctly that is what toggles the "played" field to True.  If they pass then it's still considered unplayed.


rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Best data structure to use?
« Reply #35 on: March 22, 2021, 02:31:37 am »
Although "real time" generally means responses within microsconds... for something like this that might possibly be overkill.

Yes, for it's current use even a couple of seconds would be fine - building this for our youth group to have some fun on youth night.

Quote
That's cruel, OP has already explained that he's a bit shaky on pointers etc.

Yeah no idea on this one but not thinking it's necessary - don't think this is going to become a huge project.

Quote
Rich: a pointer is the address of something, but from the POV of programming it also carries type information around with it. @ takes the address of something (most commonly a record), ^ dereferences a pointer and returns whatever was pointed to.

Yeah I'll read up a bit and see if I can get my head completely around it - seems straight-forward though.

Quote
A useful compromise is the TStringList type. This is an object, i.e. you'd do something like

Code: Pascal  [Select][+][-]
  1. myStringList := TStringList.Create;
  2. try
  3.   ...
  4. finally
  5.   FreeAndNil(myStringList)
  6. end;
  7.  

It looks a bit like an array of strings, but it can be sorted and searched fairly efficiently (see the documentation starting at https://www.freepascal.org/docs-html/current/rtl/classes/index.html ) and each string can also have an object associated with it.

Yeah I've used the Tstringlist already for loading data in from the INI file and transferring it to the list box.


rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Best data structure to use?
« Reply #36 on: March 22, 2021, 02:39:55 am »
About the disk storage.
You may want to consider using a separate file for storing the info which words were already played.

That way you do not need to modify the original data file (or update packs).
If you do a new release, and the data file comes with more entries (added to the same main data file), you can replace the old data file, and still have the "played" info in the separate file.

This makes sense, however it's pretty trivial to toggle all the played flags back to 0 - for a new install it would just reset everything to unplayed.

Quote
It also would be in line with the recommendations of most OS. At least OS like Win,Linux,Mac that are multi-user.
If you distribute the app, it is possible that it is played by more than one person (OS User account)  on one PC. So the "played" file could be saved in the users home directory.

My plan is for this to be played on a large screen TV on a single computer with the gameboard on a second monitor with the controls on the main monitor (initial screenshot) so not planning for multi-user although one could modify it for such.

Quote
Next question is, if you want the files to be human readable. But probably that is a good idea.

Yeah that was the thought behind the INI to make it easy to add words manually or to troubleshoot issues - also thought about making a grid based editor for the words as well to make it easy to add a lot of words quickly.  Also thought about CSV import too.


Quote
For the "already played" I would suggest to just dump the words, one per line into a text file (TStringList can do that).
Note that if you keep the order in which they were played, then you can limit how many words you save.
You could limit to store the last 1000 played words. After that they can be played again. (If you want).

My plan was to keep playing all the words until all of them have been played.. so the route would count the number of words and then reset the played counter if there were 0 words with a 0 status of played. So if count > 0 but unplayed is 0 then reset all words to unplayed

Quote
When you load the data, you may have to re-arrange it.

When a round starts I was thinking about randomizing the list of words (only unplayed would be loaded for the round) and then it would just keep track of which ones were guessed correctly.  Passed words are considered as unplayed.


rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Best data structure to use?
« Reply #37 on: March 22, 2021, 02:41:36 am »
Hey Rich,

Did a bit of a rewrite and now:

Hi Gus,

Sorry haven't had a spare minute to look at what you've done here - I coordinate all the activites for our church service on Sunday so Saturday afternoon till after church on Sunday is really busy for me.  I should be able to get back to this tomorrow.  I hope to load it up and take a look!

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Best data structure to use?
« Reply #38 on: March 22, 2021, 02:45:02 am »
Take a look at in-memory datasets (like TBufDataset, ZMSQL).
ZMSQL supports SQL queries and can load and save data to text files.

Someone mentioned also the TCSVdataset that works with CSV data files I was going to look at too.  I'll read up on those two as well.

Quote
One more thing.
I would save date and time when the word was played. When you'll start to run out of words you first mark the oldest played words so that they can be played again.

I was just going to toggle them all back to 0 once we have played them all and let it start over.  Might have to happen during a round though which depending on the amount of words might take a while... but I think pascal works on records pretty quickly.

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Best data structure to use?
« Reply #39 on: March 22, 2021, 02:50:17 am »
Thanks to everyone who is contributing!  I think this will help my learning quite a bit - reading up on all these different options.  In the end I just want to make a fun game for our youth group to play that we can use over and over.  Some of the things I still need to do are come up with the sound effects for the countdown timer when it reaches say 5 seconds or so and maybe some sort of background music track at the beginning while it's displaying the choice of categories. 

I plan to make it so the operator can select maybe up to 6 or so categories from the list that then would be displayed on the screen for the youth to choose from and once they choose a category then there is a series of rounds (configurable) played and the computer will keep score between how many were correct and passed.

At the end of each round it would display which words were correct and which words were "passed" (wrong) and show the scores.

I should make this a project on github or something and make it open source so everyone can help contribute.. but that may be a bit above my paygrade yet. :)

Rich
« Last Edit: March 22, 2021, 02:51:52 am by rwebb616 »

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1125
  • Professional amateur ;-P
Re: Best data structure to use?
« Reply #40 on: March 22, 2021, 08:54:48 pm »
Hi Rich,

Sorry haven't had a spare minute to look at what you've done here - I coordinate all the activites for our church service on Sunday so Saturday afternoon till after church on Sunday is really busy for me.  I should be able to get back to this tomorrow.  I hope to load it up and take a look!

Hey don't be sorry, I understand busy.

What I gave you will take care of 100% of your data needs but it's about 70% of your game needs.
I only implemented the Load/Save of the data and the Add/Delete of Categories and Words, all from the top object I called TCWSContainer.
I didn't do any of the game logic, cuz I was only aiming at giving you an example of how to do the data manipulation with TObjects that then get marshalled to a JSON file.

The Container object has the LoadFromFile() and SaveToFile() that will take care of marshalling and un-marshalling of the data.

The Container also has AddCategory() and DeleteCategory() which only adds unique categories.

The Container also has AddWord() and DeleteWord() which only adds unique words under each category.

It also has GetCategories() which will get you a TStringList of all the categories and GetWords(CategoryID) that will do the same for words.

I think it was MarkMLI that suggested that each word should have a TDateTime for last played. I need to implement this in a manner that the getter/setter takes a TDateTime, but drops an Integer(UnixTime) on the object, so that when it's marshalled it's dropped as an Integer. I guess I could try it with the double that TDateTime is based on, but I dunno, I'll have a gander on that.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1125
  • Professional amateur ;-P
Re: Best data structure to use?
« Reply #41 on: March 22, 2021, 09:01:06 pm »
Hi Rich,

Thanks to everyone who is contributing!  I think this will help my learning quite a bit - reading up on all these different options.

You're more than welcome. That's what this forum is all about, giving back to the community.

I should make this a project on github or something and make it open source so everyone can help contribute.. but that may be a bit above my paygrade yet. :)

The GitHub idea is fantastic. But when you say "above my paygrade", is that from fear or just because you lack the knowledge?

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Best data structure to use?
« Reply #42 on: March 22, 2021, 09:03:09 pm »

The GitHub idea is fantastic. But when you say "above my paygrade", is that from fear or just because you lack the knowledge?

Cheers,
Gus

Probably a little of both.  I have used "git" to clone a repository but I haven't used github to set up one.  I also don't have my head completely around how the repos work - pull requests and commits etc.  It's probably mostly lack of knowledge. 

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1125
  • Professional amateur ;-P
Re: Best data structure to use?
« Reply #43 on: March 22, 2021, 09:35:27 pm »
Hi Rich,

Well, I was as afraid and ignorant as you about 6 months ago.

You know what I did? I just started a repo, or a ton (I kinda got carried away and did a backup of all my little projects), and messed with all the possibilities that the UI would allow me.

Was I scared? You bet I was, but I got a bit more confident and I persevered, and a bit more confident after that and so on.

But you don't have to go at it alone, you can ask me what ever you want.
My sig has a link to my GitHub account and IINM there's a link to my email on my profile, so ask away.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Best data structure to use?
« Reply #44 on: March 22, 2021, 11:51:11 pm »
Quote
My sig has a link to my GitHub account and IINM there's a link to my email on my profile, so ask away.

How extremely kind of you!  Thank you - I just might do that.  Right now I just want to get a v1.0 going so we can test it out with our youth group.  I think they'll have a blast with it.

 

TinyPortal © 2005-2018