Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FreshPenguin112
GitHub Repository: FreshPenguin112/bookmarklets
Path: blob/main/ticmobile.js
5051 views
1
// Randomize the first marker.
2
var turn=Math.random()<1/2 ? 0 : 1;
3
4
// Set unit size.
5
var unit=(innerWidth<innerHeight ? innerWidth : innerHeight)/11;
6
7
function newUnit(){
8
unit=(innerWidth<innerHeight ? innerWidth : innerHeight)/11;
9
}
10
11
// Create menu.
12
var menu=document.createElement('div');
13
window.addEventListener('resize',function(){ newUnit();menu.style.height=9*unit+'px';menu.style.width=9*unit+'px'; });
14
menu.style.position='fixed';
15
menu.style.top='50%';
16
menu.style.left='50%';
17
menu.style.height=9*unit+'px';
18
menu.style.width=9*unit+'px';
19
menu.style.transform='translate(-50%,-50%)';
20
menu.style.webkitTransform='translate(-50%,-50%)';
21
menu.style.zIndex=9999;
22
menu.innerHTML='<a style=\'background:white;position:absolute;left:100%;cursor:pointer\' onClick=\'menu.remove()\'>exit</a>';
23
document.body.appendChild(menu);
24
25
// Create board.
26
var board=document.createElement('div');
27
board.style.background='rgba(255,255,255,0.9)';
28
board.style.position='absolute';
29
board.style.top=0;
30
board.style.left=0;
31
board.style.height='100%';
32
board.style.width='100%';
33
board.mark=null;
34
menu.appendChild(board);
35
36
// Create macro and micro maps.
37
BOXES=[];
38
boxes=[[],[],[],[],[],[],[],[],[]];
39
40
// Fill the board.
41
for(var i=0;i<3;i++){
42
for(var j=0;j<3;j++){
43
var BOX=document.createElement('div');
44
BOX.style.position='absolute';
45
BOX.style.top=i*100/3+'%';
46
BOX.style.left=j*100/3+'%';
47
BOX.style.height=100/3+'%';
48
BOX.style.width=100/3+'%';
49
BOX.mark=null;
50
51
for(var k=0;k<3;k++){
52
for(var l=0;l<3;l++){
53
var box=document.createElement('div');
54
55
box.setAttribute('onMouseOver','this.style.background=\'rgba(0,0,0,0.25)\'');
56
box.setAttribute('onMouseOut','this.style.background=\'transparent\'');
57
box.setAttribute('onClick','pick(this)');
58
59
box.style.position='absolute';
60
box.style.top=k*100/3+'%';
61
box.style.left=l*100/3+'%';
62
box.style.height=100/3+'%';
63
box.style.width=100/3+'%';
64
65
box.macro=3*i+j;
66
box.micro=3*k+l;
67
box.mark=null;
68
boxes[3*i+j].push(box);
69
70
if(box.macro%2==0){
71
box.style.boxShadow='0 0 5px black inset';
72
}
73
else{
74
box.style.boxShadow='0 0 5px gray inset';
75
}
76
77
BOX.appendChild(box);
78
}
79
}
80
BOXES.push(BOX);
81
board.appendChild(BOX);
82
}
83
}
84
85
/*
86
+---+---+---+
87
| 0 | 1 | 2 |
88
+---+---+---+
89
| 3 | 4 | 5 |
90
+---+---+---+
91
| 6 | 7 | 8 |
92
+---+---+---+
93
*/
94
95
// Check for victories.
96
function check(m){
97
var checkmark=0;
98
if(m[0].mark!=null){
99
if((m[0].mark==m[1].mark && m[1].mark==m[2].mark) || (m[0].mark==m[3].mark && m[3].mark==m[6].mark)){
100
checkmark=1;
101
}
102
}
103
if(m[4].mark!=null){
104
if((m[3].mark==m[4].mark && m[4].mark==m[5].mark) || (m[1].mark==m[4].mark && m[4].mark==m[7].mark) || (m[0].mark==m[4].mark && m[4].mark==m[8].mark) || (m[2].mark==m[4].mark && m[4].mark==m[6].mark)){
105
checkmark=1;
106
}
107
}
108
if(m[8].mark!=null){
109
if((m[6].mark==m[7].mark && m[7].mark==m[8].mark) || (m[2].mark==m[5].mark && m[5].mark==m[8].mark)){
110
checkmark=1;
111
}
112
}
113
114
if(checkmark){
115
marker(m[0].parentNode);
116
}
117
else{
118
if(m[0].mark!=null && m[1].mark!=null && m[2].mark!=null && m[3].mark!=null && m[4].mark!=null && m[5].mark!=null && m[6].mark!=null && m[7].mark!=null && m[8].mark!=null){
119
draw(m[0].parentNode);
120
}
121
}
122
}
123
124
// Place a tied marker.
125
function draw(obj){
126
// Disable the tied box.
127
obj.mark=2;
128
disable(obj);
129
obj.style.pointerEvents='none';
130
}
131
132
// Place a marker.
133
function marker(obj){
134
// Place an O.
135
if(turn==0){
136
var o=document.createElement('div');
137
o.style.boxSizing='border-box';
138
o.style.position='absolute';
139
o.style.top='10%';
140
o.style.left='10%';
141
o.style.height='80%';
142
o.style.width='80%';
143
// Set O border size based on nested box level, because for some reason percentage isn't supported.
144
var inBOXES=0;
145
for(var i=0;i<9;i++){
146
if(obj==BOXES[i]){
147
inBOXES=1;
148
break;
149
}
150
}
151
o.style.border=unit*(obj==board ? 9 : (inBOXES ? 3 : 1))/5+'px solid black';
152
window.addEventListener('resize',function(){ newUnit();o.style.border=unit*(obj==board ? 9 : (inBOXES ? 3 : 1))/5+'px solid black'; });
153
o.style.borderRadius='50%';
154
obj.appendChild(o);
155
}
156
// Place an X.
157
else{
158
var x1=document.createElement('div');
159
x1.style.boxSizing='border-box';
160
x1.style.background='black';
161
x1.style.position='absolute';
162
x1.style.top='10%';
163
x1.style.left='40%';
164
x1.style.height='80%';
165
x1.style.width='20%';
166
x1.style.transform='rotate(45deg)';
167
x1.style.webkitTransform='rotate(45deg)';
168
obj.appendChild(x1);
169
var x2=x1.cloneNode();
170
x2.style.transform='rotate(-45deg)';
171
x2.style.webkitTransform='rotate(-45deg)';
172
obj.appendChild(x2);
173
}
174
// Mark the map and disable the marked box.
175
obj.mark=turn;
176
disable(obj);
177
obj.style.pointerEvents='none';
178
}
179
180
// Do stuff to a box upon click.
181
function pick(b){
182
// Mark box.
183
marker(b);
184
185
// Check victories.
186
check(boxes[b.macro]);
187
check(BOXES);
188
189
// Pass turn to other player.
190
turn>0 ? turn=0 : turn=1;
191
192
// Enable next macro box if it isn't won.
193
if(BOXES[b.micro].mark==null){
194
// Disable all boxes.
195
for(i=0;i<9;i++){
196
for(var j=0;j<9;j++){
197
disable(boxes[i][j]);
198
}
199
}
200
// Enable next macro box.
201
for(i=0;i<9;i++){
202
// Forces won macro boxes to stay disabled.
203
if(board.mark==null){
204
enable(boxes[b.micro][i]);
205
}
206
}
207
}
208
// Enable all macro boxes if it's won.
209
else{
210
for(i=0;i<9;i++){
211
for(var j=0;j<9;j++){
212
// Forces won macro boxes to stay disabled.
213
if(board.mark==null){
214
enable(boxes[i][j]);
215
}
216
}
217
}
218
}
219
// Forces won micro boxes to stay disabled.
220
disable(b);
221
}
222
223
// Disables a box.
224
function disable(b){
225
b.style.background='rgba(0,0,0,0.25)';
226
b.setAttribute('onMouseOver','');
227
b.setAttribute('onMouseOut','');
228
b.setAttribute('onClick','');
229
}
230
231
// Enables a box.
232
function enable(b){
233
// Forces won boxes to stay disabled.
234
if(b.mark==null){
235
b.style.background='transparent';
236
}
237
b.setAttribute('onMouseOver','this.style.background=\'rgba(0,0,0,0.25)\'');
238
b.setAttribute('onMouseOut','this.style.background=\'transparent\'');
239
b.setAttribute('onClick','pick(this)');
240
}
241
242