-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
137 lines (119 loc) · 5.67 KB
/
index.html
File metadata and controls
137 lines (119 loc) · 5.67 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
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
<html>
<head>
<title>Pure JavaScript Memory</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script language="javascript">
function Board(width, height, moveFunction) {
this.width = width;
this.height = height;
this.moveFunction = moveFunction;
this.squares = [];
this.clickedSquares = {first: null, second: null};
this.gameWon = false;
for (var y = 0; y < this.height; y++) {
this.squares[y] = [];
for (var x = 0; x < this.width; x++) {
this.squares[y][x] = 0;
}
}
this.populate = function () {
for (var number = 1; number <= this.width * this.height / 2; number++) {
this.placeNumber(number);
this.placeNumber(number);
}
}
this.placeNumber = function (number) {
for (; ;) {
var y = Math.floor(Math.random() * this.height);
var x = Math.floor(Math.random() * this.width);
if (this.squares[y][x] == 0) {
this.squares[y][x] = number;
return;
}
}
}
this.squaresMatch = function (firstSquare, secondSquare) {
return this.squares[firstSquare.y][firstSquare.x] == this.squares[secondSquare.y][secondSquare.x];
}
this.clickedSquare = function (id) {
if (this.gameWon) {
return;
}
var square = new Square(id);
if (this.sameSquareClicked(square)) {
return;
}
if (this.bothSquaresFlipped()) {
this.clickedSquares.first.clear();
this.clickedSquares.second.clear();
this.clickedSquares.first = this.clickedSquares.second = null;
}
this.flipSquare(square);
if (this.bothSquaresFlipped()) {
this.moveFunction();
if (this.squaresMatch(this.clickedSquares.first, this.clickedSquares.second)) {
this.gameWon = true;
showWinBanner();
}
}
}
this.flipSquare = function (square) {
if (this.clickedSquares.first == null) {
this.clickedSquares.first = square;
} else if (this.clickedSquares.first != null && this.clickedSquares.second == null) {
this.clickedSquares.second = square;
}
square.showContents();
}
this.sameSquareClicked = function (otherSquare) {
return this.clickedSquares.first != null && this.clickedSquares.first.id == otherSquare.id;
}
this.bothSquaresFlipped = function () {
return this.clickedSquares.first != null && this.clickedSquares.second != null;
}
}
function Square(id) {
this.id = id;
this.x = parseInt(id.substring(1, 2));
this.y = parseInt(id.substring(2, 3));
this.clear = function () {
document.getElementById(this.id).innerHTML = "<img src=\"back.png\"/>";
}
this.showContents = function () {
document.getElementById(this.id).innerHTML = "<img src=\"" + board.squares[this.y][this.x] + ".png\"/>";
}
}
function showWinBanner() {
document.getElementById("overlay").style.visibility = "visible";
}
var moves = 0;
var board = new Board(4, 4, function () {
document.getElementById("moveCounter").innerHTML = ++moves;
});
</script>
</head>
<body onload="board.populate();">
<div id="overlay" onclick="location.reload()"><img id="winbanner" src="win.png"/></div>
<div id="panel">
Moves: <span id="moveCounter">0</span>
</div>
<div id="grid">
<div class="cell" id="c00" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell" id="c10" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell" id="c20" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell end" id="c30" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell" id="c01" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell" id="c11" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell" id="c21" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell end" id="c31" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell" id="c02" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell" id="c12" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell" id="c22" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell end" id="c32" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell" id="c03" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell" id="c13" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell" id="c23" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
<div class="cell end" id="c33" onclick="board.clickedSquare(this.id)"><img src="back.png"/></div>
</div>
</body>
</html>