-
Notifications
You must be signed in to change notification settings - Fork 4
/
Controller.cpp
142 lines (115 loc) · 2.56 KB
/
Controller.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
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
#include "Controller.h"
Controller::Controller(QObject *parent, int row, int column)
: QObject(parent),
m_row(row),
m_column(column)
{
m_board = new Qizi*[m_row];
for (int r = 0; r < m_row; r++)
m_board[r] = new Qizi[m_column];
}
Controller::~Controller()
{
for (int r = 0; r < m_row; r++)
delete[] m_board[r];
delete[] m_board;
}
void Controller::reset()
{
m_myQizi = BLACK_QIZI;
m_myTurn = false;
clear();
}
void Controller::start(Qizi qizi, bool firstDrop)
{
m_myQizi = qizi;
m_myTurn = firstDrop;
clear();
}
bool Controller::drop(Step step)
{
if (m_board[step.pos.column][step.pos.row] != EMPTY_QIZI)
return false;
m_board[step.pos.column][step.pos.row] = step.qizi;
if (step.qizi == m_myQizi)
m_myTurn = false;
else
m_myTurn = true;
return true;
}
bool Controller::remove(Pos pos)
{
if (m_board[pos.column][pos.row] == EMPTY_QIZI)
return false;
m_board[pos.column][pos.row] = EMPTY_QIZI;
return true;
}
void Controller::clear()
{
for (int c = 0; c < m_column; c++)
for (int r = 0; r < m_row; r++)
m_board[c][r] = EMPTY_QIZI;
}
Qizi Controller::myQizi()
{
return m_myQizi;
}
Qizi Controller::otherQizi()
{
if (m_myQizi == BLACK_QIZI)
return WHITE_QIZI;
else if (m_myQizi == WHITE_QIZI)
return BLACK_QIZI;
return EMPTY_QIZI;
}
bool Controller::isMyTurn()
{
return m_myTurn;
}
void Controller::setMyTurn(bool myTurn)
{
m_myTurn = myTurn;
}
Qizi Controller::getQizi(Pos pos)
{
return m_board[pos.column][pos.row];
}
bool Controller::checkWin(Step lastStep)
{
int up = countSameQizi(lastStep, 0, -1);
int down = countSameQizi(lastStep, 0, 1);
if (up + down >= 4)
return true;
int left = countSameQizi(lastStep, -1, 0);
int right = countSameQizi(lastStep, 1, 0);
if (left + right >= 4)
return true;
int leftUp = countSameQizi(lastStep, -1, -1);
int rightDown = countSameQizi(lastStep, 1, 1);
if (leftUp + rightDown >= 4)
return true;
int upRight = countSameQizi(lastStep, 1, -1);
int downLeft = countSameQizi(lastStep, -1, 1);
if (upRight + downLeft >= 4)
return true;
return false;
}
int Controller::countSameQizi(Step step, int colOffset, int rowOffset)
{
Pos besidePos;
besidePos.column = step.pos.column + colOffset;
besidePos.row = step.pos.row + rowOffset;
if (besidePos.column < 0 ||
besidePos.row >= m_column ||
besidePos.row < 0 ||
besidePos.row >= m_row)
return 0;
if (getQizi(besidePos) == step.qizi)
{
Step besideStep;
besideStep.pos = besidePos;
besideStep.qizi = step.qizi;
return countSameQizi(besideStep, colOffset, rowOffset) + 1;
}
return 0;
}