-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameGridModel.js
179 lines (142 loc) · 4.4 KB
/
GameGridModel.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @providesModule GameGridModel
* @flow
*/
'use strict';
var JewelModel = require('JewelModel');
var GameGridModel = function(rows, columns, types, gridSize) {
this.rows = rows || 9;
this.columns = columns || 9;
this.types = types || 4;
this.jewelSize = gridSize/this.columns;
this.gridSize = gridSize;
this.jewels = [];
this.init();
};
// Create a grid full of random jewel types
GameGridModel.prototype.init = function () {
var data = [];
var columns;
var jewels = [];
var i = 0;
for (var r = 0; r < this.rows; r++) {
columns = [];
for (var c = 0; c < this.columns; c++) {
jewels[i] = new JewelModel({
column: c,
row: r,
size: this.jewelSize,
type: Math.floor(Math.random() * this.types) + 1,
});
columns[c] = jewels[i];
i++;
}
data[r] = columns;
}
this.jewels = jewels;
this.grid = data;
console.log(this.grid);
};
// Check if given type matches the one at the given row and column
GameGridModel.prototype.matchType = function(row, column, jewel) {
if(this.grid[row] && this.grid[row][column] && this.getType(row, column) === jewel.type) {
return 1;
}
return false;
};
// Get type of jewel at given grid position
GameGridModel.prototype.getType = function(row, column) {
return this.getJewel(row, column).type;
};
// Get jewel at given grid position
GameGridModel.prototype.getJewel = function(row, column) {
console.log(row, column, this.grid);
return this.grid[row][column];
};
// Get jewel at given grid position
GameGridModel.prototype.getJewelAtIndex = function(index) {
var point = this.mapIndex(index);
return this.getJewel(point.row, point.column);
};
// Convert row and columns to cell index
GameGridModel.prototype.mapToIndex = function(row, column) {
return (row * this.grid.length) + column;
};
// Do a recursive search to find ajacent matching jewels
GameGridModel.prototype.findMatches = function(jewel, matches) {
var row, column;
// Try North
row = jewel.row - 1;
column = jewel.column;
if(matches.indexOf(this.mapToIndex(row, column)) === -1 && this.matchType(row, column, jewel)) {
matches.push(this.mapToIndex(row, column));
matches = this.findMatches({row: row, column: column, type: jewel.type}, matches);
}
// Try East
row = jewel.row;
column = jewel.column + 1;
if(matches.indexOf(this.mapToIndex(row, column)) === -1 && this.matchType(row, column, jewel)) {
matches.push(this.mapToIndex(row, column));
matches = this.findMatches({row: row, column: column, type: jewel.type}, matches);
}
// Try South
row = jewel.row + 1;
column = jewel.column;
if(matches.indexOf(this.mapToIndex(row, column)) === -1 && this.matchType(row, column, jewel)) {
matches.push(this.mapToIndex(row, column));
matches = this.findMatches({row: row, column: column, type: jewel.type}, matches);
}
// Try West
row = jewel.row;
column = jewel.column -1;
if(matches.indexOf(this.mapToIndex(row, column)) === -1 && this.matchType(row, column, jewel)) {
matches.push(this.mapToIndex(row, column));
matches = this.findMatches({row: row, column: column, type: jewel.type}, matches);
}
return matches;
};
// Map cell index to row and colum number
GameGridModel.prototype.mapIndex = function(index) {
var row, column;
row = Math.floor(index / this.grid.length);
column = index - (row * 9);
return {
row: row,
column: column
};
};
// Keep bubbling jewel upwards
GameGridModel.prototype.bubbleJewel = function(jewel) {
var jewelAbove;
if(jewel.row > 0) {
jewelAbove = this.getJewel(jewel.row-1, jewel.column);
// Swap jewel positions
jewelAbove.row++;
jewel.row--;
// Log new jewels in grid
this.updateGridWithJewel(jewel);
this.updateGridWithJewel(jewelAbove);
// Continue bubbling
jewel.type = 0;
this.bubbleJewel(jewel);
}
};
GameGridModel.prototype.swapJewels = function(jewelA, jewelB) {
var r1, c1, r2, c2;
r1 = Math.abs(jewelA.row);
c1 = Math.abs(jewelA.column);
r2 = Math.abs(jewelB.row);
c2 = Math.abs(jewelB.column);
jewelA.row = r2;
jewelA.column = c2;
jewelB.row = r1;
jewelB.column = c1;
// Log new jewels in grid
this.updateGridWithJewel(jewelA);
this.updateGridWithJewel(jewelB);
};
// Update grid to reflect jewel change
GameGridModel.prototype.updateGridWithJewel = function(jewel) {
this.grid[jewel.row][jewel.column] = jewel;
};
module.exports = GameGridModel;