-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.cpp
85 lines (77 loc) · 1.53 KB
/
checker.cpp
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
#include "checker.hpp"
bool checker(int **board, int size)
{
bool mainDiagonal = checkMainDiagonal(board, size);
bool secondaryDiagonal = checkSecondaryDiagonal(board, size);
return (mainDiagonal && secondaryDiagonal);
}
bool checkMainDiagonal(int **board, int size)
{
int count;
for (int i = 0; i < size; i++)
{
count = 0;
for (int j = 0; (j + i) < size; j++)
{
if (board[j][j + i])
{
count++;
}
}
if (count > 1)
{
return false;
}
}
for (int i = 1; i < size; i++)
{
count = 0;
for (int j = 0; (j + i) < size; j++)
{
if (board[i + j][j])
{
count++;
}
}
if (count > 1)
{
return false;
}
}
return true;
}
bool checkSecondaryDiagonal(int **board, int size)
{
int count;
for (int i = 0; i < size; i++)
{
count = 0;
for (int j = i, k = 0; j >= 0; j--, k++)
{
if (board[j][k])
{
count++;
}
}
if (count > 1)
{
return false;
}
}
for (int i = 1; i < size; i++)
{
count = 0;
for (int j = (size-1), k = i; k < size; j--, k++)
{
if (board[j][k])
{
count++;
}
}
if (count > 1)
{
return false;
}
}
return true;
}