-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout.js
109 lines (88 loc) · 2.71 KB
/
layout.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
var board = $('#board');
var TileSize = 25;
var size = 10
var dev = false;
var cells = [];
function initGame() {
cells = [];
$("#board").height(size * TileSize);
$("#board").width(size * TileSize);
var boardHtml = '<div id="player"></div>';
var n = 0,
k = 0;
for (var i = 0; i < size; i++) {
for (var j = 0; j < size; j++) {
boardHtml += `<div class="cell" style="left:${j * TileSize}px;top: ${i * TileSize}px;height: ${TileSize - 1}px;width:${TileSize - 1}px" id="${j}-${i}">`;
boardHtml += "</div>";
cells.push(`#${j}-${i}`);
n++;
}
}
$(board).html(boardHtml);
initPlayer()
generateMaze();
writeOnConsole(`Initialized ${size}x${size} Game Board`)
writeOnConsole(" ")
initAgent();
$("#board .cell").click(function () {
var pos = getPosFromCellAddr($(this).attr('id'));
var index = pos[0] + pos[1] * size;
console.log(pos, env.rewardArry[index]);
})
}
function drawWeights() {
for (var y = 0; y < size; y++) {
for (var x = 0; x < size; x++) {
var r = 255,
g = 255,
b = 255,
a = 1;
var cell = env.xyToS(x, y);
// get value of cell under agent policy
if (typeof agent.V !== 'undefined')
var vv = agent.V[cell];
else if (typeof agent.Q !== 'undefined') {
var possibleStates = env.allowedActions(cell),
vv = -1;
for (var i = 0, n = possibleStates.length; i < n; i++) {
var qsa = agent.Q[possibleStates[i] * size + cell];
if (i === 0 || qsa > vv) vv = qsa;
}
}
var ms = 100;
if (vv > 0) {
g = 255;
r = 255 - vv * ms;
b = 255 - vv * ms;
a = 0.2;
}
if (vv < 0) {
g = 255 + vv * ms;
r = 255;
b = 255 + vv * ms;
a = 1;
}
var vcol = `rgba(${Math.floor(r)},${Math.floor(g)},${Math.floor(b)}, ${a})`;
if (env.t && env.T[cell] === 1) vcol = "#AAA";
var id = `#${x}-${y}`;
$(id).css({
background: vcol
})
}
}
}
writeOnConsole("Maze Puzzle (c) Nauman Umer");
writeOnConsole("Info: Use Arrow keys to move Player", "rgb(97, 175, 255)");
$("#size").change(() => {
size = parseInt($("#size option:selected").val());
var dims = {
10: 25,
15: 20,
20: 20,
30: 15,
40: 10,
50: 10
}
TileSize = dims[size];
initGame();
})