summaryrefslogtreecommitdiff
path: root/tools/engage.js
blob: bd784bdf4b715615f9a1c3b98a33bbafff50658d (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
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
"use strict"

function array_insert(array, index, item) {
	for (let i = array.length; i > index; --i)
		array[i] = array[i - 1]
	array[index] = item
}

function set_add(set, item) {
	let a = 0
	let b = set.length - 1
	while (a <= b) {
		let m = (a + b) >> 1
		let x = set[m]
		if (item < x)
			b = m - 1
		else if (item > x)
			a = m + 1
		else
			return
	}
	array_insert(set, a, item)
}

const A1 = 0, A2 = 1, A3 = 2
const D1 = 3, D2 = 4, D3 = 5

const NAMES = [
	"A1", "A2", "A3",
	"D1", "D2", "D3",
]

function show_array(array) {
	for (let row = 0; row < 6; row += 3) {
		let s = []
		for (let col = 0; col < 3; ++col) {
			if (array[row+col])
				s.push(NAMES[row+col].padEnd(3, ' '))
			else
				s.push("-- ")
		}
		console.log(s.join(" "))
	}
	console.log("")
}

function verify(array, out) {
	for (let i = 0; i < 6; ++i) {
		if (array[i]) {
			let okay = false
			for (let eng of out)
				if (eng.includes(i))
					okay = true
			if (!okay)
				return false
		}
	}
	return true
}

function make_engagement(array, choice) {
	let out = []
	let a, b

	function engage(a, b) {
		for (let eng of out) {
			if (eng.includes(a)) {
				set_add(eng, b)
				return
			}
			if (eng.includes(b)) {
				set_add(eng, a)
				return
			}
		}
		out.push([a,b])
	}

	// A1 vs D1 etc
	if (array[A1] && array[D1]) engage(A1, D1)
	if (array[A2] && array[D2]) engage(A2, D2)
	if (array[A3] && array[D3]) engage(A3, D3)

	// A1 vs D2/D3 etc
	if (array[A1] && !array[D1] && array[D2]) engage(A1, D2)
	if (array[A1] && !array[D1] && !array[D2] && array[D3]) engage(A1, D3)

	if (array[A3] && !array[D3] && array[D2]) engage(A3, D2)
	if (array[A3] && !array[D3] && !array[D2] && array[D1]) engage(A3, D1)

	if (array[D1] && !array[A1] && array[A2]) engage(D1, A2)
	if (array[D1] && !array[A1] && !array[A2] && array[A3]) engage(D1, A3)

	if (array[D3] && !array[A3] && array[A2]) engage(D3, A2)
	if (array[D3] && !array[A3] && !array[A2] && array[A1]) engage(D3, A1)

	// A2 vs D1/D3
	if (array[A2] && !array[D2]) {
		if (array[D1] && !array[D3]) engage(A2, D1)
		if (!array[D1] && array[D3]) engage(A2, D3)
		if (choice)
			if (array[D1] && array[D3]) engage(A2, choice === 1 ? D1 : D3)
	}

	// D2 vs A1/D3
	if (array[D2] && !array[A2]) {
		if (array[A1] && !array[A3]) engage(D2, A1)
		if (!array[A1] && array[A3]) engage(D2, A3)
		if (choice)
			if (array[A1] && array[A3]) engage(D2, choice === 1 ? A1 : A3)
	}

	if (verify(array, out)) {
		console.log(out.map(eng => eng.map(x=>NAMES[x]).join("+")).join(" / "))
		return out
	}
	return null
}

var ENGAGEMENTS = []
var CHOICE = []

function pack_battle_array(filled) {
	let bits = 0
	for (let p = 0; p < 6; ++p)
		if (filled[p])
			bits |= (1 << p)
	return bits
}

function run(array) {
	console.log("<tr>")
	console.log("<td>")
	show_array(array)
	console.log("<td>")
	let eng = make_engagement(array, 0)
	let bits = pack_battle_array(array)
	ENGAGEMENTS[bits] = eng
	if (!eng) {
		let eng_a = make_engagement(array, 1)
		console.log("or")
		let eng_b = make_engagement(array, 2)
		CHOICE.push([ bits, eng_a, eng_b ])
	}
}

function runall() {
	for (let x = 0; x < 64; ++x) {
		if ((x & 7) && (x & 56))
			run([ (x>>5)&1, (x>>4)&1, (x>>3)&1, (x>>2)&1, (x>>1)&1, (x>>0)&1 ])
	}
}

console.log("<!DOCTYPE html>")
console.log("<style>td{white-space:pre;font-family:monospace;padding:3em;border:1px solid black}</style>")
console.log("<table>")
runall()

//run([1,1,0, 1,0,1])

//run([1,1,1, 1,1,0])
//run([1,1,1, 0,1,0])
//run([1,0,1, 0,1,0])

//run([1,0,1, 0,1,0])
//run([0,1,1, 1,0,0])
//run([1,0,0, 0,1,1])
//run([1,1,0, 1,0,1])
//run([1,1,1, 1,0,1])

console.log("</table>")

console.log("<pre>")
for (let i = 0; i < ENGAGEMENTS.length; ++i) if (!ENGAGEMENTS[i]) ENGAGEMENTS[i] = []
console.log("const ENGAGEMENTS = " + JSON.stringify(ENGAGEMENTS))
for (let [bits,a,b] of CHOICE)
	console.log("const ENGAGEMENTS_" + bits + " = " + JSON.stringify([a,b]))