-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cs
460 lines (416 loc) · 14.3 KB
/
Board.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
using System;
using System.Linq;
namespace Skyscrapers
{
/// <summary>
/// The board class.
/// </summary>
class Board
{
// Constants variables
public const int HintsAmount = 3;
public const int BoardSize = 4;
// Example Board
private int[,] ExampleBoard;
// Class attributes
private int[,] SolvedBoard { get; }
private int[,] Edges { get; }
private int[,] ResetBoard { get; }
private int[,] SolvingBoard { get; set; }
private int CountHints { get; set; }
public Board(int size)
{
ExampleBoard = new int[,] { { 1, 4, 3, 2 },
{ 2, 1, 4, 3 },
{ 4, 3, 2, 1 },
{ 3, 2, 1, 4 } };
SolvedBoard = GenerateBoard(size);
Edges = CreateEdges(size);
ResetBoard = new int[size,size];
SolvingBoard = new int[size, size];
CountHints = HintsAmount;
}
static int[,] GenerateBoard(int size)
{
Random rnd = new Random();
int[,] board = new int[size, size];
int[] digits = new int[size];
for (int i = 1; i <= size; i++)
{
digits[i - 1] = i;
}
int[] shuffeld_row = new int[size];
for (int i = 0; i < size; i++)
{
while (!CheckRow(board, shuffeld_row))
{ // while row is in board, shuffle again!
shuffeld_row = digits.OrderBy(item => rnd.Next()).ToArray();
}
for (int j = 0; j < size; j++)
{
board[i, j] = shuffeld_row[j];
}
}
return board;
}
static bool CheckRow(int[,] built_board, int[] seq)
{
for (int j = 0; j < built_board.GetLength(0); j++)
{
for (int i = 0; i < seq.Length; i++)
{
if (built_board[j,i] == seq[i])
{
return false;
}
}
}
return true;
}
private int[,] CreateEdges(int size)
{ //Create edges using the board inside.
int[,] edges = new int[4, size];
for (int side = 0; side < 4; side++)
{
for (int i = 0; i < size; i++)
{
edges[side, i] = CountBuildings(side,i,SolvedBoard);
}
}
return edges;
}
private int CountBuildings(int side, int i, int[,] board)
{ // Check how many buildings are in each row/colmn
int height = 0;
int cur_highest = 0;
switch (side)
{
case 0:
for (int j = 0; j < board.GetLength(0); j++)
{
if (board[i, j] > cur_highest)
{
height++;
cur_highest = board[i, j];
}
if (cur_highest == board.GetLength(0))
{
break;
}
}
return height;
case 1:
for (int j = board.GetLength(0) - 1; j >= 0; j--)
{
if (board[i, j] > cur_highest)
{
height++;
cur_highest = board[i, j];
}
if (cur_highest == board.GetLength(0))
{
break;
}
}
return height;
case 2:
for (int j = 0; j < board.GetLength(0); j++)
{
if (board[j, i] > cur_highest)
{
height++;
cur_highest = board[j, i];
}
if (cur_highest == board.GetLength(0))
{
break;
}
}
return height;
case 3:
for (int j = board.GetLength(0) - 1; j >= 0; j--)
{
if (board[j, i] > cur_highest)
{
height++;
cur_highest = board[j, i];
}
if (cur_highest == board.GetLength(0))
{
break;
}
}
return height;
default:
return height;
}
}
//Gets methods:
public int[,] GetResetBoard()
{
return SolvingBoard;
}
public int[,] GetEdges()
{
return Edges;
}
public int[,] GetSolvingdBoard()
{
return SolvingBoard;
}
public int[,] GetSolveddBoard()
{
return SolvedBoard;
}
public int GetCountHints()
{
return CountHints;
}
public (int,int) GetHint()
{ //function that gives a hint.
// Creates a Random object
Random rnd = new Random();
// Generates the first random row and coloumn of the cell
int row = rnd.Next(0, SolvingBoard.GetLength(0));
int col = rnd.Next(0, SolvingBoard.GetLength(1));
// checks if needs to grill again the row and the coloumn and does it:
while (SolvingBoard[row, col] == SolvedBoard[row, col])
{
row = rnd.Next(0, SolvingBoard.GetLength(0));
col = rnd.Next(0, SolvingBoard.GetLength(1));
}
// takes the value of the cell from the solved board
// and copy it to the currently solving board
SolvingBoard[row, col] = SolvedBoard[row, col];
ResetBoard[row, col] = SolvingBoard[row, col];
CountHints -= 1;
return (row, col);
}
public string UpdateHintButtonText()
{
return string.Format("Hint ({0})", GetCountHints().ToString());
}
public void UpdateSolvingBoard(string cell_name, string cell_text)
{ // update in the logic what the screen shows.
int row = cell_name[4]-48;
int col = cell_name[6]-48;
if (cell_text == "")
{
SolvingBoard[row, col] = 0;
}
else
{
int val = cell_text[0] - 48;
SolvingBoard[row, col] = val;
}
}
public void BackToResetBoard()
{ //Returning to reset board
for (int row = 0; row < ResetBoard.GetLength(0); row++)
{
for (int col = 0; col < ResetBoard.GetLength(1); col++)
{
SolvingBoard[row, col] = ResetBoard[row, col];
}
}
}
private bool AllCellsFilled()
{ // Check if all the cells are filled.
for (int row = 0; row < ResetBoard.GetLength(0); row++)
{
for (int col = 0; col < ResetBoard.GetLength(1); col++)
{
if (SolvingBoard[row, col] == 0)
{
return false;
}
}
}
return true;
}
private bool EdgesSatisfied()
{ //check if the solving fits the edges.
for (int side = 0; side < 4; side++)
{
for (int i = 0; i < SolvingBoard.GetLength(0); i++)
{
if (Edges[side, i] != CountBuildings(side, i, SolvingBoard))
{
return false;
}
}
}
return true;
}
private bool DigitNoRepeat()
{ // Check if theere are no repeats in each row and colmn
for (int cur_row = 0; cur_row < ResetBoard.GetLength(0); cur_row++)
{
for (int row = 0; row < ResetBoard.GetLength(0); row++)
{
if (row == cur_row)
break;
else
{
for (int col = 0; col < ResetBoard.GetLength(1); col++)
{
if (SolvingBoard[cur_row, col] == SolvingBoard[row, col])
return false;
}
}
}
}
return true;
}
public bool CheckSolution()
{ //"IsSaffe : check if all the functions are true
if (!AllCellsFilled())
return false;
if (!EdgesSatisfied())
return false;
if (!DigitNoRepeat())
return false;
return true;
}
public bool SolveSkyScrapers(int index)
{
if (index == SolvingBoard.Length)
{
return true;
}
int row = index / SolvingBoard.GetLength(0);
int col = index % SolvingBoard.GetLength(1);
if (ResetBoard[row,col] != 0)
{
SolvingBoard[row, col] = ResetBoard[row, col];
return SolveSkyScrapers(index + 1);
}
for (int value = 1; value <= SolvingBoard.GetLength(0); value++)
{
if (ValidValue(value, row, col))
{
SolvingBoard[row, col] = value;
if (SolveSkyScrapers(index + 1))
{
return true;
}
}
SolvingBoard[row, col] = 0;
}
return false;
}
private bool ValidValue(int value, int row, int col)
{
if (!IsPossible(value, row, col))
return false;
int[] seq = new int[SolvingBoard.GetLength(0)];
if (!CheckAvailableSeqRow(seq, 0, row, 0))
return false;
seq = new int[SolvingBoard.GetLength(1)];
if (!CheckAvailableSeqCol(seq, 0, 0, col))
return false;
return true;
}
private bool CheckAvailableSeqRow(int[] seq, int specific_index, int row, int col)
{
if (specific_index == SolvingBoard.GetLength(0))
{
if (CheckEdges(0, seq, row))
return true;
return false;
}
if (SolvingBoard[row, col] != 0)
{
seq[specific_index] = SolvingBoard[row, col];
if (CheckAvailableSeqRow(seq, specific_index + 1, row, col +1))
return true;
seq[specific_index] = 0;
}
for (int value = 1; value <= seq.Length; value++)
{
if (!IsPossible(value,row,col) || !NoRepeatInseq(value,seq,specific_index))
continue;
seq[specific_index] = value;
if (CheckAvailableSeqRow(seq, specific_index + 1, row, col + 1))
return true;
seq[specific_index] = 0;
}
return false;
}
private bool IsPossible(int value, int row, int col)
{
for (int i = 0; i < SolvingBoard.GetLength(0); i++)
{
if (i != col)
if (SolvingBoard[row, i] == value)
return false;
if (i != row)
if (SolvingBoard[i, col] == value)
return false;
}
return true;
}
private bool NoRepeatInseq(int value, int[] seq, int specific_index)
{
for (int i = 0; i < SolvingBoard.GetLength(0); i++)
{
if (i != specific_index)
if (seq[i] == value)
return false;
}
return true;
}
private bool CheckAvailableSeqCol(int[] seq, int specific_index, int row, int col)
{
if (specific_index == SolvingBoard.GetLength(0))
{
if (CheckEdges(2, seq, col))
return true;
return false;
}
if (SolvingBoard[row, col] != 0)
{
seq[specific_index] = SolvingBoard[row, col];
if (CheckAvailableSeqCol(seq, specific_index + 1, row + 1, col))
return true;
seq[specific_index] = 0;
}
for (int value = 1; value <= seq.Length; value++)
{
if (!IsPossible(value, row, col) || !NoRepeatInseq(value, seq, specific_index))
continue;
seq[specific_index] = value;
if (CheckAvailableSeqCol(seq, specific_index + 1, row+ 1, col))
return true;
seq[specific_index] = 0;
}
return false;
}
private bool CheckEdges(int direction, int[] seq, int seq_number)
{
int count_left = 1;
int count_right = 1;
int max_left = seq[0];
int max_right = seq[seq.Length - 1];
for (int i=1; i<seq.Length; i++)
{
if (seq[i] > max_left)
{
max_left = seq[i];
count_left++;
}
}
for (int i = seq.Length-2; i >= 0; i--)
{
if (seq[i] > max_right)
{
max_right = seq[i];
count_right++;
}
}
if (count_left == Edges[direction, seq_number] && count_right == Edges[direction + 1, seq_number])
return true;
return false;
}
}
}