-
Notifications
You must be signed in to change notification settings - Fork 0
/
Judge.cpp
112 lines (101 loc) · 2.67 KB
/
Judge.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
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
#include "Judge.h"
bool userWin(const int x, const int y, const int M, const int N, int* const* board){
//横向检测
int i, j;
int count = 0;
for (i = y; i >= 0; i--)
if (!(board[x][i] == 1))
break;
count += (y - i);
for (i = y; i < N; i++)
if (!(board[x][i] == 1))
break;
count += (i - y - 1);
if (count >= 4) return true;
//纵向检测
count = 0;
for (i = x; i < M; i++)
if (!(board[i][y] == 1))
break;
count += (i - x);
if (count >= 4) return true;
//左下-右上
count = 0;
for (i = x, j = y; i < M && j >= 0; i++, j--)
if (!(board[i][j] == 1))
break;
count += (y - j);
for (i = x, j = y; i >= 0 && j < N; i--, j++)
if (!(board[i][j] == 1))
break;
count += (j - y - 1);
if (count >= 4) return true;
//左上-右下
count = 0;
for (i = x, j = y; i >= 0 && j >= 0; i--, j--)
if (!(board[i][j] == 1))
break;
count += (y - j);
for (i = x, j = y; i < M && j < N; i++, j++)
if (!(board[i][j] == 1))
break;
count += (j - y - 1);
if (count >= 4) return true;
return false;
}
bool machineWin(const int x, const int y, const int M, const int N, int* const* board){
//横向检测
int i, j;
int count = 0;
for (i = y; i >= 0; i--)
if (!(board[x][i] == 2))
break;
count += (y - i);
for (i = y; i < N; i++)
if (!(board[x][i] == 2))
break;
count += (i - y - 1);
if (count >= 4) return true;
//纵向检测
count = 0;
for (i = x; i < M; i++)
if (!(board[i][y] == 2))
break;
count += (i - x);
if (count >= 4) return true;
//左下-右上
count = 0;
for (i = x, j = y; i < M && j >= 0; i++, j--)
if (!(board[i][j] == 2))
break;
count += (y - j);
for (i = x, j = y; i >= 0 && j < N; i--, j++)
if (!(board[i][j] == 2))
break;
count += (j - y - 1);
if (count >= 4) return true;
//左上-右下
count = 0;
for (i = x, j = y; i >= 0 && j >= 0; i--, j--)
if (!(board[i][j] == 2))
break;
count += (y - j);
for (i = x, j = y; i < M && j < N; i++, j++)
if (!(board[i][j] == 2))
break;
count += (j - y - 1);
if (count >= 4) return true;
return false;
}
bool isTie(const int N, const int* top){
bool tie = true;
for (int i = 0; i < N; i++)
{
if (top[i] > 0)
{
tie = false;
break;
}
}
return tie;
}