Advertisement
Guest User

Controller

a guest
Apr 26th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SudokuLibrary
  8. {
  9.     public class Controller
  10.     {
  11.         private const int Length = 9;
  12.         private int[,] arr;
  13.        
  14.         public int[,] Array
  15.         {
  16.             get
  17.             {
  18.                 if (arr == null)
  19.                     arr = new int[Length, Length];
  20.                 return arr;
  21.             }
  22.         }
  23.  
  24.         public bool CheckCell(int i, int j, int num)
  25.         {
  26.             // if this numer was found returns false and whole function returns false
  27.             if (CheckSquare(i, j, num) == false )
  28.                 return false;
  29.  
  30.             // if this numer was found returns false and whole function returns false
  31.             if (CheckRow(i, num) == false)
  32.                 return false;
  33.  
  34.             // if this numer was found returns false and whole function returns false
  35.             if (CheckColumn(j, num) == false)
  36.                 return false;
  37.  
  38.             // otherwise returns true
  39.             return true;
  40.         }
  41.  
  42.         /// <summary>
  43.         /// returns true if no num in square around was found. Otherwise returns false
  44.         /// </summary>
  45.         /// <param name="i">x coordinate of input </param>
  46.         /// <param name="j">y coordinate of input</param>
  47.         /// <param name="num">the numer of input </param>
  48.         /// <returns></returns>
  49.         private bool CheckSquare(int i, int j, int num)
  50.         {
  51.             for (int x = i/3; x < i / 3 + 1; x++)
  52.             {
  53.                 for (int y = j/3; y < 3 + 1; y++)
  54.                 {
  55.                     if (arr[x, y] == num)
  56.                         return false;
  57.                 }
  58.             }
  59.  
  60.             return true;
  61.         }
  62.  
  63.         /// <summary>
  64.         /// returns true if no num was found in row. Otherwise false
  65.         /// </summary>
  66.         /// <param name="i"></param>
  67.         /// <param name="num"></param>
  68.         /// <returns></returns>
  69.         private bool CheckRow(int i, int num)
  70.         {
  71.             for (int y = 0; y < Length; y++)
  72.             {
  73.                 if (arr[i,y] == num)
  74.                     return false;
  75.             }
  76.  
  77.             return true;
  78.         }
  79.  
  80.         /// <summary>
  81.         /// returns true if no num was found in column. Otherwise false
  82.         /// </summary>
  83.         /// <param name="j"></param>
  84.         /// <param name="num"></param>
  85.         /// <returns></returns>
  86.         private bool CheckColumn(int j, int num)
  87.         {
  88.             for (int x = 0; x < Length; x++)
  89.             {
  90.                 if (arr[x,j] == num)
  91.                     return false;
  92.             }
  93.  
  94.             return true;
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement