- Home /
Advice on making a Sudoku like win condition for a game
Hello everyone.
I was wondering the best way to set up a UI trigger to enable if, say, all 9 numbers exist on a 3x3 grid in any given order, but no number repeats itself(like one 3x3 in sudoku) The puzzle part I can take care of, its the idea of how to trigger a condition if 1-9 sows up in any given order on a 3x3 space/grid/3x3 objects, etc.
Any advice, links or tutorials would help me greatly and be appreciated.
Answer by Llama_w_2Ls · Dec 06, 2020 at 03:10 PM
You can have an object-like class that handles the win checking per square. It may have 9 slots (3x3 square) and can have a value from 1-9, or null if not filled yet. Every move, you might check the array of slots and check if it isn't missing any number from 1-9. If it is, then it is either not filled yet, or multiple entries of the same number have been entered. I don't want to write code for this, but I can help you if you need it. Just some advice from me. @etnom22000
Unfortunately thats not how sudoku works. You also have vertical and horizontal line checks its not just that you need to check if a square has distinct values.
Thanks so much for the info! You made this pretty clear. I'm not an expert on arrays so looking into this will help me get better at it!. Thanks so much!
Answer by sacredgeometry · Dec 06, 2020 at 04:05 PM
Personally I would think of your data-structure as something separate from your ui. The optimal way of storing and working on your data is rarely 100% synonymous with the interface you expose to the user.
Maybe using something like a multi dimensional array for the entire grid and then just bounding the search within a certain range of those arrays.
For example (and excuse the contrived example):
int[,] board = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Would allow you to index a value in the array like board[0, 0]
What this means is that you can iterate over the rows or columns one at a time by incrementing either the "x" or "y" indexes checking that each of the values are distinct and that you have all of them.
That will give you the win condition regardless of the third condition i.e. that each square will have unique values which is an implied emergent property of the other.
This is great! I will research more about this to use it. This version that uses the rule "Only 1-9 and the box cannot contain doubles" is to simplify the puzle. The game I'm working on focuses on the button presses to change multiple tiles at once in sequence. So when presses, it counts from 1-9 and also does the neighboring squares/numbers as well. so corner will change 3 object in sequence middle does top, bottom, left and right and so on. The sudoku part is just the containing of the numbers 1-9, in any order.
If you are making a sudoku board (i.e a 3x3 grid of 3x3 sections) then the answer you accepted wont work. Because you have to do cross section checking. You cant just check if a section has distinct values because each row and column also needs to have distinct values.
Edit: "The sudoku part is just the containing of the numbers 1-9, in any order."
Ok well as long as you are not needing to do sudoku checking it should be fine.
Your answer
Follow this Question
Related Questions
Activate/Deactivate Scripts form another Script 2 Answers
Counting prefabs in screen?,Problem with counting prefabs in screen 1 Answer
How to move the object to where the object is already pointing to? 1 Answer
how to allow the key to only open 1 door rather than all of them? 0 Answers
Changing Character Controller Height, But Main Camera Doesn't Follow Properly? 1 Answer