aboutsummaryrefslogtreecommitdiff
path: root/utils/langton_ant/script.js
blob: b5405a3fadd8ebb7f11e52916e33fcb8e6f5be42 (plain)
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
// This was coded a very long time ago and I put it here for posterity
// Please don't judge me too harshly
// I was young and naive
$(function() {
	let x = 100;
	let y = 100;

	function table_generator() {
		for (let i = 0; i < x; i++) {
			$('#table').append('<tr class="' + i + '"></tr>');
			for (let j = 0; j < y; j++) {
				$('#table tr[class="' + i + '"]').append('<td class="' + j + '"></td>');
			}
		}
	}

	table_generator();
	let case_index = [Math.floor(x/2), Math.floor(y/2)];
	let case_table = $('#table tr[class="' + case_index[0] + '"] td[class="' + case_index[1] + '"]');
	case_table.css('background-color', 'black');
	let direction = 0;

	function case_change() {
		// up    = 0
		// left  = 1
		// bot   = 2
		// right = 3

		switch (direction) {
			case 0:
				case_index[0] = case_index[0] - 1;
				break;
			case 1:
				case_index[1] = case_index[1] - 1;
				break;
			case 2:
				case_index[0] = case_index[0] + 1;
				break;
			case 3:
				case_index[1] = case_index[1] + 1;
				break;
		}
	}

	function update() {
		if (case_table.css('background-color') == 'rgb(0, 0, 0)') {
			if (direction == 3) {
				direction = 0;
			} else {
				direction = direction + 1;
			}
			case_table.css('background-color', 'white');
			case_change();
			case_table = $('#table tr[class="' + case_index[0] + '"] td[class="' + case_index[1] + '"]');
		} else {
			if (direction == 0) {
				direction = 3;
			} else {
				direction = direction - 1;
			}
			case_table.css('background-color', 'black');
			case_change();
			case_table = $('#table tr[class="' + case_index[0] + '"] td[class="' + case_index[1] + '"]');
		}
	}

	setInterval(update, 10);
});