-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
203 lines (187 loc) · 7.59 KB
/
index.html
File metadata and controls
203 lines (187 loc) · 7.59 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
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
---
title: Squarepad
permalink: /squarepad
---
<html>
<head>
<title>David: Squarepad</title>
</head>
<body>
<div id="game">
<svg height="400 " width="400 " style="background-color: aquamarine; ">
<rect x="4 " y="4 " width="95 " height="95 " style="border-width: 10px;stroke:black;fill:green;stroke-width: 8px; "></rect>
</svg>
</div>
{% include jsLiscenseGPL.html %}
<script>
var gameElement = document.getElementById("game ");
var color = [
[
[
"black ", "blue "
],
[
"yellow ", "green "
]
],
[
[
"red ", "purple "
],
[
"orange ", "white "
]
]
];
class game {
constructor(round) {
this.round = round;
//board is 1 indexed it's sad
this.board = [];
for (let row = 0; row <= round + 1; row++) {
this.board[row] = [];
for (let col = 0; col <= round + 1; col++) {
this.board[row][col] = Math.random() > 0.5 ? [0, 0, 0] : [1, 1, 1];
}
}
while (!this.boardReady()) {
var pickRow = this.rand1toround();
var pickCol = this.rand1toround();
while ((this.board[pickRow][pickCol].includes(1) && this.board[pickRow][pickCol].includes(0))) {
pickRow = this.rand1toround();
pickCol = this.rand1toround();
//console.log(pickRow, pickCol);
}
var color = [0, 0, 0];
color = color.map(() => Math.random() > 0.5 ? 1 : 0);
let affectedCells = [
[pickRow, pickCol + 1],
[pickRow, pickCol - 1],
[pickRow + 1, pickCol],
[pickRow - 1, pickCol],
[pickRow, pickCol]
];
affectedCells.forEach(function(cell) {
let oldCell = this.board[cell[0]][cell[1]];
let red = (oldCell[0] + color[0]) % 2;
let yellow = (oldCell[1] + color[1]) % 2;
let blue = (oldCell[2] + color[2]) % 2;
this.board[cell[0]][cell[1]] = [red, yellow, blue]
}.bind(this));
}
console.log(this.board);
this.displayBoard(); //html on page after this point
this.rectangles = [];
for (let row = 1; row <= this.round; row++) {
this.rectangles[row] = [];
for (let col = 1; col <= this.round; col++) {
let rectangle = document.getElementById(`${row},${col}`);
this.rectangles[row][col] = rectangle;
this.rectangles[rectangle] = [row, col]
}
}
}
rand1toround() {
return Math.floor(Math.random() * this.round) + 1
}
displayBoard() {
let displayArea = document.getElementById("game");
let svgWidth = 800;
let border = 8;
let innerWidth = svgWidth - border;
var htmlToDisplay = " "
var rectTags = " "
for (var row = 1; row <= this.round; row++) {
for (var col = 1; col <= this.round; col++) {
let cellColor = this.board[row][col];
let fill = color[cellColor[0]][cellColor[1]][cellColor[2]];
rectTags += `<rect x='${border/2 + innerWidth*(col-1)/this.round}' y='${border/2 + innerWidth*(row-1)/this.round}' width='${innerWidth/this.round} ' height='${innerWidth/this.round} ' style='border-width:${border}px;stroke:black;fill:${fill}' id='${row},${col}' onmousedown="handlemousedown(event, ${row},${col})"> </rect>\n`;
}
}
htmlToDisplay = `<svg width="${svgWidth}" height="${svgWidth}">
${rectTags}
</svg>`;
displayArea.innerHTML = htmlToDisplay;
}
onmousedown(e, row, col) {
let pos = this.rectangles[e.target];
var button = "wierd"
switch (e.which) {
case 1:
button = "left"
break;
case 3:
button = "right"
break;
default:
break;
}
this.handleClick(row, col, button);
}
boardReady() {
let black = [0, 0, 0];
let white = [1, 1, 1];
for (let row = 1; row <= this.round; row++) {
for (let col = 1; col <= this.round; col++) {
if (!(this.board[row][col].includes(1) && this.board[row][col].includes(0))) {
return false;
}
}
}
return true;
}
getSquare(row, col) {
return document.getElementById(row + "," + col);
}
hasWon() {
for (var row = 1; row <= this.round; row++) {
for (var col = 1; col <= this.round; col++) {
if (this.board[row][col].includes(1)) {
return false;
}
}
}
return true;
}
handleClick(row, col, button) {
let affectedCells = [
[row, col + 1],
[row, col - 1],
[row + 1, col],
[row - 1, col]
];
let cellColor = this.board[row][col];
affectedCells.forEach(function(cell) {
let oldCell = this.board[cell[0]][cell[1]];
let red = (oldCell[0] + cellColor[0]) % 2;
let yellow = (oldCell[1] + cellColor[1]) % 2;
let blue = (oldCell[2] + cellColor[2]) % 2;
this.board[cell[0]][cell[1]] = [red, yellow, blue]
}.bind(this));
if (!cellColor.includes(1)) {
this.board[row][col] = [1, 1, 1]
} else {
this.board[row][col] = button == "left" ? [0, 0, 0] : [1, 1, 1]
}
if (this.hasWon()) {
return currentGame = new game(this.round + 1);
}
for (var row = 1; row <= this.round; row++) {
for (var col = 1; col <= this.round; col++) {
let localColor = this.board[row][col];
let fill = color[localColor[0]][localColor[1]][localColor[2]];
this.getSquare(row, col).style.fill = fill;
}
}
}
}
var currentGame = new game(1);
function handlemousedown(e, row, col) {
currentGame.onmousedown(e, row, col);
}
function handleClick(row, col) {
return currentGame.handleClick(row, col);
}
</script>
</body>
</html>