-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameboard.cpp
More file actions
91 lines (82 loc) · 1.83 KB
/
gameboard.cpp
File metadata and controls
91 lines (82 loc) · 1.83 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
#define GL_SILENCE_DEPRECATION
#include <GLUT/glut.h>
#include <unistd.h>
#include "graphics.h"
#include "gameboard.h"
using namespace std;
Board::Board(PoisonCookie * poison)
: mX(poison->getX()), mY(poison->getY()) {
mPoison = poison;
mCookies = new Cookie*[mY];
for (int y = 0; y < mY; y++) {
mCookies[y] = new Cookie[mX];
for (int x = 0; x < mX; x++) {
mCookies[y][x] = Cookie();
}
}
}
Board::~Board() {
for (int y = 0; y < mY; y++) {
delete [] mCookies[y];
mCookies[y] = 0;
}
delete [] mCookies;
mCookies = 0;
}
void Board::updateCookies() {
int ** board = mPoison->getBoard();
for (int y = 0; y < mY; y++) {
for (int x = 0; x < mX; x++) {
mCookies[y][x].mAlive = board[y][x] == 1;
}
}
}
void Board::Draw() {
// Draw the board lines
glColor3d(1, 82.0/255, 82.0/255); // #FF5252
for (int i = 0; i <= mX; i++) {
DrawLine(i, 0, i, mY);
}
for (int i = 0; i <= mY; i++) {
DrawLine(0, i, mX, i);
}
//Draw the Poisoned Cookie a different color
mCookies[0][0].Draw(0, 0, true);
//Draw cookies
for (int y = 0; y < mY; y++) {
for (int x = 0; x < mX; x++) {
if (x == 0 && y == 0)
continue;
mCookies[y][x].Draw(x, y);
}
}
}
void Board::Move(int x, int y) {
mPoison->move(x, y, 0); updateCookies();
}
void Board::Computer() {
mPoison->computerMove(); updateCookies();
}
Cookie::Cookie()
: mAlive(true) {
for (int i = 0; i < 3; i++ ) {
Chip c;
c.x = ((rand() % 50)+25) / 100.0;
c.y = ((rand() % 50)+25) / 100.0;
c.r = .1;
mChips.push_back(c);
}
}
void Cookie::Draw(int x, int y, bool poison) {
if (mAlive) {
glColor3d(220.0/255, 146.0/255, 71.0/255);
DrawCircle(x+.5, y+.5, .4, .01);
if (poison)
glColor3d(0,.7,0);
else
glColor3d(73.0/255, 34.0/255, 4.0/255);
for (unsigned int i = 0; i < mChips.size(); i++) {
DrawCircle(x + mChips[i].x, y + mChips[i].y, mChips[i].r, .01);
}
}
}