Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
I am trying to make a Sudoku game in Unity3D but I'm currently stuck on the logic behind the game. (checking if all the numbers in the same subgrid, row and column are different)

I have been able to get a script that generates the whole grid running, both for a 2x2, 2x3 and a 3x3 level. The part that I am stuck on is making the 2D Array to hold the selected values for all subgrids. I can also provide the full GenerateGrid script if it's needed, but any help apart from that would be of help.

Here is also a small sample of the GenerateGrid script - the method that defines the whole grid's size depending on the difficulty:

C#
void GetDifficulty() //Method to get the difficulty of the current game
    { 
        switch (difficulty) //Switch statement to check all of the possible game difficulties
        {
            case Difficulty.Easy: //If the current difficulty is easy
                gridSize = 4; //Sets the grid size to 4
                FindObjectOfType<AudioManager>().Play("BGM_Level_Easy"); //Finds the audio manager in the scene and plays the easy theme music
                cam.transform.position = new Vector3(-2.5f, (float)gridSize * 10, ((float)gridSize / 2 - 0.5f) * 10); //Sets the camera position based on the grid size
                break; //Exits the switch statement
            case Difficulty.Medium: //If the current difficulty is medium
                gridSize = 6; //Sets the grid size to 6
                FindObjectOfType<AudioManager>().Play("BGM_Level_Medium"); //Finds the audio manager in the scene and plays the medium theme music
                cam.transform.position = new Vector3(-1f, (float)gridSize * 10, ((float)gridSize / 2 - 0.5f) * 10); //Sets the camera position based on the grid size
                break; //Exits the switch statement
            case Difficulty.Hard: //If the current difficulty is hard
                gridSize = 9; //Sets the grid size to 9
                FindObjectOfType<AudioManager>().Play("BGM_Level_Hard"); //Finds the audio manager in the scene and plays the hard theme music
                cam.transform.position = new Vector3(2f, (float)gridSize * 10, ((float)gridSize / 2 - 0.5f) * 10); //Sets the camera position based on the grid size
                break; //Exits the switch statement
            default: //Default to medium difficulty if no difficulty is set
                gridSize = 6;
                FindObjectOfType<AudioManager>().Play("BGM_Level_Medium");
                cam.transform.position = new Vector3(-1f, (float)gridSize * 10, ((float)gridSize / 2 - 0.5f) * 10); //Sets the camera position based on the grid size
                break; //Exits the switch statement
        }      
        Debug.Log(difficulty); //Logs the difficulty to ensure that the correct values have been set


(Note: I have tried my best to research as much as I can about this, but none of the stuff I found online was about a Unity3D version of the game, only 2D ones)

Thanks!

What I have tried:

I have tried making the code manually compare each tile in the subgrid to each other tile, but it didn't work - only printed that it did. (apart from being really unoptimized)
Posted
Updated 6-Dec-21 19:21pm
v2
Comments
BillWoodruff 5-Dec-21 21:14pm    
What makes your game 3d ? What does the player see that a 2d flat grid doesn't show ?
Denis Hristov 5-Dec-21 22:00pm    
Hello! That is a wonderful question. The reason why my game is set in a 3D environment and not a 2D one is because I'm making the numbers into various 3D images (of cartoon fish to be specific - it is going to be a kids game) and I need it to be 3D to be able to edit their look and positioning on the grid.
BillWoodruff 5-Dec-21 23:58pm    
Glad you found my response relevant.

"making the 2D Array to hold the selected values for all subgrids."

please clarify what "selected" means here: this refers to cells the user has selected, or cells containing user entered values ... or ?

"I have been able to get a script that generates the whole grid"

what structure is returned by the script ? an array ?
Denis Hristov 6-Dec-21 0:13am    
First, the GenerateGrid script is supposed to generate random values and not previously inputted ones. So, if one subgrid already generated specific values (lets say 1, 7 and 9), I'll need to let the code know whenever I put another value in and to REMEMBER it - hence the 2D Array. So if I put a 3 inside, that 2D Array would check if the specific subgrid, column and row have the specific number repeating and if one or more of them do - it will inform the user and highlight the error. If they don't, the number will simply be put in.

Also, the script only generates the grids for each level type (Easy - 4x4, Medium - 6x6 and Hard - 9x9), then stores the subrid sizes (Easy - 2x2, Medium - 2x3 and Hard - 3x3). I can provide the whole script if you want to take a look at it for yourself. That wouldn't be a problem for me.

Lastly, I do apologize for not being as clear as I should on these things. I am new to coding as you can see and with that come many things that I am still yet to learn about.

Thank you once again for replying and have a wonderful rest of your day!

BillWoodruff 6-Dec-21 0:40am    
I'm having difficulty visualizing what the different size grids are. Let's take the typical 3x3 sub-grid case where each sub-grid is 3x3 cells ... 9 rows, 9 columns, 81 cells.

I assume your script will "seed" the grid with valid initial values ... right ?

Then, the user will input a value in a cell, and you wish to check if that value is valid for the row and column where the value is entered ... right ?

One obvious strategy is to define the data structure for the grid, then pass it by reference to the code that seeds it with initial values ... does that seem relevant ?

"3d ness" ? Are only the contents of the cells 3d elements ? I am not familiar with Unity 3d, so don't know what data structures are available.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900