-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphwalker-map.js
More file actions
214 lines (191 loc) · 5.58 KB
/
graphwalker-map.js
File metadata and controls
214 lines (191 loc) · 5.58 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
204
205
206
207
208
209
210
211
212
213
/*
Graph walker - A simple 2-player graph walking game.
Copyright (C) 2012, 2013 B. Conijn <bcmpinc@users.sourceforge.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
This file contains the various available maps.
*/
var MAPS=[];
var SCALE=1000;
// Constructor: creates a new Map
var map=function(name, fun) {
this.name=name;
this.fun=fun;
MAPS.push(this);
}
// Constructor: Adds a node to the map.
// Let n be a node:
// - n.x,n.y denote the location of the node.
// - n.exit tells what player can exit at this node. (0 if not an exit, default)
// - n.element is the svg element that displays this node.
// - n.used stores whether the node has been visited already.
var node=function(x,y,boundary) {
this.x=x*SCALE;
this.y=y*SCALE;
this.edges=[];
this.id=map.length;
if (boundary) this.boundary=true;
map.push(this);
}
node.prototype.toString=function(){return "node"+this.id;};
// Constructor: Adds an edge to the map.
// Let e be an edge:
// - e.a and e.b are the nodes connected by this edge.
// - e.used tells whether this edge is in use. Such an edge can not be used to make a move.
// - e.element the svg element used to display the edge.
var edge=function(na,nb,used) {
this.a=na
this.b=nb
this.used=used||false;
na.edges.push(this);
nb.edges.push(this);
if (used) na.used=nb.used=true;
}
// Grid generators
var rectangular_grid = function(size) {
var grid={}
var scale = .8/size;
// Create nodes
for (var y=-size;y<=size;y++) {
for (var x=-size;x<=size;x++) {
boundary = (x==-size || x==size || y==-size || y==size);
grid[[x,y]]=new node(x*scale,y*scale, boundary);
}
}
// create exit nodes
grid[[-size,0]].exit=1;
grid[[ size,0]].exit=2;
current.node=grid[[0,0]];
current.player="p"+(1+Math.floor(Math.random()*2));
return grid;
}
var triangular_grid = function(size) {
var grid={}
var scale = 0.9/size;
var up = Math.sqrt(0.75);
// Create nodes
for (var y=-size;y<=size;y++) {
for (var x=-size;x<=size;x++) {
if (x-y>=-size && x-y<=size) {
boundary = (x==-size || x==size || y==-size || y==size || x-y==-size || x-y==size);
grid[[x,y]]=new node((x+y)*.5*scale,(x-y)*up*scale, boundary);
}
}
}
// create exit nodes
grid[[-size,-size]].exit=1;
grid[[ size, size]].exit=2;
current.node=grid[[0,0]];
current.player="p"+(1+Math.floor(Math.random()*2));
return grid;
}
var hexagonal_grid = function(size) {
var grid={}
var scale = 0.9/size;
var up = Math.sqrt(0.75);
// Create nodes
for (var y=-size;y<=size;y++) {
for (var x=-size;x<=size;x++) {
if (x-y>=-size && x-y<=size && (x+y+300)%3>0) {
boundary = (x==-size || x==size || y==-size || y==size || x-y==-size || x-y==size);
grid[[x,y]]=new node((x+y)*.5*scale,(x-y)*up*scale, boundary);
}
}
}
// create exit nodes
if ((size+size)%3==0) size--;
grid[[-size,-size]].exit=1;
grid[[ size, size]].exit=2;
current.player="p"+(1+Math.floor(Math.random()*2));
return grid;
}
// Connects a grid
// dx and dy are equal length arrays with offsets that need to be connected.
// bounds (optional) is another equal length array, that tells whether the connection can be a boundary edge.
var connect_nodes=function(grid, dx, dy, bounds) {
// create edges
N = dx.length;
if (!bounds) bounds = [];
for (var p1 in grid) {
g1 = grid[p1];
p1 = p1.split(",");
for (var i=0; i<N; i++) {
p2 = [p1[0]-dx[i],p1[1]-dy[i]];
g2 = grid[p2];
if(g2) {
if (g1.boundary && g2.exit && bounds[i]) continue;
if (g2.boundary && g1.exit && bounds[i]) continue;
new edge(g1,g2, g1.boundary && g2.boundary && bounds[i]);
}
}
}
}
new map("Original", function(){
var grid = rectangular_grid(5);
var dx=[1,1,1,0];
var dy=[-1,0,1,1];
var bounds=[0,1,0,1];
connect_nodes(grid, dx, dy, bounds);
});
new map("Original (handicap p2)", function(){
var grid = rectangular_grid(5);
var dx=[1,1,1,0];
var dy=[-1,0,1,1];
var bounds=[0,1,0,1];
current.node=grid[[-4,0]];
current.player="p1";
connect_nodes(grid, dx, dy, bounds);
});
new map("Knight", function(){
var grid = rectangular_grid(5);
var dx=[-2,2,-1,1];
var dy=[1,1,2,2];
connect_nodes(grid, dx, dy);
});
new map("Triangles", function(){
var grid = triangular_grid(6);
var dx=[0,1,1];
var dy=[1,0,1];
var bounds=[1,1,1];
connect_nodes(grid, dx, dy, bounds);
});
new map("Triangles dense", function(){
var grid = triangular_grid(5);
var dx=[0,1,1,2,1,1];
var dy=[1,0,1,1,2,-1];
var bounds=[1,1,1,0,0,0];
connect_nodes(grid, dx, dy, bounds);
});
new map("Hexagons", function(){
var grid = hexagonal_grid(7);
var dx=[0,1,1,0,2,2];
var dy=[1,0,1,2,0,2];
var bounds=[1,1,1,1,1,1];
connect_nodes(grid, dx, dy, bounds);
c = new node(0, -Math.sqrt(0.75)*0.666667*0.9/7);
new edge(c,grid[[-1,-1]])
new edge(c,grid[[ 1, 1]])
current.node=c;
});
new map("Hexagrams", function(){
var grid = hexagonal_grid(7);
var dx=[0,1,1,2,1,1];
var dy=[1,0,1,1,2,-1];
var bounds=[1,1,1,0,0,0];
connect_nodes(grid, dx, dy, bounds);
c = new node(0, 0);
new edge(c,grid[[-1,-1]])
new edge(c,grid[[ 1, 1]])
current.node=c;
});
// kate: space-indent off; indent-width 2;