-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diamond.js
185 lines (174 loc) · 4.82 KB
/
Diamond.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
180
181
182
183
184
185
var diamondList = new Array(4);
var baseColor = 10;
//初始化格局
function initRound() {
//先随机生成位置
var fristGridRow = GetRandomNum(0, 3);
var fristGridColumn = GetRandomNum(0, 3);
var valueOfGrid = GetRandomNum(1, 2);
valueOfGrid *= 2;
var diamond = new Diamond(fristGridRow, fristGridColumn, valueOfGrid);
//然后检查位置是否被占用
//如被占用则顺序找出未被占用的位置
var row = diamond.row;
var colunm = diamond.colunm;
var isConflict = isExistAt(row, colunm, diamondList);
var haveFind = true;
if(isConflict) {
haveFind = false;
for(var i = 0; i < 4; i++) {
for(var j = 0; j < 4; j++) {
isConflict = isExistAt(i, j, diamondList);
if(!isConflict) {
haveFind = true;
colunm = j;
break;
}
}
if(haveFind) {
row = i;
break;
}
}
}
if(haveFind) {
diamond.row = row;
diamond.colunm = colunm;
generateDiv(diamond);
diamondList[row.toString() + colunm.toString()] = diamond;
} else {
console.log("图表已满,找不到空位");
}
}
function isExistAt(row, colunm, inDiamondList) {
var diamond = inDiamondList[row.toString() + colunm.toString()];
if(!diamond){
return false;
}
else
{
return true;
}
}
function generateDiv(diamondPara) {
var diamondID = "diamond" + diamondPara.row + diamondPara.colunm;
console.log("生成方块UI" + diamondID);
var createDiaObject = $("<div>").attr("id",diamondID);
$("#page").append(createDiaObject);
$("#" + diamondID).attr("class","diamond");
var topValue = $("#td" + diamondPara.row + diamondPara.colunm).offset().top;
var leftValue = $("#td" + diamondPara.row + diamondPara.colunm).offset().left;
$("#" + diamondID).offset({top:topValue,left:leftValue});
$("#" + diamondID).css("background-color",diamondPara.color(diamondPara.value));
$("#" +diamondID).text(diamondPara.value);
flash("#" + diamondID,3,10,50);
console.log("生成方块" + diamondPara.row + diamondPara.colunm);
}
function generateCopyDiv(diamondPara) {
var diamondID = "copydiamond" + diamondPara.row + diamondPara.colunm;
console.log("生成方块UI" + diamondID);
var createDiaObject = $("<div>").attr("id",diamondID);
$("#page").append(createDiaObject);
$("#" + diamondID).attr("class","diamond");
var topValue = diamondPara.row * 50 + $("#tableCopy").offset().top;
var leftValue = diamondPara.colunm * 50 + $("#tableCopy").offset().left;
$("#" + diamondID).offset({top:topValue,left:leftValue});
$("#" + diamondID).css("background-color",diamondPara.color(diamondPara.value));
$("#" +diamondID).text(diamondPara.value);
console.log("生成方块" + diamondPara.row + diamondPara.colunm);
}
function GetRandomNum(Min, Max) {
var Range = Max - Min;
var Rand = Math.random();
return(Min + Math.round(Rand * Range));
}
//可移动的方格对象
function Diamond(row, colunm, value) {
this.row = row;
this.colunm = colunm;
this.value = value;
this.lastRow;
this.lastColunm;
this.lastValue;
this.mergeTo = false;//被并入下一个格子
this.color = function(value) {
var result;
switch (value){
case 2:
{
result = "#fdffdf";
}
break;
case 4:
{
result = "#f3d7b5";
}
break;
case 8:
{
result = "#efcee8";
}
break;
case 16:
{
result = "#daf9ca";
}
break;
case 32:
{
result = "#c7b3e5";
}
break;
case 64:
{
result = "#ffebcc";
}
break;
case 128:
{
result = "#ffb86c";
}
break;
case 256:
{
result = "#f01b2d";
}
break;
case 512:
{
result = "#869b74";
}
break;
case 1024:
{
result = "#ff5983";
}
break;
case 2048:
{
result = "#c1194e";
}
break;
default:
{
result = "#000000";
}
break;
}
return result;
}
this.id = function(diamond) {
return "diamond" + diamond.row + diamond.colunm;
}
this.merged = false;//有格子并入
}
$.fn.getHexBackgroundColor = function(rgb) {
if($.browser.msie && $.browser.version > 8 || $.browser.mozilla || $.browser.webkit) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return("0" + parseInt(x).toString(16)).slice(-2);
}
rgb = "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
return rgb;
}