Repository navigation
Expand file tree
/
Copy pathGoBang.cpp
More file actions
155 lines (147 loc) · 3.63 KB
/
Copy pathGoBang.cpp
File metadata and controls
155 lines (147 loc) · 3.63 KB
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
#include "GoBang.hpp"
#include <iostream>
GoBang::GoBang()
{
}
int GoBang::ComputerPlay(enum Player player)
{
struct Point position = {0, 0};
srand((unsigned int)time(NULL));
while (1)
{
position.x = (rand() % 15) + 1;
position.y = (rand() % 15) + 1;
// std::cout << chessBoard.get(position);
if (this->chessBoard.get(position) == 'b')
{
break;
}
}
// std::cout << (char)player << std::endl;
this->chessBoard.put(position, (char)player);
this->previousStep = position;
this->previousPlayer = player;
return 0;
}
int GoBang::PlayerPlay(enum Player player, struct Point position)
{
if ((this->chessBoard.get(position) == 'o') ||
(this->chessBoard.get(position) == 'x'))
{
return 1; // which represents false return
}
else
{
this->chessBoard.put(position, player);
this->previousStep = position;
this->previousPlayer = player;
return 0;
}
return 0;
}
int checkInRange(int x, int y)
{
return ((x < 15) && (x > 0) &&
(y < 15) && (y > 0));
}
bool GoBang::Victory()
{
const int arrayX[] = {1, 1, 0, -1};
const int arrayY[] = {0, 1, 1, 1};
for (int arrayPos = 0; arrayPos < 4; ++arrayPos)
{
int acc = 0;
int coordinateX = this->previousStep.x;
int coordinateY = this->previousStep.y;
int addX = arrayX[arrayPos];
int addY = arrayY[arrayPos];
int DirectionFlag = 0;
while (true)
{
bool inrange = checkInRange(coordinateX + addX, coordinateY + addY);
if (inrange && this->chessBoard.get(coordinateX + addX, coordinateY + addY) == this->previousPlayer)
{
acc += 1;
coordinateX += addX;
coordinateY += addY;
}
else
{
if (DirectionFlag == 0)
{
coordinateX = this->previousStep.x;
coordinateY = this->previousStep.y;
addX *= -1;
addY *= -1;
DirectionFlag = 1;
}
else
{
break;
}
}
}
if (acc >= 4)
{
return true;
}
}
return false;
}
void GoBang::PrintChessBoard()
{
//system("clear");
for (int i = 0; i <= 15; ++i)
{
if (i == 0)
{
printf(" ");
}
for (int j = 0; j <= 15; ++j)
{
if (i == 0)
{
if (j != 0)
{
printf("%d ", j); // "%d "
if (j <= 9)
{
printf(" ");
}
}
else
{
printf(" ");
}
}
else if (j == 0)
{
printf("%d", i); // "%d"
if (i <= 9)
printf(" ");
}
else
{
if (i <= 15)
{
printf("%c |", this->chessBoard.get(i, j)); // "%c |"
}
}
}
std::cout << std::endl;
std::cout << " "; // " "
for (int m = 0; m < 15; m++)
{
printf("--|");
}
std::cout << std::endl;
}
}
std::string GoBang::Status()
{
return this->chessBoard.toStr();
}
void GoBang::PreviousInfo(struct Point &step, Player &player){
step = this->previousStep;
player = this->previousPlayer;
}