| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | <!DOCTYPE html> | 
					
						
							|  |  |  | <html> | 
					
						
							|  |  |  | <style> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .snake.field { | 
					
						
							|  |  |  | 	width: 500px; | 
					
						
							|  |  |  | 	height: 500px; | 
					
						
							|  |  |  | 	border: solid 1px silver; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </style> | 
					
						
							|  |  |  | <script> | 
					
						
							| 
									
										
										
										
											2017-04-07 01:26:11 +03:00
										 |  |  | //--------------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 02:32:35 +03:00
										 |  |  | // XXX automate clearing of handlers... | 
					
						
							|  |  |  | function makeEvent(handler_attr){ | 
					
						
							|  |  |  | 	return function(func){ | 
					
						
							|  |  |  | 		if(func instanceof Function){ | 
					
						
							|  |  |  | 			var handlers = this[handler_attr] = this[handler_attr] || [] | 
					
						
							|  |  |  | 			handlers.push(func) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			var that = this | 
					
						
							|  |  |  | 			var args = [].slice.call(arguments) | 
					
						
							|  |  |  | 			this[handler_attr] | 
					
						
							|  |  |  | 				&& this[handler_attr] | 
					
						
							|  |  |  | 					.forEach(function(handler){ handler.apply(that, args) }) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return this | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 19:06:13 +03:00
										 |  |  | // XXX normalize x/y everywhere we input coordinates... | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | var Snake = { | 
					
						
							|  |  |  | 	config: { | 
					
						
							|  |  |  | 		apple_color: 'red', | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 		wall_color: 'gray', | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 		interval: 200, | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_field: null, | 
					
						
							|  |  |  | 	_cells: null, | 
					
						
							| 
									
										
										
										
											2017-04-05 05:19:55 +03:00
										 |  |  | 	players: null, | 
					
						
							|  |  |  | 	field_size: null, | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 01:21:32 +03:00
										 |  |  | 	// utils... | 
					
						
							|  |  |  | 	random_point: function(){ | 
					
						
							|  |  |  | 		var cells = this._cells | 
					
						
							|  |  |  | 		var l = cells.length | 
					
						
							|  |  |  | 		var w = this.field_size.width | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		do { | 
					
						
							|  |  |  | 			var i = Math.floor(Math.random() * l) | 
					
						
							|  |  |  | 		} while(cells[i].style.backgroundColor != '') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return { | 
					
						
							|  |  |  | 			x: i%w, | 
					
						
							|  |  |  | 			y: Math.floor(i/w), | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 02:11:41 +03:00
										 |  |  | 	// XXX BUG: going down into bottom left corner breaks the snake... | 
					
						
							|  |  |  | 	//		...check other corner cases! | 
					
						
							|  |  |  | 	//			...looks like the only corner case... | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 	_tick: function(){ | 
					
						
							|  |  |  | 		var that = this | 
					
						
							|  |  |  | 		var l = this._cells.length | 
					
						
							|  |  |  | 		var w = this.field_size.width | 
					
						
							|  |  |  | 		var h = this.field_size.height | 
					
						
							|  |  |  | 		var tick = this.__tick = (this.__tick + 1 || 0) | 
					
						
							|  |  |  | 		var directions = 'neswn' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this._cells.forEach(function(cell, i){ | 
					
						
							|  |  |  | 			var color = cell.style.backgroundColor | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// skip cells we touched... | 
					
						
							|  |  |  | 			if(cell.tick == tick){ | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// snake... | 
					
						
							|  |  |  | 			if(cell.age != null){ | 
					
						
							|  |  |  | 				// handle cell age... | 
					
						
							|  |  |  | 				if(cell.age == 0){ | 
					
						
							|  |  |  | 					delete cell.age | 
					
						
							|  |  |  | 					cell.style.backgroundColor = '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					cell.age -= 1 | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// head... | 
					
						
							|  |  |  | 				var direction = cell.direction | 
					
						
							|  |  |  | 				var next =  | 
					
						
							| 
									
										
										
										
											2017-04-07 13:58:44 +03:00
										 |  |  | 					direction == 'n' ? (i < w ? l - w + i : i - w) | 
					
						
							|  |  |  | 					// XXX BUG: this returns 256 when at x = 0... | 
					
						
							|  |  |  | 					: direction == 's' ? (i > (l-w) ? i - (l-w) : i + w) | 
					
						
							|  |  |  | 					: direction == 'e' ? ((i+1)%w == 0 ? i - (w-1) : i + 1) | 
					
						
							|  |  |  | 					: direction == 'w' ? (i%w == 0 ? i + (w-1) : i - 1) | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 					: null  | 
					
						
							|  |  |  | 				if(next != null){ | 
					
						
							|  |  |  | 					next = that._cells[next] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// turn... | 
					
						
							|  |  |  | 					var turn = that.players[color] || '' | 
					
						
							|  |  |  | 					if(turn != ''){ | 
					
						
							|  |  |  | 						var j = turn == 'left' ? directions.indexOf(direction) - 1 | 
					
						
							|  |  |  | 							: directions.indexOf(direction) + 1 | 
					
						
							|  |  |  | 						j = j < 0 ? 3 : j | 
					
						
							|  |  |  | 						direction = directions[j] | 
					
						
							|  |  |  | 						that.players[color] = '' | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					var age = cell.age | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 					var move = false | 
					
						
							| 
									
										
										
										
											2017-04-07 13:58:44 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 					// special case: other snake's head -- kill both... | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 					if(next.direction){ | 
					
						
							| 
									
										
										
										
											2017-04-06 02:32:35 +03:00
										 |  |  | 						var other = next.style.backgroundColor | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 						next.style.backgroundColor = '' | 
					
						
							|  |  |  | 						// NOTE: we are not deleteing .direction here as  | 
					
						
							|  |  |  | 						//		we can have upto 4 snakes colliding... | 
					
						
							| 
									
										
										
										
											2017-04-04 02:11:41 +03:00
										 |  |  | 						next.direction = '' | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 						delete next.age | 
					
						
							| 
									
										
										
										
											2017-04-06 02:32:35 +03:00
										 |  |  | 						that.snakeKilled(other) | 
					
						
							|  |  |  | 						that.snakeKilled(color) | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 					// apples... | 
					
						
							|  |  |  | 					} else if(next.style.backgroundColor == that.config.apple_color){ | 
					
						
							|  |  |  | 						age += 1 | 
					
						
							|  |  |  | 						move = true | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | 						that.appleEaten() | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 					// empty... | 
					
						
							|  |  |  | 					// NOTE: anything but an apple will kill the snake... | 
					
						
							|  |  |  | 					} else if(next.style.backgroundColor == ''){ | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 						move = true | 
					
						
							| 
									
										
										
										
											2017-04-06 02:32:35 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					// kill... | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						that.snakeKilled(color) | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 02:32:35 +03:00
										 |  |  | 					// do the move... | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 					if(move){ | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 						next.tick = tick | 
					
						
							|  |  |  | 						next.style.backgroundColor = color | 
					
						
							|  |  |  | 						next.age = age + 1 | 
					
						
							|  |  |  | 						next.direction = direction | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					delete cell.direction | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			cell.tick = tick | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setup: function(field){ | 
					
						
							|  |  |  | 		field = field || this._field | 
					
						
							|  |  |  | 		field = this._field = typeof(field) == typeof('str') ? document.querySelector(field) | 
					
						
							|  |  |  | 			: field | 
					
						
							|  |  |  | 		this._cells = [].slice.call(field.querySelectorAll('td')) | 
					
						
							|  |  |  | 		this.field_size = { | 
					
						
							|  |  |  | 			width: field.querySelector('tr').querySelectorAll('td').length, | 
					
						
							|  |  |  | 			height: field.querySelectorAll('tr').length, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		this.players = {} | 
					
						
							|  |  |  | 		return this | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | 	clear: function(){ | 
					
						
							| 
									
										
										
										
											2017-04-07 13:58:44 +03:00
										 |  |  | 		// this resets the cell object attrs... | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | 		this.setup() | 
					
						
							| 
									
										
										
										
											2017-04-07 13:58:44 +03:00
										 |  |  | 		// reset the actual cells... | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | 		this._cells.forEach(function(c){ c.style.backgroundColor = '' })  | 
					
						
							|  |  |  | 		this.players = {} | 
					
						
							|  |  |  | 		delete this.__appleEatenHandlers | 
					
						
							| 
									
										
										
										
											2017-04-06 02:32:35 +03:00
										 |  |  | 		delete this.__killHandlers | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | 		return this | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 	// constructors... | 
					
						
							| 
									
										
										
										
											2017-04-05 19:06:13 +03:00
										 |  |  | 	// XXX normalize input x/y... | 
					
						
							| 
									
										
										
										
											2017-04-06 01:21:32 +03:00
										 |  |  | 	snake: function(color, point, direction, age){ | 
					
						
							|  |  |  | 		point = point || this.random_point() | 
					
						
							|  |  |  | 		var head = this._cells[point.x + point.y * this.field_size.width] | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 		head.style.backgroundColor = color | 
					
						
							|  |  |  | 		head.direction = direction | 
					
						
							|  |  |  | 		head.age = (age || 5) - 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this.players[color] = '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return this | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2017-04-06 01:21:32 +03:00
										 |  |  | 	apple: function(point){ | 
					
						
							|  |  |  | 		point = point || this.random_point() | 
					
						
							|  |  |  | 		this._cells[point.x + point.y * this.field_size.width] | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 			.style.backgroundColor = this.config.apple_color | 
					
						
							|  |  |  | 		return this | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2017-04-06 01:21:32 +03:00
										 |  |  | 	wall: function(point, direction, length){ | 
					
						
							|  |  |  | 		point = point || this.random_point() | 
					
						
							|  |  |  | 		var x = point.x | 
					
						
							|  |  |  | 		var y = point.y | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 		length = length || 1 | 
					
						
							| 
									
										
										
										
											2017-04-08 02:32:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 		while(length > 0){ | 
					
						
							|  |  |  | 			this._cells[x + y * this.field_size.width] | 
					
						
							|  |  |  | 				.style.backgroundColor = this.config.wall_color | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			x += direction == 'e' ? 1 | 
					
						
							|  |  |  | 				: direction == 'w' ? -1 | 
					
						
							|  |  |  | 				: 0 | 
					
						
							|  |  |  | 			x = x < 0 ? this.field_size.width + x | 
					
						
							|  |  |  | 				: x % this.field_size.width | 
					
						
							|  |  |  | 			y += direction == 'n' ? -1 | 
					
						
							|  |  |  | 				: direction == 's' ? 1 | 
					
						
							|  |  |  | 				: 0 | 
					
						
							|  |  |  | 			y = y < 0 ? this.field_size.height + y | 
					
						
							|  |  |  | 				: y % this.field_size.height | 
					
						
							|  |  |  | 			length -= 1 | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-08 02:32:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 05:13:22 +03:00
										 |  |  | 		return this | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | 	// events... | 
					
						
							| 
									
										
										
										
											2017-04-06 02:32:35 +03:00
										 |  |  | 	appleEaten: makeEvent('__appleEatenHandlers'), | 
					
						
							|  |  |  | 	snakeKilled: makeEvent('__killHandlers'), | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 	// actions... | 
					
						
							| 
									
										
										
										
											2017-04-06 01:21:32 +03:00
										 |  |  | 	start: function(t){ | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 		this.__timer = this.__timer  | 
					
						
							| 
									
										
										
										
											2017-04-06 01:21:32 +03:00
										 |  |  | 			|| setInterval(this._tick.bind(this), t || this.config.interval || 200) | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 		return this | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	stop: function(){ | 
					
						
							|  |  |  | 		clearInterval(this.__timer) | 
					
						
							|  |  |  | 		delete this.__timer | 
					
						
							|  |  |  | 		delete this.__tick | 
					
						
							|  |  |  | 		return this | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2017-04-07 13:58:44 +03:00
										 |  |  | 	pause: function(){ | 
					
						
							|  |  |  | 		return this.__timer ? this.stop() : this.start() }, | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 	left: function(color){  | 
					
						
							|  |  |  | 		this.players[color || Object.keys(this.players)[0]] = 'left'  | 
					
						
							|  |  |  | 		return this | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	right: function(color){ | 
					
						
							|  |  |  | 		this.players[color || Object.keys(this.players)[0]] = 'right'  | 
					
						
							|  |  |  | 		return this | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 02:32:35 +03:00
										 |  |  | 	// levels... | 
					
						
							| 
									
										
										
										
											2017-04-06 01:21:32 +03:00
										 |  |  | 	basicLevel: function(){ | 
					
						
							|  |  |  | 		return this | 
					
						
							|  |  |  | 			.wall({x:3, y:10}, 's', 11) | 
					
						
							|  |  |  | 			.wall({x:3, y:5}, 'e', 5) | 
					
						
							|  |  |  | 			.wall({x:3+5, y:5}, 's', 5) | 
					
						
							|  |  |  | 			.wall({x:3+5, y:5+4}, 'e', 12) }, | 
					
						
							|  |  |  | 	randomLevel: function(){ | 
					
						
							|  |  |  | 		return this | 
					
						
							|  |  |  | 			.wall(null, 's', 11) | 
					
						
							|  |  |  | 			.wall(null, 's', 11) | 
					
						
							|  |  |  | 			.wall(null, 'e', 11) | 
					
						
							|  |  |  | 			.wall(null, 'e', 11) }, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 18:53:07 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 01:26:11 +03:00
										 |  |  | //--------------------------------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | var HANDLER_SET = false | 
					
						
							|  |  |  | var KEY_CONFIG = { | 
					
						
							| 
									
										
										
										
											2017-04-07 13:58:44 +03:00
										 |  |  | 	' ': ['pause'], | 
					
						
							| 
									
										
										
										
											2017-04-08 02:32:08 +03:00
										 |  |  | 	ArrowLeft: ['left'], | 
					
						
							|  |  |  | 	ArrowRight: ['right'],  | 
					
						
							|  |  |  | 	// IE compatibility... | 
					
						
							|  |  |  | 	Left: ['left'], | 
					
						
							|  |  |  | 	Right: ['right'], | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | } | 
					
						
							|  |  |  | function kbHandler(event){ | 
					
						
							| 
									
										
										
										
											2017-04-08 02:32:08 +03:00
										 |  |  | 	console.log('KEY:', event.key) | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | 	var action = KEY_CONFIG[event.key] | 
					
						
							|  |  |  | 	action  | 
					
						
							| 
									
										
										
										
											2017-04-07 13:58:44 +03:00
										 |  |  | 		&& action[0] in Snake  | 
					
						
							| 
									
										
										
										
											2017-04-07 01:26:11 +03:00
										 |  |  | 		&& Snake[action[0]].apply(Snake, action.slice(1)) | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 01:26:11 +03:00
										 |  |  | //--------------------------------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 18:53:07 +03:00
										 |  |  | function setup(){ | 
					
						
							| 
									
										
										
										
											2017-04-05 18:58:22 +03:00
										 |  |  | 	Snake | 
					
						
							|  |  |  | 		.setup('.snake') | 
					
						
							| 
									
										
										
										
											2017-04-06 01:21:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | 		// make setup(..) re-enterable.. | 
					
						
							|  |  |  | 		.stop() | 
					
						
							|  |  |  | 		.clear() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 01:21:32 +03:00
										 |  |  | 		//.randomLevel() | 
					
						
							| 
									
										
										
										
											2017-04-06 02:32:35 +03:00
										 |  |  | 		.basicLevel() | 
					
						
							| 
									
										
										
										
											2017-04-06 01:21:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | 		.appleEaten(function(){ this.apple() }) | 
					
						
							| 
									
										
										
										
											2017-04-06 01:21:32 +03:00
										 |  |  | 		.apple() | 
					
						
							|  |  |  | 		.apple() | 
					
						
							| 
									
										
										
										
											2017-04-05 18:53:07 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 02:32:35 +03:00
										 |  |  | 		// XXX do something better with direction... | 
					
						
							|  |  |  | 		.snakeKilled(function(color){ this.snake(color, null, 's', 3) }) | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | 		.snake('blue', null, 's', 3) | 
					
						
							| 
									
										
										
										
											2017-04-05 18:53:07 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | 		.start(300) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// setup kb handler (only once)... | 
					
						
							|  |  |  | 	if(!HANDLER_SET){ | 
					
						
							|  |  |  | 		document.addEventListener('keydown', kbHandler) | 
					
						
							|  |  |  | 		HANDLER_SET = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 01:26:11 +03:00
										 |  |  | //--------------------------------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | function test(game){ | 
					
						
							|  |  |  | 	return Snake | 
					
						
							|  |  |  | 		.setup(game || '.snake') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// XXX BUG: this will break as soon as it reaches the corner... | 
					
						
							|  |  |  | 		.snake('blue', {x:0, y:0}, 's', 5) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// hit an apple... | 
					
						
							|  |  |  | 		.snake('green', {x:5, y:3}, 's', 5) | 
					
						
							|  |  |  | 		.apple({x:5, y:5}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// hit a wall... | 
					
						
							|  |  |  | 		.snake('silver', {x:14, y:3}, 'w', 5) | 
					
						
							|  |  |  | 		.wall({x:2, y:14}, 's', 7) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		.start() | 
					
						
							| 
									
										
										
										
											2017-04-05 18:53:07 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 01:26:11 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 02:10:41 +03:00
										 |  |  | <head> <title>Simple Snake</title> </head> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 18:53:07 +03:00
										 |  |  | <body onload="setup()"> | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 04:38:11 +03:00
										 |  |  | <table class="snake field" cellspacing="0"> | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | 	<tr> <td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> | 
					
						
							|  |  |  | 		<td></td><td></td><td></td><td></td> <td></td><td></td><td></td><td></td> </tr> | 
					
						
							|  |  |  | </table> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 18:53:07 +03:00
										 |  |  | <p> | 
					
						
							|  |  |  | XXX basic instructions... | 
					
						
							|  |  |  | </p> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 01:58:52 +03:00
										 |  |  | </body> | 
					
						
							|  |  |  | </html> | 
					
						
							| 
									
										
										
										
											2017-04-05 18:53:07 +03:00
										 |  |  | <!-- vim:set ts=4 sw=4 spell : --> |