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
|
#-*- coding:utf-8 -*-
# Pybik -- A 3 dimensional magic cube game.
# Copyright © 2009-2011 B. Clausius <barcc@gmx.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Ported from GNUbik
# Original filename: move-queue.c
# Original copyright and license: 2004 Dale Mellor, GPL3+
import re
from .debug import *
# A structure containing information about a movement taking place.
class MoveData(object):
def __init__(self, maxis, mslice, mdir):
self.axis = maxis # int 0...
self.slice = mslice # int 0...dim-1 or -1 for all slices
self.dir = mdir # 0 or 1.
def copy(self):
return MoveData(self.axis, self.slice, self.dir)
def __str__(self):
return 'MoveData(maxis={}, mslice={}, mdir={})'.format(self.axis, self.slice, self.dir)
__repr__ = __str__
class MoveQueue(object):
class MoveQueueItem(MoveData):
def __init__(self, data, mark_after=False, code=None, markup=None):
if data:
MoveData.__init__(self, data.axis, data.slice, data.dir)
else:
MoveData.__init__(self, 0, 0, 0)
self.mark_after = mark_after
self.code = code
self.markup = markup
def copy(self):
return MoveQueue.MoveQueueItem(self,
mark_after=self.mark_after,
code=self.code,
markup=self.markup)
def __init__(self):
self.current_place = 0 # Number of steps of current from head.
self.moves = []
self.converter = FormatFlubrd
def at_start(self):
return self.current_place == 0
def at_end(self):
return self.current_place == self.queue_length
@property
def head(self):
return self.moves and self.moves[0] or None
@property
def tail(self):
return self.moves and self.moves[-1] or None
@property
def _prev(self):
return self.moves[self.current_place-1]
@property
def _current(self):
return self.moves[self.current_place]
@property
def queue_length(self):
return len(self.moves)
def __str__(self):
return 'MoveQueue(len=%s)' % self.queue_length
# Routine to push a new MoveData object onto the _back_ of the given queue. A
# new, locally-managed copy is made of the incumbent datum. Return 1 (one) if
# the operation is successful; 0 (zero) indicates that there was insufficient
# memory to grow the queue.
def push(self, move_data, mark_after=False, code=None, markup=None):
new_element = self.MoveQueueItem(move_data,
mark_after=mark_after, code=code, markup=markup)
self.moves.append(new_element)
return 1
# Procedure to copy the incumbent datum to the current place in the queue, and
# drop all subsequent queue entries (so that the current element becomes the
# tail). If current points past the end of the tail (including the case when
# there are no data in the queue), then a regular push operation is performed
# at the tail, and in this case zero may be returned if there is insufficient
# memory to grow the queue. In all other cases 1 (one) will be returned.
def push_current(self, move_data):
# If there are no data in the queue, then perform a standard push
# operation. Also do this if we are at the tail.
if not self.at_end():
self.moves[self.current_place:] = []
self.push(move_data)
return 1
def prev(self):
return None if self.at_start() else self._prev
# Routine to get the data at the 'current' position in the queue. If there are
# no data in the queue, None will be returned.
def current(self):
return None if self.at_end() else self._current
# Routine to retard the current pointer (move it towards the _head_ of the queue).
def retard(self):
if not self.at_start():
self.current_place -= 1
def rewind_start(self):
self.current_place = 0
# Remove the current object and all those that come afterwards.
def truncate(self):
self.moves[self.current_place:] = []
def truncate_before(self):
self.moves[:self.current_place] = []
self.current_place = 0
def reset(self):
self.current_place = 0
self.moves[:] = []
def advance(self):
if self.at_end():
return False
self.current_place += 1
return not self.at_end()
def invert(self):
self.moves.reverse()
for move in self.moves:
move.dir = 1 - move.dir
move.code = None
move.markup = None
if not (self.at_start() or self.at_end()):
self.current_place = len(self.moves) - self.current_place
def mark_current(self, mark=True):
if not self.at_start():
self._prev.mark_after = mark
if self._prev.code is not None:
self._prev.code = self._prev.code.replace(' ','')
#FIXME: recreate self._prev.markup
if mark:
self._prev.code += ' '
@debug_func
def format(self, size):
#TODO: arg to use explicit converter
code = ''
markup = ''
pos = 0
for i, move in enumerate(self.moves):
if move.code is None:
move.code, move.markup = self.converter.format(move, size)
code += move.code
markup += move.markup
if i < self.current_place:
pos = len(code)
return code, pos, markup
@debug_func
def parse_iter(self, code, pos, size):
#TODO: arg to use explicit converter
code = code.lstrip(' ')
queue_pos = self.current_place
move_code = ''
for i, c in enumerate(code):
if move_code and self.converter.isstart(c):
data, mark, markup_code = self.converter.parse(move_code, size)
#FIXME: if data is None an invalid move should be pushed
self.push(data, mark, move_code, markup_code)
yield data, queue_pos
if i == pos:
queue_pos = self.queue_length
move_code = c
else:
move_code += c
if i < pos:
queue_pos = self.queue_length + 1
if move_code:
data, mark, markup_code = self.converter.parse(move_code, size)
self.push(data, mark, move_code, markup_code)
if len(code)-len(move_code) < pos:
queue_pos = self.queue_length
yield data, queue_pos
def parse(self, code, pos, size):
qpos = 0
for res in self.parse_iter(code, pos, size):
qpos = res[1]
return qpos
class FormatFlubrd:
map_face_flubrd = 'flubrd'
re_flubrd = re.compile("(.)(\d*)(['-]?)([^ ]*)( *)(.*)")
@classmethod
def isstart(cls, char):
return char.lower() in cls.map_face_flubrd
@staticmethod
def _format_markup(mface, mslice, mdir, err1, mark, err2):
if err1: err1 = '<span underline="error" color="red">%s</span>' % err1
if err2: err2 = '<span underline="error">%s</span>' % err2
return ''.join((mface, mslice, mdir, err1, mark, err2))
@staticmethod
def intern_to_normal_move(maxis, mslice, mdir, size):
if mslice == -1:
return maxis+3 if mdir else maxis, 0, 0, True
elif mslice*2 > size-1:
return maxis+3, size-mslice-1, 1-mdir, False
else:
return maxis, mslice, mdir, False
@classmethod
def format(cls, move, size):
mface, mslice, mdir, whole_cube = cls.intern_to_normal_move(move.axis, move.slice, move.dir, size)
mface = cls.map_face_flubrd[mface]
if whole_cube:
mface = mface.upper()
mslice = str(mslice+1) if mslice else ''
mdir = '-' if mdir else ""
mark = ' ' if move.mark_after else ''
move_code = mface + mslice + mdir + mark
markup_code = cls._format_markup(mface, mslice, mdir, '', mark, '')
return move_code, markup_code
@staticmethod
def normal_to_intern_move(mface, mslice, mdir, whole_cube, size):
if mface < 3:
return mface, (-1 if whole_cube else mslice), mdir
else:
return mface-3, (-1 if whole_cube else size-mslice-1), 1-mdir
@classmethod
def parse(cls, move_code, size):
#debug('move_code: '+move_code)
mface, mslice, mdir, err1, mark, err2 = cls.re_flubrd.match(move_code).groups()
markup_code = cls._format_markup(mface, mslice, mdir, err1, mark, err2)
whole_cube = mface.isupper()
mface = cls.map_face_flubrd.find(mface.lower())
mslice = int(mslice or 1) - 1
mdir = int(bool(mdir))
mark = bool(mark)
if mface >= 0 and 0 <= mslice < size:
# move finished, normalize it
maxis, mslice, mdir = cls.normal_to_intern_move(mface, mslice, mdir, whole_cube, size)
return MoveData(maxis, mslice, mdir), mark, markup_code
else:
debug('Error parsing formula')
return None, False, move_code
|