Hello, I’m creating a crossword game where it’s planned to potentially add 1000 or more levels. Each level consists of stages, each stage is a crossword.
I used to saving levels data in SO, but I don’t think that would be a good solution here since I’ll have this many levels. I was thinking of separating each level in its own folder, where there are several files in json for stages. Then I would need to have an index in folder name by which I’ll be able to load it. But, I might also need to have a difficulty level and other criteria, and I don’t think I can do it with this approach.
Is my only option here to store them in a database?
You supply the words together with the „hint“. You end up with a long list. Then you use an appropriate algorithm to randomly generate the crosswords puzzle from the words and their hints. There is no „design“ in crosswords puzzles ever since we have computers.
I already have the algorithm, it generates json crosswords, but it can’t do that at runtime obviously. I’m asking how to store a lot of crossword levels produced by algorithm to then load them at runtime.
As to storage, you could simply save an entire stage worth of crosswords as json. That shouldn’t be more than a few KB on disk and in memory , and you can store all extra data as well (difficulty etc). Heck, even the entire game’s worth of crosswords should be able to keep in memory.
You can also split it up any way you want, eg have a stage.json that references the puzzle json by filename.
There is no fast crossword generation algorithm from what I gathered, the algorithm I implemented following stack overflow solution involves scoring each word placement as well as generating several crosswords and scoring them by criteria(intersections, board length), it’s O(n!) I think and takes 1-2 seconds at least for each crossword, this is why I can’t generate them at runtime.
I’m a bit concerned about storing all levels in memory, but saving the entire stage is a good idea, thanks! I think, for now, I’ll store related metadata like difficulty in file names, this way I won’t need to load all levels into memory.
How large are your crossword puzzles? You can store crossword puzzles quite compact when you just store a word index into a general word list along with a start position and direction bit (across or vertical). If the word list is really large, storing it compressed probably makes sense.
I highly doubt that your algoritm is O(n!) and if it truly is, it’s probably a horribly inefficient implementation. N-factorial is generally unlikely unless every word actually crosses every other word which is pretty impossible as horizontal words can only cross vertical ones. Word lookup can be optimised by several dictionaries which can be initialized once at start. Though it’s up to you how you want to create your game.
Your concerns seems to be a lot premature optimisation. Just storing them as json so you could load them individually either as scriptable objects or plain C# classes would be pretty easy. Splitting “levels” into packs of, say 100 and storing them together in a single json file, probably compressed, would be tiny. Having a handfull of high quality images probably requires more memory than your level data.
They aren’t large, 10x10 max. Yes, I’m already storing them that way.(without compression)
You might be right, I don’t know algorithm complexity that well, so I could’ve misjudged it, but the point is I can’t do it at runtime at all…
I’m just not sure about loading them into memory all at once, I would need to read all 1000 levels, which are separate files(I heard that loading a lot of small files might be slow) and cache it. For me, it seems like I can get away with just saving necessary metadata in filename and load the appropriate level when needed.
Maybe I am wrong, and it’s not an issue at all, I’m not very experienced with this, for me it seemed like potentially a problem and since it would be harder to change it later, I want to make sure that I’m doing the right thing
You are seriously getting wedged up in concerns you should not have.
Steps to success:
Get it working.
Get it working well.
Do NOT touch Step #2 until Step #1 is done. Seriously.
99.9% of the time on modern hardware, ESPECIALLY with little text files like this, you’ll find that Step #1 suffices for Step #2, which you don’t even end up having to do!
Put the JSON files in a directory under Resources/ and just use Resources.LoadAll<TextAsset>() to get them.
Then MOVE ON … storing a bunch of utterly-unrelated tiny text files should be the most trivial part of your game.
Getting a good crossword UI is going to be FAR harder. Focus your efforts there. That will make your game sink or swim.
Here’s a crossword generator from 6 to 7 years ago for Unity 2017. On my 5950X running in Unity 2023 it can make a new puzzle in 30ms. It lacks a license file so it’s under the default github “you can’t use the source code” license but it should still give you an idea of what you can achieve.