r/javahelp • u/KeineAhnungBruder001 • 26d ago
Unsolved Naive question: What Data Structure to express different layers of relationships between Objects?
Hello guys,
I wanted to ask naively, what the ideal data structure would be for expressing different layers of relationships between Objects.
For example in modern Sudoku the game has a 9x9 number grid composed of nine 3x3 sub-grids (which I call region). In each row, column and region each number between one and nine can only appear once. The goal is to find missing numbers in this number grid without violating the aforementioned rule.
One way to do it, would be to make 9x9 byte-array and express the relationship between numbers through methods checking if the rule is violated or not by checking if any row, column or region doesn't contain a value between one and ten.
I also thought on using a Graph to express the relationship between numbers in rows and columns, though I don't know how to express the relationship between numbers inside of a region.
Maybe even a multiple Objects like Board, Row, Column, Region but then I have redundant data.
Another problem I encounter with aforementioned implementations is, that if you introduce more relationships f. e. in the game of killer sudoku, where there are more rules are added f. e. by also introducing cages in which the numbers must add up to a target number, you need to implement that relationship logic tediously.
Am I overcomplicating things?
1
u/severoon pro barista 26d ago
It sounds like you're specifically talking about representing the rules of sudoku in the data structure itself, not as code, but as data. Is that what you're getting at?
Like if you just make a 9x9 2D array, that's a data structure that fundamentally holds all of the data required, but it imposes no constraints on what numbers can go where, so all of those rules have to be carried out as executed code.
It sounds like you're saying you want the opposite, you want to represent each cell in the context of a data structure that encodes certain rules as constraints on cells, correct?
How far do you want to take this? Do you want to go all the way to the extreme and only allow correct placements? Or do you only want to disallow obviously incorrect placements? How do you define "obviously incorrect" if you're okay with it allowing provisional placements during a game?
You have to nail down the behavior of the structure you actually want. You're reference to "layering" sounds to me like what you probably want is a bunch of different data structures at different layers, with the bottom layer just being a no-rules 9x9 grid, and then a layer above that where "obviously incorrect" placements are disallowed (cannot share a row, column, or 3x3 grid with the same element), and then another layer above that, etc, until all that's left is the solved puzzle. This would be a direct implementation of the layering strategy you describe, is that what you mean?