-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiece.cpp
142 lines (133 loc) · 3.8 KB
/
piece.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 "graphics.h"
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include "piece.h"
using namespace std;
Piece *Piece::pieces = NULL;
int Piece::side = 18;
int Piece::space = 1;
void Piece::testDraw() {
Piece *pieces = Piece::getPieces(), *temp;
int i, gd = DETECT, gm;
Point cur (10, 4 * side + space);
initgraph (&gd, &gm, "C:\\TurboC3\\BGI");
for (i = 0; i < 4; i++) {
temp = pieces + i;
do {
temp->draw (cur);
cur.x += (5 * side) + (6 * space);
temp = temp->getRotation();
} while (temp != pieces + i);
cur.x = 0;
cur.y += (5 * side) + (6 * space);
}
clearviewport();
cur.y = 4 * side + space;
for (i = 4; i < 7; i++) {
temp = pieces + i;
do {
temp->draw (cur);
cur.x += (5 * side) + (6 * space);
temp = temp->getRotation();
} while (temp != pieces + i);
cur.x = 0;
cur.y += (5 * side) + (6 * space);
}
}
void Piece::draw (Point cur) {
int i;
char sk[5];
for (i = 0; i < 4; i++) {
rectangle (cur.x + body[i].x * side + space,
cur.y - body[i].y * side - space,
cur.x + (body[i].x + 1) *side - space,
cur.y - (body[i].y + 1) *side + space);
}
for (i = 0; i < width; i++) {
sk[i] = (char) ( (int) (skirt[i]) + (int) ('0') );
}
sk[i] = '\0';
outtextxy (cur.x + width * side / 2, cur.y + 5 * space, sk);
}
Piece::Piece (char pointlist[9], int c) {
int i, j;
height = width = 0;
next = NULL;
color = c;
for (i = 0; i < 8; i += 2) {
body[i / 2] = Point (ctoi (pointlist[i]), ctoi (pointlist[i + 1]) );
}
for (i = 0; i < 4; i++) {
if (body[i].y > height) {
height = body[i].y;
}
if (body[i].x > width) {
width = body[i].x;
}
}
height++;
width++;
skirt = new int[width];
for (i = 0; i < width; i++) {
skirt[i] = 10;
for (j = 0; j < 4; j++)
if (body[i].x == i && body[j].y < skirt[i]) {
skirt[i] = body[j].y;
}
}
}
Piece *Piece::getPieces() {
int i;
Piece *p;
p = new Piece[7];
p[0] = Piece ("00010203", LIGHTGREEN);
p[1] = Piece ("00010210", LIGHTRED);
p[2] = Piece ("00101112", YELLOW);
p[3] = Piece ("00101121", LIGHTCYAN);
p[4] = Piece ("01111020", LIGHTMAGENTA);
p[5] = Piece ("00011011", BROWN);
p[6] = Piece ("00101120", LIGHTGRAY);
for (i = 0; i < 7; i++) {
p[i].linkRotations();
}
pieces = p;
return p;
}
void Piece::linkRotations() {
Piece *temp = this;
char newpointslist[9];
int i;
do {
for (i = 0; i < 8; i += 2) {
newpointslist[i] = (char) ('0' + temp->height - 1 - temp->body[i / 2].y);
newpointslist[i + 1] = (char) ('0' + temp->body[i / 2].x);
}
newpointslist[8] = '\0';
temp->next = new Piece (newpointslist, color);
if (equals (temp->next) ) {
delete temp->next;
temp->next = this;
} else if (temp->next == NULL) {
cout << "Low memory" << endl;
getch();
exit (0);
} else {
temp = temp->next;
}
} while (temp->next != this);
}
int Piece::equals (Piece *p) {
int i, j, pointmatch = 1;
for (i = 0; i < 4 && pointmatch; i++) {
pointmatch = 0;
for (j = 0; j < 4; j++)
if (body[i] == p->body[j]) {
pointmatch = 1;
}
}
return pointmatch;
}
int ctoi (char c) {
return (int) (c - '0');
}