summaryrefslogtreecommitdiff
path: root/rules.js
blob: e6461e1d1ef2e0c021568265f7c9cdb0485516dd (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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"use strict"

const RED = "Red"
const BLUE = "Blue"

const states = {}

var game, view

exports.scenarios = [ "Standard" ]

exports.roles = [ RED, BLUE ]

const TOKEN_DRAGON = 0
const TOKEN_RED_1 = 1
const TOKEN_RED_2 = 2
const TOKEN_BLUE_1 = 3
const TOKEN_BLUE_2 = 4

const S_DARKNESS = 0
const S_GOLD = 1
const S_BLUE = 2
const S_WHITE = 3
const S_RED = 4
const S_PURPLE = 5
const S_DRAGON = 6
const S_OFF_BOARD = 7

const TILE_NONE = 0
const FIRST_TILE = 1
const TILE_BLUE = 1
const TILE_RED = 13
const TILE_GOLD = 25
const TILE_WHITE = 37
const TILE_GREEN = 49
const LAST_TILE = 55

function gen_action(action, argument) {
	if (!(action in view.actions))
		view.actions[action] = []
	view.actions[action].push(argument)
}

function gen_action_token(token) {
	gen_action("token", token)
}

function gen_action_tile(tile) {
	gen_action("tile", tile)
}

function gen_action_space(space) {
	gen_action("space", space)
}

exports.action = function (state, player, action, arg) {
	game = state
	let S = states[game.state]
	if (action in S)
		S[action](arg, player)
	else
		throw new Error("Invalid action: " + action)
	return game
}

exports.resign = function (state, player) {
	game = state
	if (game.state !== 'game_over') {
		if (player === RED)
			goto_game_over(BLUE, "Red resigned.")
		if (player === BLUE)
			goto_game_over(RED, "Blue resigned.")
	}
	return game
}

exports.view = function(state, player) {
	game = state

	view = {
		log: game.log,
		prompt: null,
		tokens: game.tokens,
		squares: game.squares,
		red_score: game.red_score,
		blue_score: game.blue_score,
		red_court: game.red_court,
		blue_court: game.blue_court,

		selected_token: -1,
		selected_tile: 0,
	}

	if (player === RED)
		view.hand = game.red_hand

	if (player === BLUE)
		view.hand = game.blue_hand

	if (game.state === "game_over") {
		view.prompt = game.victory
	} else if (player !== game.active) {
		let inactive = states[game.state].inactive || game.state
		view.prompt = `Waiting for ${game.active} \u2014 ${inactive}...`
	} else {
		view.actions = {}
		states[game.state].prompt()
		if (game.undo && game.undo.length > 0)
			view.actions.undo = 1
		else
			view.actions.undo = 0
	}

	return view;
}

exports.setup = function (seed, scenario, options) {
	game = {
		seed: seed,
		state: null,
		log: [],
		undo: [],

		red_score: 0,
		blue_score: 0,
		tokens: [ S_DRAGON, S_OFF_BOARD, S_OFF_BOARD, S_OFF_BOARD, S_OFF_BOARD ],
		squares: [ 0, 0, 0, 0, 0, 0 ],
		darkness: [],
		red_court: [],
		blue_court: [],
		red_hand: [],
		blue_hand: [],
	}

	for (let i = FIRST_TILE; i < TILE_GREEN; ++i)
		game.darkness.push(i)

	shuffle(game.darkness)

	game.red_hand.push(game.darkness.pop())
	game.red_hand.push(game.darkness.pop())

	game.blue_hand.push(game.darkness.pop())
	game.blue_hand.push(game.darkness.pop())

	for (let i = TILE_GREEN; i < TILE_GREEN + 6; ++i)
		game.darkness.push(i)

	shuffle(game.darkness)

	for (let i = 0; i < 6; ++i)
		game.squares[i] = game.darkness.pop()

	if (random(2) === 0)
		game.active = RED
	else
		game.active = BLUE

	game.state = "move_token"

	return game
}

// === FLOW OF PLAY ===

states.move_token = {
	prompt() {
		view.prompt = "Move one of your tokens to an oval space."
		if (game.active === RED) {
			if (game.tokens[TOKEN_RED_1] === S_OFF_BOARD) {
				gen_action_token(TOKEN_RED_1)
			} else if (game.tokens[TOKEN_RED_2] === S_OFF_BOARD) {
				gen_action_token(TOKEN_RED_2)
			} else {
				gen_action_token(TOKEN_RED_1)
				gen_action_token(TOKEN_RED_2)
			}
		} else {
			if (game.tokens[TOKEN_BLUE_1] === S_OFF_BOARD) {
				gen_action_token(TOKEN_BLUE_1)
			} else if (game.tokens[TOKEN_BLUE_2] === S_OFF_BOARD) {
				gen_action_token(TOKEN_BLUE_2)
			} else {
				gen_action_token(TOKEN_BLUE_1)
				gen_action_token(TOKEN_BLUE_2)
			}
		}
	},
	token(token) {
		push_undo()
		game.selected_token = token
		game.state = "move_token_to"
	},
}

function is_oval_space_empty(s) {
	for (let i = 0; i < 5; ++i)
		if (game.tokens[i] === s)
			return false
	return true
}

states.move_token_to = {
	prompt() {
		view.prompt = "Move your token to an oval space."
		view.selected_token = game.selected_token
		for (let i = 0; i < 7; ++i)
			if (is_oval_space_empty(i))
				gen_action_space(i)
	},
	space(space) {
		game.tokens[game.selected_token] = space
		// take action!
	}
}

// === THE ACTIONS: DRAGON ===
// === THE ACTIONS: SECRECY ===
// === THE ACTIONS: CLOTH OF GOLD ===
// === THE ACTIONS: BANQUETS AND FEASTS  ===
// === THE ACTIONS: GODLINESS AND PIETY ===
// === THE ACTIONS: TOURNAMENTS ===
// === THE ACTIONS: COLLECTIONS ===
// === END OF THE CONTEST ===

// === COMMON LIBRARY ===

function clear_undo() {
	if (game.undo.length > 0)
		game.undo = []
}

function push_undo() {
	let copy = {}
	for (let k in game) {
		let v = game[k]
		if (k === "undo")
			continue
		else if (k === "log")
			v = v.length
		else if (typeof v === "object" && v !== null)
			v = object_copy(v)
		copy[k] = v
	}
	game.undo.push(copy)
}

function pop_undo() {
	let save_log = game.log
	let save_undo = game.undo
	game = save_undo.pop()
	save_log.length = game.log
	game.log = save_log
	game.undo = save_undo
}

function random(range) {
	// An MLCG using integer arithmetic with doubles.
	// https://www.ams.org/journals/mcom/1999-68-225/S0025-5718-99-00996-5/S0025-5718-99-00996-5.pdf
	// m = 2**35 − 31
	return (game.seed = game.seed * 200105 % 34359738337) % range
}

function shuffle(list) {
	// Fisher-Yates shuffle
	for (let i = list.length - 1; i > 0; --i) {
		let j = random(i + 1)
		let tmp = list[j]
		list[j] = list[i]
		list[i] = tmp
	}
}

// Fast deep copy for objects without cycles
function object_copy(original) {
	if (Array.isArray(original)) {
		let n = original.length
		let copy = new Array(n)
		for (let i = 0; i < n; ++i) {
			let v = original[i]
			if (typeof v === "object" && v !== null)
				copy[i] = object_copy(v)
			else
				copy[i] = v
		}
		return copy
	} else {
		let copy = {}
		for (let i in original) {
			let v = original[i]
			if (typeof v === "object" && v !== null)
				copy[i] = object_copy(v)
			else
				copy[i] = v
		}
		return copy
	}
}

// Array remove and insert (faster than splice)

function array_remove(array, index) {
	let n = array.length
	for (let i = index + 1; i < n; ++i)
		array[i - 1] = array[i]
	array.length = n - 1
}

function array_remove_item(array, item) {
	let n = array.length
	for (let i = 0; i < n; ++i)
		if (array[i] === item)
			return array_remove(array, i)
}

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