github snake

在github上玩贪吃蛇

安装此脚本
作者推荐脚本

您可能也喜欢github rainmeter

安装此脚本
  1. // ==UserScript==
  2. // @name github snake
  3. // @author wusuluren
  4. // @description 在github上玩贪吃蛇
  5. // @require http://cdn.bootcss.com/jquery/1.8.3/jquery.min.js
  6. // @match *://github.com/*
  7. // @supportURL https://github.com/Wusuluren
  8. // @version 0.0.1
  9. // @grant None
  10. // @namespace https://greasyfork.org/users/194747
  11. // ==/UserScript==
  12. (function () {
  13. 'use strict';
  14. class Position {
  15. constructor(x, y) {
  16. this.x = x
  17. this.y = y
  18. }
  19. }
  20. class SnakeNode extends Position {
  21. constructor(x, y, prev, next) {
  22. super(x, y)
  23. this.prev = prev
  24. this.next = next
  25. }
  26. }
  27. const CLEAR_COLOR = 0
  28. const SNAKE_COLOR = 2
  29. const FOOD_COLOR = 4
  30. var color_table = new Array()
  31. var snake_head, snake_tail
  32. var food
  33. var Screen_Width, Screen_Height
  34. var map
  35. $(function(){
  36. get_color_table()
  37. full(0)
  38. init()
  39. var timer = setInterval(function() {
  40. if (!move_snake()) {
  41. clearInterval(timer)
  42. full(0)
  43. return
  44. }
  45. }, 1000)
  46. })
  47. function get_color_table () {
  48. var lis = $('.legend').children()
  49. for (var i = 0; i < lis.length; i++) {
  50. color_table[i] = lis[i].style.backgroundColor
  51. }
  52. }
  53. function draw_point(x, y, depth) {
  54. var gs = document.getElementsByTagName('g')
  55. if (x < gs.length) {
  56. var days = gs[x].getElementsByClassName('day')
  57. if (y < days.length) {
  58. days[y].setAttribute('fill', color_table[depth])
  59. }
  60. }
  61. }
  62. function draw_position(pos, depth) {
  63. var gs = document.getElementsByTagName('g')
  64. if (pos.x < gs.length) {
  65. var days = gs[pos.x].getElementsByClassName('day')
  66. if (pos.y < days.length) {
  67. days[pos.y].setAttribute('fill', color_table[depth])
  68. }
  69. }
  70. }
  71. function full (depth) {
  72. var gs = document.getElementsByTagName('g')
  73. if (gs === undefined)
  74. return false
  75. for (var x = 1; x < gs.length; x++) {
  76. for (var y = 0; y < 7; y++)
  77. draw_point(x, y, depth)
  78. }
  79. return true
  80. }
  81. function new_position() {
  82. while(1) {
  83. var x = Math.floor(Math.random() * Screen_Width)
  84. var y = Math.floor(Math.random() * Screen_Height)
  85. if (map[y][x] === undefined || map[y][x] == 0)
  86. break
  87. }
  88. return new Position(x, y)
  89. }
  90. function same_position(a, b) {
  91. return a.x == b.x && a.y == b.y
  92. }
  93. function init() {
  94. var gs = document.getElementsByTagName('g')
  95. Screen_Width = gs.length
  96. Screen_Height = 7
  97. map = new Array()
  98. for (var y = 0; y < Screen_Height; y++) {
  99. map[y] = new Array()
  100. for (var x = 0; x < Screen_Width; x++)
  101. map[y][x] = 0
  102. }
  103. var pos = new_position()
  104. snake_head = new SnakeNode(pos.x, pos.y, undefined, undefined)
  105. snake_tail = snake_head
  106. draw_position(snake_head, SNAKE_COLOR)
  107. map[pos.y][pos.x] = 1
  108. food = new_position()
  109. draw_position(food, FOOD_COLOR)
  110. }
  111. function in_screen(x, y) {
  112. return (x >= 0 && x < Screen_Width) && (y >= 0 && y < Screen_Height)
  113. }
  114. function get_safe_x(x) {
  115. if (x < 0)
  116. return Screen_Width - 1
  117. if (x >= Screen_Width)
  118. return 0
  119. return x
  120. }
  121. function get_safe_y(y) {
  122. if (y < 0)
  123. return Screen_Height - 1
  124. if (y >= Screen_Height)
  125. return 0
  126. return y
  127. }
  128. function get_next_step() {
  129. var gap_x = [0, -1, 0, 1]
  130. var gap_y = [-1, 0, 1, 0]
  131. var min_score = Infinity
  132. var min_idx = 0
  133. for (var i = 0; i < 4; i++) {
  134. var x = get_safe_x(snake_head.x + gap_x[i])
  135. var y = get_safe_y(snake_head.y + gap_y[i])
  136. if (map[y][x] == 0) {
  137. var score = Math.abs(food.x - x) + Math.abs(food.y - y)
  138. if (score < min_score) {
  139. min_score = score
  140. min_idx = i
  141. }
  142. }
  143. }
  144. return new Position(get_safe_x(snake_head.x + gap_x[min_idx]), get_safe_y(snake_head.y + gap_y[min_idx]))
  145. }
  146. function move_snake() {
  147. var next_step = get_next_step()
  148. if (!judge_game(next_step)) {
  149. return false
  150. }
  151. var new_head = new SnakeNode(next_step.x, next_step.y, undefined, snake_head)
  152. snake_head.prev = new_head
  153. snake_head = new_head
  154. var discard_node = snake_tail
  155. snake_tail = snake_tail.prev
  156. snake_tail.next = undefined
  157. map[snake_head.y][snake_head.x] = 1
  158. map[discard_node.y][discard_node.x] = 0
  159. draw_position(discard_node, CLEAR_COLOR)
  160. draw_position(snake_head, SNAKE_COLOR)
  161. return true
  162. }
  163. function judge_game(next_step) {
  164. if (same_position(next_step, food)) {
  165. var pos = new_position()
  166. var node = new SnakeNode(pos.x, pos.y, snake_tail, undefined)
  167. snake_tail.next = node
  168. snake_tail = node
  169. map[snake_tail.y][snake_tail.x] = 1
  170. food = new_position()
  171. draw_position(food, FOOD_COLOR)
  172. return true
  173. }
  174. if (map[next_step.y][next_step.x] == 1) {
  175. return false
  176. }
  177. return true
  178. }
  179. })();