-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku.cs
130 lines (111 loc) · 2.83 KB
/
Sudoku.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
namespace Sudoku_Solver
{
class Sudoku
{
public const int SIZE = 81;
private readonly object pLocker = new object();
private readonly object vLocker = new object();
private Field[] game = new Field[SIZE];
public Sudoku(int[] init)
{
for (int i = 0; i < SIZE; i++)
{
game[i] = new Field();
if (init[i] == 0)
{
for (int j = 1; j < 10; j++)
{
game[i].AddPossibility(j);
}
}
else
{
game[i].Value = init[i];
}
}
}
public int Get(int index)
{
return game[index].Value;
}
public Field GetField(int index)
{
return game[index];
}
public void Set(int index, int value)
{
lock (vLocker)
{
game[index].Value = value;
}
}
public List<int> GetPossibilities(int index)
{
return game[index].GetPossibilities();
}
public void RemovePossibility(int index, int value)
{
lock (pLocker)
{
game[index].RemovePossibility(value);
}
}
public void RemoveAllPossibilities(int index)
{
lock (pLocker)
{
game[index].RemoveAllPossibilities();
}
}
public int IndexOf(Field other)
{
for (int i = 0; i < SIZE; i++)
{
if(game[i] == other)
{
return i;
}
}
return -1;
}
public void PrintSudoku()
{
string line = "";
string separator = "";
for (int i = 0; i < 38; i++)
{
separator += "-";
}
Console.WriteLine(separator);
for (int i = 0; i < Sudoku.SIZE; i++)
{
if (i % 3 == 0)
{
line += " | ";
}
line += " ";
if (Get(i) == 0)
{
line += "_";
}
else
{
line += Get(i);
}
line += " ";
if (i % 9 == 8)
{
line += " | ";
Console.WriteLine(line);
line = "";
}
if (i % 27 == 26)
{
Console.WriteLine(separator);
}
}
}
}
}