Skip to content

Commit

Permalink
Convert mdo+kec from the command line (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
frcepeda authored and lhchavez committed Dec 6, 2018
1 parent 3566dd1 commit 7b3d566
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cmd/kareljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var program = require('commander');
var fs = require('fs');
var version = require('../package.json').version;
var karel = require('../js/karel.js');
var util = require("../js/util.js");

function readStdin() {
return new Promise(function(resolve, reject) {
Expand Down Expand Up @@ -76,10 +77,17 @@ program
.action(function(options){
readStdin()
.then(stdin => {
return require("../js/util.js").Draw(stdin, options.output, options);
return util.Draw(stdin, options.output, options);
});
});

program
.command('convert <mdo> <kec>')
.description('Converts a .kec with its .mdo into a karel.js XML.')
.action(function(mdoPath, kecPath){
process.stdout.write(util.ImportMdoKec(mdoPath, kecPath).save());
});

program.parse(process.argv);

// vim: set expandtab:ts=2:sw=2
17 changes: 17 additions & 0 deletions js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ var Draw = function(worldString, outputFile, opts) {
return doneWriting;
};

// Takes the path to a pair of .mdo and .kec files and returns a karel.js world.
var ImportMdoKec = function(mdoPath, kecPath) {
var bufferToUint16Array = function(b) {
return new Uint16Array(b.buffer, b.byteOffset,
b.byteLength / Uint16Array.BYTES_PER_ELEMENT);
};

var mdo = bufferToUint16Array(fs.readFileSync(mdoPath));
var kec = bufferToUint16Array(fs.readFileSync(kecPath));

var world = new karel.World(100, 100);
world.import(mdo, kec);

return world;
};

if (typeof exports !== 'undefined') {
exports.Draw = Draw;
exports.ImportMdoKec = ImportMdoKec;
}
20 changes: 20 additions & 0 deletions test/mdokec/apagón.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<ejecucion>
<condiciones instruccionesMaximasAEjecutar="10000000" longitudStack="65000"/>
<mundos>
<mundo nombre="mundo_0" ancho="100" alto="100">
<pared x1="6" y1="10" x2="7"/>
<pared x1="4" y1="10" y2="11"/>
<pared x1="2" y1="11" y2="12"/>
<pared x1="4" y1="12" x2="5"/>
<pared x1="6" y1="11" y2="12"/>
<pared x1="2" y1="14" x2="3"/>
<pared x1="7" y1="13" y2="14"/>
<posicionDump x="6" y="12"/>
</mundo>
</mundos>
<programas tipoEjecucion="CONTINUA" intruccionesCambioContexto="1" milisegundosParaPasoAutomatico="0">
<programa nombre="p1" ruta="{$2$}" mundoDeEjecucion="mundo_0" xKarel="6" yKarel="9" direccionKarel="NORTE" mochilaKarel="INFINITO">
<despliega tipo="MUNDO"/>
</programa>
</programas>
</ejecucion>
Binary file added test/mdokec/apagón.kec
Binary file not shown.
Binary file added test/mdokec/apagón.mdo
Binary file not shown.
23 changes: 23 additions & 0 deletions test/problems.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,26 @@ describe('draw worlds', function() {
});
});
});

describe('import old mdo+kec', function() {
var oldCases = fs.readdirSync('test/mdokec');

oldCases.forEach(function(casename) {
if (!casename.endsWith('.in')) return;

it(casename.slice(0, -3), function() {
var inPath = 'test/mdokec/' + casename;
var mdoPath = inPath.slice(0, -3) + '.mdo';
var kecPath = inPath.slice(0, -3) + '.kec';

var world = util.ImportMdoKec(mdoPath, kecPath);

var output = world.save().replace(/\s+/g, '');

var expectedOutput =
fs.readFileSync(inPath, {encoding: 'utf-8'}).replace(/\s+/g, '');

assert.equal(output, expectedOutput);
});
});
});

0 comments on commit 7b3d566

Please sign in to comment.