gnome-games r8069 - in trunk/glchess/src/lib: . chess gtkui



Author: rancell
Date: Sat Oct 18 06:05:03 2008
New Revision: 8069
URL: http://svn.gnome.org/viewvc/gnome-games?rev=8069&view=rev

Log:
Translate LAN and SAN moves (Bug #462594)

Modified:
   trunk/glchess/src/lib/chess/__init__.py
   trunk/glchess/src/lib/chess/lan.py
   trunk/glchess/src/lib/chess/san.py
   trunk/glchess/src/lib/game.py
   trunk/glchess/src/lib/gtkui/chessview.py

Modified: trunk/glchess/src/lib/chess/__init__.py
==============================================================================
--- trunk/glchess/src/lib/chess/__init__.py	(original)
+++ trunk/glchess/src/lib/chess/__init__.py	Sat Oct 18 06:05:03 2008
@@ -54,6 +54,33 @@
     except IndexError:
         _rankMap[key] = r
 
+                 # Translators: The notation form of a pawn.
+                 # See http://en.wikipedia.org/wiki/Algebraic_chess_notation#Figurine_Algebraic_Notation for translations.
+                 # Do not translate the 'chess-notation|' text.
+_pieceStrings = {'P': _('chess-notation|P'),
+                 # Translators: The notation form of a knight. Do not translate the 'chess-notation|' text
+                 'N': _('chess-notation|N'),
+                 # Translators: The notation form of a bishop. Do not translate the 'chess-notation|' text
+                 'B': _('chess-notation|B'),
+                 # Translators: The notation form of a rook. Do not translate the 'chess-notation|' text                
+                 'R': _('chess-notation|R'),
+                 # Translators: The notation form of a queen. Do not translate the 'chess-notation|' text
+                 'Q': _('chess-notation|Q'),
+                 # Translators: The notation form of a king. Do not translate the 'chess-notation|' text                
+                 'K': _('chess-notation|K')}
+                 
+_pieceMap = {}
+for (key, r) in _pieceStrings.iteritems():
+    try:
+        _pieceMap[key] = r.split('|', 1)[1]
+    except IndexError:
+        _pieceMap[key] = r
+
+_notationMap = {}
+_notationMap.update(_fileMap)
+_notationMap.update(_rankMap)
+_notationMap.update(_pieceMap)    
+        
 def translate_file(file):
     """Get the translated form of a file.
     
@@ -72,8 +99,6 @@
     """
     return _rankMap[rank]
 
-positionStrings = {'a1': 'chess-position|a1'}
-
 def translate_coordinate(coordinate):
     """Get the translated form of a chess board coordinate.
     
@@ -84,3 +109,18 @@
     # FIXME: Assumes files are always before ranks. Should probably make all
     # 64 coordinates translatable to be strictly translatable
     return _fileMap[coordinate[0]] + _rankMap[coordinate[1]]
+
+def translate_notation(notation):
+    """Get the translated form of a chess move in LAN or SAN notation
+    
+    'notation' is the notation to translate (e.g. 'Nxc6', 'f2f4').
+    
+    Returns a translated form of this notation.
+    """
+    out = ''
+    for c in notation:
+        try:
+            out += _notationMap[c]
+        except KeyError:
+            out += c
+    return out

Modified: trunk/glchess/src/lib/chess/lan.py
==============================================================================
--- trunk/glchess/src/lib/chess/lan.py	(original)
+++ trunk/glchess/src/lib/chess/lan.py	Sat Oct 18 06:05:03 2008
@@ -19,15 +19,16 @@
 CASTLE_LONG  = 'o-o-o'
 
 # Characters used to describe pieces
-_typeToLAN = {board.PAWN:   'p',
-              board.KNIGHT: 'n',
-              board.BISHOP: 'b',
-              board.ROOK:   'r',
-              board.QUEEN:  'q',
-              board.KING:   'k'}
+_typeToLAN = {board.PAWN:   'P',
+              board.KNIGHT: 'N',
+              board.BISHOP: 'B',
+              board.ROOK:   'R',
+              board.QUEEN:  'Q',
+              board.KING:   'K'}
 _lanToType = {}
 for (pieceType, character) in _typeToLAN.iteritems():
     _lanToType[character] = pieceType
+    _lanToType[character.lower()] = pieceType # English pieces are sometimes written in lowercase
 
 class DecodeError(Exception):
     """
@@ -66,80 +67,79 @@
     moveType      = None
     result        = None
     
-    move = move.lower()
-
+    m = move
+    
     if colour is board.WHITE:
         baseFile = '1'
     else:
         baseFile = '8'
-    if move == CASTLE_SHORT:
+    if m == CASTLE_SHORT:
         return ('e' + baseFile, 'g' + baseFile, None, None, None, None)
-    elif move == CASTLE_LONG:
+    elif m == CASTLE_LONG:
         return ('e' + baseFile, 'c' + baseFile, None, None, None, None)
 
     # First character can be the piece types
-    if len(move) < 1:
+    if len(m) < 1:
         raise DecodeError('Too short')
     try:
-        pieceType = _lanToType[move[0]]
+        pieceType = _lanToType[m[0]]
     except KeyError:
         pieceType = None
     else:
-        move = move[1:]
+        m = m[1:]
 
-    if len(move) < 2:
+    if len(m) < 2:
         raise DecodeError('Too short')
     try:
-        start = _checkLocation(move[:2])
+        start = _checkLocation(m[:2])
     except DecodeError, e:
         if pieceType is None:
             raise e
         # Perhaps the first character wasn't a piece type
-        else:
-            move = _typeToLAN[pieceType] + move
-            start = _checkLocation(move[:2])
-            pieceType = None
-    move = move[2:]
+        m = move
+        start = _checkLocation(m[:2])
+        pieceType = None
+    m = m[2:]
         
-    if len(move) < 1:
+    if len(m) < 1:
         raise DecodeError('Too short')
-    if move[0] == MOVE or move[0] == TAKE:
-        moveType = move[0]
-        move = move[1:]
+    if m[0] == MOVE or m[0] == TAKE:
+        moveType = m[0]
+        m = m[1:]
 
-    if len(move) < 2:
+    if len(m) < 2:
         raise DecodeError('Too short')
-    end = _checkLocation(move[:2])
-    move = move[2:]
+    end = _checkLocation(m[:2])
+    m = m[2:]
 
     # Look for promotion type, note this can be in upper or lower case
-    if len(move) > 0:
-        if move[0] == '=':
-            if len(move) < 2:
+    if len(m) > 0:
+        if m[0] == '=':
+            if len(m) < 2:
                 raise DecodeError('Too short')
             try:
-                promotionType = _lanToType[move[1]]
+                promotionType = _lanToType[m[1]]
             except KeyError:
                 raise DecodeError('Unknown promotion type')
-            move = move[2:]
+            m = m[2:]
         else:
             try:
-                promotionType = _lanToType[move[0]]
+                promotionType = _lanToType[m[0]]
             except KeyError:
                 pass
             else:
-                move = move[1:]
+                m = m[1:]
 
-    if len(move) == 1:
-        if move == CHECK or move == CHECKMATE:
-            result = move
-            move = ''
-    elif len(move) == 2:
-        if move == '++':
+    if len(m) == 1:
+        if m == CHECK or m == CHECKMATE:
+            result = m
+            m = ''
+    elif len(m) == 2:
+        if m == '++':
             result = CHECKMATE
-            move = ''
+            m = ''
 
-    if len(move) != 0:
+    if len(m) != 0:
         raise DecodeError('Extra characters')
     
     return (start, end, pieceType, moveType, promotionType, result)

Modified: trunk/glchess/src/lib/chess/san.py
==============================================================================
--- trunk/glchess/src/lib/chess/san.py	(original)
+++ trunk/glchess/src/lib/chess/san.py	Sat Oct 18 06:05:03 2008
@@ -92,7 +92,7 @@
         """
         pass
     
-    def decodeSAN(self, colour, san):
+    def decode(self, colour, san):
         """Decode a SAN move.
         
         'colour' is the colour of the player making the move (self.WHITE or self.BLACK).
@@ -408,7 +408,7 @@
             
         def testDecode(self, colour, san):
             try:
-                result = self.decodeSAN(colour, san)
+                result = self.decode(colour, san)
                 print san.ljust(7) + ' => ' + str(result)
             except Error, e:
                 print san.ljust(7) + ' !! ' + str(e)

Modified: trunk/glchess/src/lib/game.py
==============================================================================
--- trunk/glchess/src/lib/game.py	(original)
+++ trunk/glchess/src/lib/game.py	Sat Oct 18 06:05:03 2008
@@ -246,16 +246,16 @@
         self.moveNumber = moveNumber
         chess.san.SANConverter.__init__(self)
     
-    def decodeSAN(self, colour, move):
-        (start, end, result, promotionType) = chess.san.SANConverter.decodeSAN(self, self.__colourToSAN[colour], move)
+    def decode(self, colour, move):
+        (start, end, result, promotionType) = chess.san.SANConverter.decode(self, self.__colourToSAN[colour], move)
         return (start, end, self.__sanToType[promotionType])
     
-    def encodeSAN(self, start, end, isTake, promotionType):
+    def encode(self, start, end, isTake, promotionType):
         if promotionType is None:
             promotion = self.QUEEN
         else:
             promotion = self.__typeToSAN[promotionType]
-        return self.encode(start, end, isTake, promotion)
+        return chess.san.SANConverter.encode(self, start, end, isTake, promotion)
 
     def getPiece(self, location):
         """Called by chess.san.SANConverter"""
@@ -524,7 +524,7 @@
         except chess.lan.DecodeError, e:
             converter = ChessGameSANConverter(self.board, len(self.__moves))
             try:
-                (start, end, promotionType) = converter.decodeSAN(colour, move)
+                (start, end, promotionType) = converter.decode(colour, move)
             except chess.san.Error, e:
                 print 'Invalid move: ' + move
                 return
@@ -549,7 +549,7 @@
         canMove = chess.lan.encode(colour, start, end, promotionType = promotion)
         converter = ChessGameSANConverter(self.board, len(self.__moves))
         try:
-            sanMove = converter.encodeSAN(start, end, moveResult.victim != None, promotionType)
+            sanMove = converter.encode(start, end, moveResult.victim != None, promotionType)
         except chess.san.Error:
             # If for some reason we couldn't make the SAN move the use the CAN move instead
             sanMove = canMove

Modified: trunk/glchess/src/lib/gtkui/chessview.py
==============================================================================
--- trunk/glchess/src/lib/gtkui/chessview.py	(original)
+++ trunk/glchess/src/lib/gtkui/chessview.py	Sat Oct 18 06:05:03 2008
@@ -380,15 +380,15 @@
                 format = '%(movenum)2i. ... %(move_san)s'
             else:
                 format = '%(movenum)2i. %(move_san)s'
-            return format % {'movenum': moveNumber, 'move_san': move.sanMove}
-
+            return format % {'movenum': moveNumber, 'move_san': glchess.chess.translate_notation(move.sanMove)}
+        
         # Note LAN format is intentionally not translated
         if self.moveFormat == 'lan':
             if move.number % 2 == 0:
                 format = '%(movenum)2i. ... %(move_can)s'
             else:
                 format = '%(movenum)2i. %(move_can)s'
-            return format % {'movenum': moveNumber, 'move_can': move.canMove}
+            return format % {'movenum': moveNumber, 'move_can': glchess.chess.translate_notation(move.canMove)}
 
         WHITE  = glchess.chess.board.WHITE
         BLACK  = glchess.chess.board.BLACK



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]