-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToeUI.cpp
131 lines (102 loc) · 3.48 KB
/
TicTacToeUI.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
#include "TicTacToeUI.h"
TicTacToeUI::TicTacToeUI() {
}
void TicTacToeUI::attachArm(TicTacToeArm* arm) {
this->arm = arm;
}
void TicTacToeUI::drawBoard(float x, float y, float width, float height) {
float dx, dy;
dx = width / 3;
dy = height / 3;
this->x = x;
this->y = y;
this->width = width;
this->height = height;
DEBUG << "Board X: " << x << " Board Y: " << y << " Board Width: " << width << " Board Height: " << height << " (dx= " << dx << ", dy= " << dy << ")";
this->arm->penUp();
for(int i = 2; i > 0; i--) {
this->arm->lineTo(x + (dx*i), y);
this->arm->penDown();
this->arm->lineTo(x + (dx*i), y + height);
this->arm->penUp();
}
for(int i = 1; i < 3; i++) {
this->arm->lineTo(x + width, y + (dy*i));
this->arm->penDown();
this->arm->lineTo(x, y + (dy*i));
this->arm->penUp();
}
this->arm->home();
}
void TicTacToeUI::drawO(int position) {
int row = (position-1)/3;
int column = (position-1)%3;
DEBUG << "Draw O Position: " << position;
this->drawO(this->x + this->width - ((column+1)*(this->width/3)) + ((this->height/3)/2), this->y + (this->height/3)*row + ((this->height/3)/2), ((this->width/3)*0.75)/2);
}
void TicTacToeUI::drawX(int position) {
int row = (position-1)/3;
int column = (position-1)%3;
DEBUG << "Draw X Position: " << position;
this->drawX(this->x + this->width - ((column+1)*(this->width/3)) + ((this->height/3)/2), this->y + (this->height/3)*row + ((this->height/3)/2), ((this->width/3)*0.75)/2);
}
void TicTacToeUI::drawX(float x, float y, float radius) {
DEBUG << "Draw X (x, y, radius): (" << x << ", " << y << ", " << radius << ")";
this->arm->penUp();
this->arm->lineTo(x+radius, y-radius);
this->arm->penDown();
this->arm->lineTo(x-radius, y+radius);
this->arm->penUp();
this->arm->lineTo(x+radius, y+radius);
this->arm->penDown();
this->arm->lineTo(x-radius, y-radius);
this->arm->penUp();
this->arm->home();
}
void TicTacToeUI::drawO(float x, float y, float radius) {
DEBUG << "Draw O (x, y, radius): (" << x << ", " << y << ", " << radius << ")";
static const int sides = 8;
this->arm->penUp();
for(int i = 0; i <= sides; i++) {
this->arm->lineTo(radius * cos(2*PI*i/sides) + x, radius * sin(2*PI*i/sides) + y);
arm->penDown();
}
this->arm->penUp();
this->arm->home();
}
void TicTacToeUI::drawWinner(TicTacToeWinner winnerData) {
switch(winnerData.type) {
case winType::ROW:
this->arm->penUp();
this->arm->lineTo(this->x + this->width, this->y + (this->height/3)*winnerData.position + ((this->height/3)/2));
this->arm->penDown();
this->arm->lineTo(this->x, this->y + (this->height/3)*winnerData.position + ((this->height/3)/2));
this->arm->penUp();
this->arm->home();
break;
case winType::COLUMN:
this->arm->penUp();
this->arm->lineTo(this->x + (this->width/3)*(2-winnerData.position) + ((this->width/3)/2), this->y);
this->arm->penDown();
this->arm->lineTo(this->x + (this->width/3)*(2-winnerData.position) + ((this->width/3)/2), this->y + this->height);
this->arm->penUp();
this->arm->home();
break;
case winType::DIAGONAL:
this->arm->penUp();
this->arm->lineTo(this->x + this->width, this->y);
this->arm->penDown();
this->arm->lineTo(this->x, this->y + this->height);
this->arm->penUp();
this->arm->home();
break;
case winType::ANTIDIAGONAL:
this->arm->penUp();
this->arm->lineTo(this->x + this->width, this->y + this->height);
this->arm->penDown();
this->arm->lineTo(this->x, this->y);
this->arm->penUp();
this->arm->home();
break;
}
}