Guest User

Untitled

a guest
Jul 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # config
  5.  
  6. # SIZE of FIELD
  7. MAX_X = 8
  8. MAX_Y = 8
  9.  
  10. # simbolic
  11. HORSE_STEP = '_♘'
  12. HORSE_BEGIN = '♞*'
  13. HORSE_NULL = ' '
  14. WHERE = '☼♾'
  15. WHERE_WIN = '⚔☹'
  16.  
  17. # horse begin coordinates
  18.  
  19. HORSE_X = 2
  20. HORSE_Y = 6
  21.  
  22. # purpos coordinates
  23.  
  24. WHERE_X = 6
  25. WHERE_Y = 4
  26.  
  27. # 0 - off, 1 - on, 2 - debug
  28. LOG_LEVEL = 2
  29.  
  30. # support (dont modify)
  31.  
  32. WIN = 0
  33. TABLE = []
  34.  
  35. def show_log(text, level):
  36. if level == 0: return
  37. if level <= LOG_LEVEL: print text
  38.  
  39. def table_generate(x, y, horse):
  40. table = []
  41. for i in xrange(MAX_Y):
  42. line = []
  43. for j in xrange(MAX_X):
  44. ch = HORSE_NULL
  45. if (x == j) and (y == i): ch = horse
  46. line.append(ch)
  47. table.append(line)
  48. return table
  49.  
  50. def string_line(ln):
  51. res = '--'
  52. for i in xrange(ln):
  53. res = res + '---'
  54. return res
  55.  
  56. def show_table(table):
  57. if len(table) == 0: return
  58. line = ' |'
  59. ln_str = len(table[0])
  60. # string_line(ln_str)
  61.  
  62. for i in xrange(ln_str):
  63. if i+1 < 10: line = line + ' %d|' % (i+1)
  64. else: line = line + '%d|' % (i+1)
  65. show_log(line, 1)
  66. i = 0
  67. show_log(string_line(ln_str), 1)
  68.  
  69. for line in table:
  70. i += 1
  71. if i < 10: show_log(' %d|' % (i)+'|'.join(line)+
  72. '|\n%s' % (string_line(ln_str)), 1)
  73. else: show_log('%d|' % (i)+'|'.join(line)+
  74. '|\n%s' % (string_line(ln_str)), 1)
  75.  
  76. def get_step(x, mx):
  77. res = []
  78. dx = x + 1
  79. if dx < mx: res.append(dx)
  80. dx = x - 1
  81. if dx > -1: res.append(dx)
  82. return res
  83.  
  84.  
  85. def get_variants(ly, lx, y, x):
  86. # d2
  87. res = {}
  88. dx = x + 2
  89. if dx < lx:
  90. res['x1'] = dx
  91. res['y1'] = get_step(y, ly)
  92. dx = x - 2
  93. if dx > -1:
  94. res['x2'] = dx
  95. res['y2'] = get_step(y, ly)
  96. dy = y + 2
  97. if dy < ly:
  98. res['y3'] = dy
  99. res['x3'] = get_step(x, lx)
  100. dy = y - 2
  101. if dy > -1:
  102. res['y4'] = dy
  103. res['x4'] = get_step(x, lx)
  104. return res
  105.  
  106. def get_pos(table, horse, step):
  107. global TABLE
  108. global WIN
  109. step += 1
  110. ident = step % 2
  111. if ident == 0 : show_log('STEP: %d TRUE' % (step), 2)
  112. else: show_log('STEP: %d FALSE' % (step), 2)
  113.  
  114. i = 0
  115. for y in table:
  116. j = 0
  117. for x in y:
  118. if x == horse:
  119. res = get_variants(len(table), len(y), i, j)
  120. X_NOW = j
  121. Y_NOW = i
  122. j += 1
  123. i += 1
  124. try:
  125. for y in res['y1']:
  126. x = res['x1']
  127. table = table_generate(y, x, 'H1')
  128. show_log('TRY GOTO %d:%d --> %d:%d' % (Y_NOW, X_NOW, y, x), 2)
  129. if TABLE[y][x] == HORSE_NULL:
  130. show_log('NEW LOCATE %d:%d' % (y, x), 2)
  131. if ident == 0:
  132. show_log('STEP TRUE, MARK FIELD', 2)
  133. TABLE[y][x] = HORSE_STEP
  134. if (x == WHERE_X) and (y == WHERE_Y):
  135. TABLE[y][x] = WHERE_WIN
  136. show_log('WIN!!!111', 2)
  137. WIN = 1
  138. get_pos(table, 'H1', step)
  139. except: pass
  140. try:
  141. for y in res['y2']:
  142. x = res['x2']
  143. table = table_generate(y, x, 'H2')
  144. show_log('TRY GOTO %d:%d --> %d:%d' % (Y_NOW, X_NOW, y, x), 2)
  145. if TABLE[y][x] == HORSE_NULL:
  146. show_log('NEW LOCATE %d:%s' % (y, x), 2)
  147. if ident == 0:
  148. show_log('STEP TRUE, MARK FIELD', 2)
  149. TABLE[y][x] = HORSE_STEP
  150. if (x == WHERE_X) and (y == WHERE_Y):
  151. TABLE[y][x] = WHERE_WIN
  152. show_log('WIN!!!111', 2)
  153. WIN = 1
  154. get_pos(table, 'H2', step)
  155. except: pass
  156. try:
  157. for x in res['x3']:
  158. y = res['y3']
  159. table = table_generate(y, x, 'H3')
  160. show_log('TRY GOTO %d:%d --> %d:%d' % (Y_NOW, X_NOW, y, x), 2)
  161. if TABLE[y][x] == HORSE_NULL:
  162. show_log('NEW LOCATE %d:%d' % (y, x), 2)
  163. if ident == 0:
  164. show_log('STEP TRUE, MARK FIELD', 2)
  165. TABLE[y][x] = HORSE_STEP
  166. if (x == WHERE_X) and (y == WHERE_Y):
  167. TABLE[y][x] = WHERE_WIN
  168. show_log('WIN!!!111', 2)
  169. WIN = 1
  170. get_pos(table, 'H3', step)
  171. except: pass
  172. try:
  173. for x in res['x4']:
  174. y = res['y4']
  175. table = table_generate(y, x, 'H4')
  176. show_log('TRY GOTO %d:%d --> %d:%d' % (Y_NOW, X_NOW, y, x), 2)
  177. if TABLE[y][x] == HORSE_NULL:
  178. show_log('NEW LOCATE %d:%d' % (y, x), 2)
  179. if ident == 0:
  180. show_log('STEP TRUE, MARK FIELD', 2)
  181. TABLE[y][x] = HORSE_STEP
  182. if (x == WHERE_X) and (y == WHERE_Y):
  183. TABLE[y][x] = WHERE_WIN
  184. show_log('WIN!!!111', 2)
  185. WIN = 1
  186. get_pos(table, 'H4', step)
  187. except: pass
  188. return []
  189.  
  190. if __name__=='__main__':
  191. WHERE_X += -1
  192. WHERE_Y += -1
  193. show_log('Starting with HORSE %d:%d\n' % (HORSE_Y, HORSE_X), 1)
  194. TABLE = table_generate(HORSE_Y-1, HORSE_X-1, HORSE_BEGIN)
  195. get_pos(TABLE, HORSE_BEGIN, 0)
  196. if WIN == 0: TABLE[WHERE_Y-1][WHERE_X-1] = WHERE
  197. show_table(TABLE)
  198. if WIN == 1: print '\nWIN --> %d:%d' % (WHERE_Y+1, WHERE_X+1)
  199. else: print '\nFAIL --> %d:%d' % (WHERE_Y, WHERE_X)
Add Comment
Please, Sign In to add comment