Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- # config
- # SIZE of FIELD
- MAX_X = 8
- MAX_Y = 8
- # simbolic
- HORSE_STEP = '_♘'
- HORSE_BEGIN = '♞*'
- HORSE_NULL = ' '
- WHERE = '☼♾'
- WHERE_WIN = '⚔☹'
- # horse begin coordinates
- HORSE_X = 2
- HORSE_Y = 6
- # purpos coordinates
- WHERE_X = 6
- WHERE_Y = 4
- # 0 - off, 1 - on, 2 - debug
- LOG_LEVEL = 2
- # support (dont modify)
- WIN = 0
- TABLE = []
- def show_log(text, level):
- if level == 0: return
- if level <= LOG_LEVEL: print text
- def table_generate(x, y, horse):
- table = []
- for i in xrange(MAX_Y):
- line = []
- for j in xrange(MAX_X):
- ch = HORSE_NULL
- if (x == j) and (y == i): ch = horse
- line.append(ch)
- table.append(line)
- return table
- def string_line(ln):
- res = '--'
- for i in xrange(ln):
- res = res + '---'
- return res
- def show_table(table):
- if len(table) == 0: return
- line = ' |'
- ln_str = len(table[0])
- # string_line(ln_str)
- for i in xrange(ln_str):
- if i+1 < 10: line = line + ' %d|' % (i+1)
- else: line = line + '%d|' % (i+1)
- show_log(line, 1)
- i = 0
- show_log(string_line(ln_str), 1)
- for line in table:
- i += 1
- if i < 10: show_log(' %d|' % (i)+'|'.join(line)+
- '|\n%s' % (string_line(ln_str)), 1)
- else: show_log('%d|' % (i)+'|'.join(line)+
- '|\n%s' % (string_line(ln_str)), 1)
- def get_step(x, mx):
- res = []
- dx = x + 1
- if dx < mx: res.append(dx)
- dx = x - 1
- if dx > -1: res.append(dx)
- return res
- def get_variants(ly, lx, y, x):
- # d2
- res = {}
- dx = x + 2
- if dx < lx:
- res['x1'] = dx
- res['y1'] = get_step(y, ly)
- dx = x - 2
- if dx > -1:
- res['x2'] = dx
- res['y2'] = get_step(y, ly)
- dy = y + 2
- if dy < ly:
- res['y3'] = dy
- res['x3'] = get_step(x, lx)
- dy = y - 2
- if dy > -1:
- res['y4'] = dy
- res['x4'] = get_step(x, lx)
- return res
- def get_pos(table, horse, step):
- global TABLE
- global WIN
- step += 1
- ident = step % 2
- if ident == 0 : show_log('STEP: %d TRUE' % (step), 2)
- else: show_log('STEP: %d FALSE' % (step), 2)
- i = 0
- for y in table:
- j = 0
- for x in y:
- if x == horse:
- res = get_variants(len(table), len(y), i, j)
- X_NOW = j
- Y_NOW = i
- j += 1
- i += 1
- try:
- for y in res['y1']:
- x = res['x1']
- table = table_generate(y, x, 'H1')
- show_log('TRY GOTO %d:%d --> %d:%d' % (Y_NOW, X_NOW, y, x), 2)
- if TABLE[y][x] == HORSE_NULL:
- show_log('NEW LOCATE %d:%d' % (y, x), 2)
- if ident == 0:
- show_log('STEP TRUE, MARK FIELD', 2)
- TABLE[y][x] = HORSE_STEP
- if (x == WHERE_X) and (y == WHERE_Y):
- TABLE[y][x] = WHERE_WIN
- show_log('WIN!!!111', 2)
- WIN = 1
- get_pos(table, 'H1', step)
- except: pass
- try:
- for y in res['y2']:
- x = res['x2']
- table = table_generate(y, x, 'H2')
- show_log('TRY GOTO %d:%d --> %d:%d' % (Y_NOW, X_NOW, y, x), 2)
- if TABLE[y][x] == HORSE_NULL:
- show_log('NEW LOCATE %d:%s' % (y, x), 2)
- if ident == 0:
- show_log('STEP TRUE, MARK FIELD', 2)
- TABLE[y][x] = HORSE_STEP
- if (x == WHERE_X) and (y == WHERE_Y):
- TABLE[y][x] = WHERE_WIN
- show_log('WIN!!!111', 2)
- WIN = 1
- get_pos(table, 'H2', step)
- except: pass
- try:
- for x in res['x3']:
- y = res['y3']
- table = table_generate(y, x, 'H3')
- show_log('TRY GOTO %d:%d --> %d:%d' % (Y_NOW, X_NOW, y, x), 2)
- if TABLE[y][x] == HORSE_NULL:
- show_log('NEW LOCATE %d:%d' % (y, x), 2)
- if ident == 0:
- show_log('STEP TRUE, MARK FIELD', 2)
- TABLE[y][x] = HORSE_STEP
- if (x == WHERE_X) and (y == WHERE_Y):
- TABLE[y][x] = WHERE_WIN
- show_log('WIN!!!111', 2)
- WIN = 1
- get_pos(table, 'H3', step)
- except: pass
- try:
- for x in res['x4']:
- y = res['y4']
- table = table_generate(y, x, 'H4')
- show_log('TRY GOTO %d:%d --> %d:%d' % (Y_NOW, X_NOW, y, x), 2)
- if TABLE[y][x] == HORSE_NULL:
- show_log('NEW LOCATE %d:%d' % (y, x), 2)
- if ident == 0:
- show_log('STEP TRUE, MARK FIELD', 2)
- TABLE[y][x] = HORSE_STEP
- if (x == WHERE_X) and (y == WHERE_Y):
- TABLE[y][x] = WHERE_WIN
- show_log('WIN!!!111', 2)
- WIN = 1
- get_pos(table, 'H4', step)
- except: pass
- return []
- if __name__=='__main__':
- WHERE_X += -1
- WHERE_Y += -1
- show_log('Starting with HORSE %d:%d\n' % (HORSE_Y, HORSE_X), 1)
- TABLE = table_generate(HORSE_Y-1, HORSE_X-1, HORSE_BEGIN)
- get_pos(TABLE, HORSE_BEGIN, 0)
- if WIN == 0: TABLE[WHERE_Y-1][WHERE_X-1] = WHERE
- show_table(TABLE)
- if WIN == 1: print '\nWIN --> %d:%d' % (WHERE_Y+1, WHERE_X+1)
- else: print '\nFAIL --> %d:%d' % (WHERE_Y, WHERE_X)
Add Comment
Please, Sign In to add comment