From c0d82707cc18e92cce11a37d53d63d469fc3b2a5 Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Tue, 5 Jul 2011 10:14:15 +0200 Subject: [PATCH] format_level using regexp replace (a bit over-the-top maybe) --- star.js/js/star.js | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/star.js/js/star.js b/star.js/js/star.js index b328fd1..0dc233d 100644 --- a/star.js/js/star.js +++ b/star.js/js/star.js @@ -96,15 +96,36 @@ function won_or_not( state ) { } function format_level( state ) { - var dl = ""; - for each ( c in state.board ) { - var classes = [ "starcell ", css_classes[ c ] ].join( '' ); - if ( state.moving == c) { - classes = [ classes, " selected " ].join( '' ); + function Replacer( conversionObject ) { + + var regexpStr = ''; + for ( var k in conversionObject ) { + regexpStr += ( regexpStr.length ? '|' : '' ) + k; } - dl = [ dl, "", c, ""].join( '' ); + var regexpr = new RegExp( regexpStr, 'mig' ); // g: global, m:multi-line i: ignore case + return function(s) { + return s.replace( regexpr, function(str, p1, p2, offset, s) { + var a = conversionObject[ str ]; + return a == undefined ? str : a; + } ); + }; } - return dl; + + var substitutions = { + '#': '#', + 'x': 'x', + ' ': ' ' + }; + if ( state.moving == cell.BALL ) { + substitutions[ '@' ] = '@'; + substitutions[ 'H' ] = 'H'; + } + else { + substitutions[ '@' ] = '@'; + substitutions[ 'H' ] = 'H'; + } + var myReplacer = Replacer( substitutions ); + return myReplacer( state.board ); } function load_level( levelset, nb ) {