-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.js
208 lines (175 loc) · 4.75 KB
/
path.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var MovesCode=" size= 5000; \n steps=5000; \n function knightMoves(x,y){ \n \t upright=[x+1,y+2]; \n \t upleft=[x-1,y+2]; \n \t rightup=[x+2,y+1]; \n \t rightdown=[x+2,y-1]; \n \t downright=[x+1,y-2]; \n \t downleft=[x-1,y-2]; \n \t leftdown=[x-2,y-1]; \n \t leftup=[x-2,y+1]; \n \t return([upright,upleft,rightup,rightdown,downright,downleft,leftdown,leftup]); }";
var size=100;
var steps=100;
var C;
var x;
var y;
var value;
//FIELD Generation
var UlamSprial=[]
function xCord(n){
if(n==1){
return(0);
}
else{
return(Math.round(xCord(n-1)+Math.sin((Math.floor(Math.sqrt(4*(n-2)+1))%4)*(Math.PI/2))));
}
}
function yCord(n){
if(n==1){
return(0);
}
else{
return(Math.round(yCord(n-1)+Math.cos((Math.floor(Math.sqrt(4*(n-2)+1))%4)*(Math.PI/2))));
}
}
function CordtoValue(x,y,Cords){ //index is value-1
// /console.log("ran1");
for(u=0;u<Cords.length;u++){
//console.log("ran");
if(Cords[u][0]==x && Cords[u][1]==y){
return(Cords[u][2]-1);
}
}
}
function Sprial(xValue,yValue,MaxNumber,offset){
Cords=[]
for(i=1;i<=MaxNumber;i++){
Cords.push([xValue(i),yValue(i),i+offset,false]);
}
return(Cords);
}
function knightMoves1(x,y){
upright=[x+1,y+1];
rightup=[x+2,y+2];
more1=[x+3,y+3];
upleft=[x-1,y+1];
leftup=[x-2,y+2];
more2=[x-3,y+3];
downright=[x+1,y-1];
rightdown=[x+2,y-2];
more3=[x+3,y-3];
downleft=[x-1,y-1];
leftdown=[x-2,y-2];
more4=[x-3,y-3];
return([upright,upleft,rightup,rightdown,downright,downleft,leftdown,leftup,more1,more2,more3,more4]);
}
function knightMoves2(x,y){
var dagsteps=4;
var moves=[];
for(i=0;i<dagsteps;i++){
moves.push([x+i,y+i]);
}
for(i=0;i<dagsteps;i++){
moves.push([x-i,y-i]);
}
for(i=0;i<dagsteps;i++){
moves.push([x-i,y+i]);
}
for(i=0;i<dagsteps;i++){
moves.push([x+i,y-i]);
}
return(moves);
}
// BASE FUNCTION FOR 2048
function knightMoves(x,y){
upright=[x+1,y+2];
upleft=[x-1,y+2];
rightup=[x+2,y+1];
rightdown=[x+2,y-1];
downright=[x+1,y-2];
downleft=[x-1,y-2];
leftdown=[x-2,y-1];
leftup=[x-2,y+1];
return([upright,upleft,rightup,rightdown,downright,downleft,leftdown,leftup]);
}
function knightLowestandTrue(x,y,cords,moves){
var lowestnumber=100000000000000;
var lowestmove=[];
for(q=0;q<moves.length;q++){ // i index was conflicting with something
//console.log("testing");
index=CordtoValue(moves[q][0],moves[q][1],cords);
// console.log("index")
// console.log(index);
// console.log("cordsIndex");
// console.log(cords[index][3]);
if(index+1<lowestnumber && cords[index][3]==false){
//console.log("q "+String(q));
//console.log([cords[index][0],cords[index][1]]);
lowestnumber= index;
lowestmove=[cords[index][0],cords[index][1]];
}
}
return(lowestmove);
}
function pathAlterations(){
}
function run(size,steps){
eval(MovesCode);
var path=[]
var pathx=[]
var pathy=[]
C=Sprial(xCord,yCord,size,0);
C[0]=[0,0,1,true];
var L=[0,0];
var NewL;
var I;
var Steps=0;
pathAlterations();
while(Steps<steps){
NewL=knightLowestandTrue(L[0],L[1],C,knightMoves(L[0],L[1]));
/*if(NewL=false){
break;
}*/
I=CordtoValue(NewL[0],NewL[1],C);
//console.log(I+1);
path.push(I+1);
pathx.push(L[0]);
pathy.push(L[1]);
//console.log(L);
try{
C[I][3]=true;}
catch(err){
break;
}
L=NewL;
Steps++;
}
return([path,pathx,pathy]);
}
function linkGenerator(){
Code=document.getElementById('MovesCode').value;
linkHash=Code.replace(/(\r\n|\n|\r)/gm,""); //make it one line
linkHash=linkHash.replace(/ /g, "%20") //get rid of spaces
linkHash=linkHash.replace(/ \t /g, "%20") //get rid of tabs
//Remove anything behind a "#" incase the link was already provided
s=window.location.href
var n = s.indexOf('#');
s = s.substring(0, n != -1 ? n : s.length);
link= s+"#"+linkHash
return(link);
}
function copyToClipboard(text) {
var dummy = document.createElement("textarea");
// to avoid breaking orgain page when copying more words
// cant copy when adding below this code
// dummy.style.display = 'none'
document.body.appendChild(dummy);
//Be careful if you use texarea. setAttribute('value', value), which works with "input" does not work with "textarea". – Eduard
dummy.value = text;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
}
function linkCopy(){
var copyText = linkGenerator();
copyToClipboard(copyText);
}
/*var trace1 = {
x: x,
y: y,
mode: 'markers'
};
var data = [ trace1];
var layout = {};
Plotly.newPlot('myDiv', data, layout, {showSendToCloud: true});*/