-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckerBoard.cpp
More file actions
121 lines (112 loc) · 3.23 KB
/
CheckerBoard.cpp
File metadata and controls
121 lines (112 loc) · 3.23 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
#include "CheckerBoard.h"
#include <cstring>
CheckerBoard::CheckerBoard(Rect _pos)
{
pos = _pos;
memset(img_board, 0, sizeof img_board);
img_black = img_white = 0;
pRuler = nullptr;
pAI = nullptr;
col_human = Color::BLACK;
col_ai = Color::WHITE;
memset(board, 0, sizeof board);
mouse_pos = Point(-1, -1);
img_board[0] = LoadTexture("img//checkerboard//board0.bmp");
img_board[1] = LoadTexture("img//checkerboard//board1.bmp");
img_board[2] = LoadTexture("img//checkerboard//board2.bmp");
img_board[3] = LoadTexture("img//checkerboard//board3.bmp");
img_board[4] = LoadTexture("img//checkerboard//board4.bmp");
img_board[5] = LoadTexture("img//checkerboard//board5.bmp");
img_board[6] = LoadTexture("img//checkerboard//board6.bmp");
img_board[7] = LoadTexture("img//checkerboard//board7.bmp");
img_board[8] = LoadTexture("img//checkerboard//board8.bmp");
img_board[9] = LoadTexture("img//checkerboard//board9.bmp");
img_black = LoadTexture("img//piece//blackpiece.bmp");
img_white = LoadTexture("img//piece//whitepiece.bmp");
}
void CheckerBoard::Init(Color c_human, GameRule* p, GameAI* pai)
{
col_human = c_human;
col_ai = c_human == Color::BLACK ? Color::WHITE : Color::BLACK;
pRuler = p;
pAI = pai;
memset(board, 0, sizeof board);
}
void CheckerBoard::SetBoard(Color tboard[9][9])
{
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
board[i][j] = tboard[i][j];
}
void CheckerBoard::Draw()
{
int w = (pos.rx - pos.lx) / 9, h = (pos.ry - pos.ly) / 9;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
{
Rect dr = Rect(j * w, i * h, (j + 1) * w, (i + 1) * h);
int id = 0;
if (i == 0 && j == 0)
id = 1;
else if (i == 0 && j == 8)
id = 2;
else if (i == 8 && j == 8)
id = 3;
else if (i == 8 && j == 0)
id = 4;
else if (i == 8)
id = 5;
else if (j == 8)
id = 6;
else if (i == 0)
id = 7;
else if (j == 0)
id = 8;
else
id = 9;
DrawTexture(dr, img_board[id]);
}
for(int i=0;i<9;i++)
for (int j = 0; j < 9; j++)
{
if (board[i][j] == Color::BLACK)
DrawCircleTexture(Point(j * w + w / 2, i * h + h / 2), (int)(0.46 * w), 0.46, img_black);
else if (board[i][j] == Color::WHITE)
DrawCircleTexture(Point(j * w + w / 2, i * h + h / 2), (int)(0.46 * w), 0.46, img_white);
}
if (mouse_legal)
DrawTransCircle(Point(mouse_pos.y * w + w / 2, mouse_pos.x * h + h / 2), (int)(0.46 * w), 0.0, 1.0, 0.0, 0.5);
else
DrawTransCircle(Point(mouse_pos.y * w + w / 2, mouse_pos.x * h + h / 2), (int)(0.46 * w), 1.0, 0.0, 0.0, 0.5);
}
void CheckerBoard::SetMousePos(Point u)
{
if (in(u))
{
int w = (pos.rx - pos.lx) / 9, h = (pos.ry - pos.ly) / 9;
int y = u.x / w, x = u.y / h;
mouse_pos = Point(x, y);
mouse_legal = pRuler->isLegal(x, y, col_human) && pRuler->moveColor() == col_human;
}
else
mouse_pos = Point(-1, -1);
}
void CheckerBoard::OnMouseClick(Point a)
{
int w = (pos.rx - pos.lx) / 9, h = (pos.ry - pos.ly) / 9;
int y = a.x / w, x = a.y / h;
if (pRuler->isLegal(x, y, col_human) && pRuler->moveColor() == col_human)
{
pRuler->setPiece(x, y, col_human);
pAI->PlayerMove(Point(x,y));
board[x][y] = col_human;
}
}
bool CheckerBoard::in(Point a)
{
return pos.inRect(a);
}
void CheckerBoard::setPiece(int x, int y, Color col)
{
board[x][y] = col;
}