-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppuzzelflower.html
278 lines (222 loc) · 5.63 KB
/
ppuzzelflower.html
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<!DOCTYPE html>
<!--Joel Sejas -->
<!--credits to Brad Manderscheid for the concept I found on
https://code.tutsplus.com/tutorials/create-an-html5-canvas-tile-swapping-puzzle--active-10747
although I used a simpler game where you click for the switch not drag
-->
<html>
<head>
<title>CS80 Final</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<style>
.header {
padding: 1%;
background-color: #44464c;
border: 1px solid #e9e9e9;
}
.button {
background-color: whitesmoke;
border: none;
color: black;
padding: 15px 25px;
text-align: center;
font-size: 16px;
cursor: pointer;
}
.button:hover {
background-color: dimgrey;
</style>
</head>
<body bgcolor="#5a6070" text="#FFFFFF" alink="#FFFFFF" vlink="#00CCFF">
<div class="header">
<h1>CS80 Final Project</h1>
</div>
<br>
<table width="312" border="0" cellpadding="10" height="50" align ="center">
<tr align="center" valign="middle">
<td width="288">
<div align="center"><b><font color="#FFFFFF" size="5" face="Georgia, t New Roman, t, serif">Picture Puzzel</font></b></div>
</td>
</tr>
</table>
<br>
<table width="200" border="2" cellpadding="5" height="800" align ="left">
<tr align="left" valign="top">
<td width="187" height="173">
<ul>
<li><b><a href = "main.html"><font color="#FFFFFF">Home</font></a></b></li>
<li><b><a href = "ppuzzel.html"><font color="#FFFFFF">Picture Puzzel</font></a></b></li>
<li><b><a href = "coinflip.html"><font color="#FFFFFF">Coin Flip</font></a></b></li>
<li><b><a href = "rps.html"><font color="#FFFFFF">Rock Paper Scissors</font></a></b></li>
</ul>
</td>
</tr>
</table>
<table width="5%" height="400" border="0" cellpadding="1" cellspacing="0" align="left">
<tr align="left" valign="top">
<td ></td>
</tr>
</table>
<script type="text/javascript">
window.onload = onReady;
var can;
var ctx;
var img;
var blockSize = 160;
var clickX;
var clickY;
var s1;
var s2;
var piecesArray = new Array();
var correctOrder = new Array();
function onReady()
{
can = document.getElementById('myCanvas');
ctx = can.getContext('2d');
img = new Image();
img.onload = onImage1Load;
img.src = "flower.jpg";
}
function onImage1Load()
{
var r;
for(var i = 0; i < 4; i++)
{
for(var j = 0; j < 3; j++)
{
r = new Rectangle(i * blockSize, j * blockSize, i*blockSize + blockSize, j * blockSize + blockSize);
piecesArray.push(r);
correctOrder.push(r);
}
}
mix(piecesArray, 30);
drawImage();
}
function onCanvasClick(evt)
{
clickX = evt.offsetX;
clickY = evt.offsetY;
var drawX = Math.floor(clickX / blockSize);
var drawY = Math.floor(clickY / blockSize);
var index = drawX * 3 + drawY;
var targetRect = piecesArray[index];
var drawHighlight = true;
drawX *= blockSize;
drawY *= blockSize;
ctx.clearRect(0, 0, 640, 480);
if(s1 != undefined && s2 != undefined)
{
s1 = s2 = undefined;
}
if(s1 == undefined)
{
s1 = targetRect;
}
else
{
s2 = targetRect;
swapRects(s1, s2);
drawHighlight = false;
}
drawImage();
if(drawHighlight)
highlightRect(drawX, drawY);
}
function highlightRect(drawX, drawY)
{
console.log(drawX, drawY);
ctx.beginPath();
ctx.moveTo(drawX, drawY);
ctx.lineTo(drawX + blockSize, drawY);
ctx.lineTo(drawX + blockSize, drawY + blockSize);
ctx.lineTo(drawX, drawY + blockSize);
ctx.lineTo(drawX, drawY);
ctx.lineWidth = 2;
// set line color
ctx.strokeStyle = "green";
ctx.stroke();
}
function swapRects(r1, r2)
{
var index1;
var index2;
var temp = r1;
index1 = piecesArray.indexOf(r1);
index2 = piecesArray.indexOf(r2);
piecesArray[index1] = r2;
piecesArray[index2] = temp;
checkWinner();
}
function checkWinner()
{
var match = true;
for(var i = 0; i < piecesArray.length; i++)
{
if(piecesArray[i] != correctOrder[i])
{
match = false;
}
}
if(match)
{
console.log('complete');
}
else
{
console.log('not complete');
}
}
function drawImage()
{
for(var k = 0; k < 4; k++)
{
for(var l = 0; l < 3; l++)
{
r = piecesArray[k*3+l];
ctx.drawImage(img, r.left, r.top, r.width, r.height, k*blockSize, l*blockSize, blockSize, blockSize);
}
}
}
function mix(ar, t)
{
var count = 0;
var temp;
var index1;
var index2;
while(count < t)
{
index1 = Math.floor(Math.random()*piecesArray.length);
index2 = Math.floor(Math.random()*piecesArray.length);
temp = piecesArray[index1];
piecesArray[index1] = piecesArray[index2];
piecesArray[index2] = temp;
count++;
}
}
function Rectangle(left, top, right, bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
this.width = right - left;
this.height = bottom - top;
}
function isCanvasSupported()
{
var elem = document.createElement('canvas');
return (elem.getContext && elem.getContext('2d'));
}
</script>
<p>
<div style="margin-left:auto; margin-right:auto; width:640px; height:480px; border: 0px grey;">
<canvas id="myCanvas" width="640" height="480" onclick="onCanvasClick(event);">
</canvas>
</div>
<br><br>
<a class="button" href="ppuzzel.html" align="center">Try Again</a>
</p>
<br>
</body>
</html>