Skip to content

Instantly share code, notes, and snippets.

@k-harris
Last active March 5, 2019 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k-harris/2283a870ea0beaf6690a122fda288db4 to your computer and use it in GitHub Desktop.
Save k-harris/2283a870ea0beaf6690a122fda288db4 to your computer and use it in GitHub Desktop.
Making Sudoku in Unity3D. It's more of a quick project so I've started with the backend before going on to the UI. This code hasn't been fully tested but I'll be updating it as time goes on.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class SudokuGameManager : MonoBehaviour
{
public Transform display;
public GameObject numberPrefab;
public List<List<BoardTile>> board= new List<List<BoardTile>>();
private List<List<BoardTile>> m_flippedBoard = new List<List<BoardTile>>(); //for checking
private List<List<BoardTile>> m_squaresToArrays = new List<List<BoardTile>>(); //for checking
public int boardSize = 9;
public float prefabSize = 100;
// Start is called before the first frame update
void Start()
{
StartGame();
}
public void StartGame()
{
CreateBoard();
}
void CreateBoard()
{
for(int x = 0; x < boardSize; x++)
{
for(int y = 0; y < boardSize; y++)
{
//data storage
board[x][y].value = 0;
//physical board
GameObject num = Instantiate(numberPrefab, display);
num.transform.position = new Vector2(x, y) * prefabSize;
board[x][y].displayObj = num;
}
}
display.transform.localPosition = new Vector2(450, 58);
}
void FlipBoard()
{
//flip the board for easy checking
m_flippedBoard = board;
for(int i =0; i < board.Count; i++)
{
for(int j = 0; j < board[i].Count; j++)
{
m_flippedBoard[j][i] = board[i][j];
}
}
}
void TurnSquaresToArrays()
{
//TODO: turn 3x3 squares into arrays for helper functions
}
void CheckAll()
{
FlipBoard();
TurnSquaresToArrays();
// check the sum on each row
CheckSumsOfRows();
// check the sum on each column
CheckSumsOfColumns();
// check for sum on each box
// TODO: box to list
// check for duplicate numbers on each row
CheckRowsForDuplicates();
// check for duplicate numbers on each column
CheckColumnsForDuplicates();
// check for duplicate numbers on each box
// TODO: box to list
}
bool CheckSumsOfRows()
{
for(int i =0; i < board.Count; i++)
{
if(CheckSum(board[i]) == false)
{
return false;
}
}
return true;
}
bool CheckSumsOfColumns()
{
for(int i =0; i < m_flippedBoard.Count; i++)
{
if(CheckSum(m_flippedBoard[i]) == false)
{
return false;
}
}
return true;
}
bool CheckRowsForDuplicates()
{
for(int i =0; i < board.Count; i++)
{
if(CheckForDuplicates(board[i]) == false)
{
return false;
}
}
return true;
}
bool CheckColumnsForDuplicates()
{
for(int i =0; i < m_flippedBoard.Count; i++)
{
if(CheckForDuplicates(m_flippedBoard[i]) == false)
{
return false;
}
}
return true;
}
//checks if the sum of array is 45
bool CheckSum(List<BoardTile> _list)
{
int sum = 0;
foreach(BoardTile i in _list) sum += i.value;
return sum != 45 ? false : true;
}
//checks if there is a duplicate value in the array
bool CheckForDuplicates(List<BoardTile> _list)
{
List<int> values = new List<int>();
foreach(BoardTile b in _list)
values.Add(b.value);
List<int> distinct = values.Distinct().ToList();
return (distinct.Count != _list.Count) ? false : true;
}
}
[System.Serializable]
public class BoardTile
{
public int value;
public GameObject displayObj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment