vtm/quelldatein/aufbau/stelle.ts

102 lines
2.6 KiB
TypeScript
Raw Normal View History

2017-11-09 14:06:35 +01:00
/*
* Verrückte Turing-Maschinen A turing complete game
* Copyright (C) 2016 Christian Fraß <vidofnir@folksprak.org>
*
* 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 <https://www.gnu.org/licenses/>.
*/
2017-11-08 15:05:06 +01:00
module mod_vtm_aufbau
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
export type typ_stelle = {u : int; v : int;};
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function stelle_hash(stelle : typ_stelle) : string
{
return (stelle.u.toFixed(0) + "_" + stelle.v.toFixed(0));
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2017-11-08 18:41:56 +01:00
export function stelle_von_hash(hash : string) : typ_stelle
2017-11-08 15:05:06 +01:00
{
2017-11-08 18:41:56 +01:00
let teile : Array<string> = hash.split("_");
return {"u": parseInt(teile[0]), "v": parseInt(teile[1])};
2017-11-08 15:05:06 +01:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function stelle_null() : typ_stelle
{
return {"u": 0, "v": 0};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function stelle_von_richtung(richtung : typ_richtung) : typ_stelle
{
switch (richtung)
{
case 0: {return {"u": +1, "v": 0}; break;}
case 1: {return {"u": +1, "v": +1}; break;}
case 2: {return {"u": 0, "v": +1}; break;}
case 3: {return {"u": -1, "v": 0}; break;}
case 4: {return {"u": -1, "v": -1}; break;}
case 5: {return {"u": 0, "v": -1}; break;}
default: {throw (new Error("ungültige Richtung '" + String(richtung) + "'")); break;}
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function stelle_addieren(stelle1 : typ_stelle, stelle2 : typ_stelle) : typ_stelle
{
return {"u": (stelle1.u + stelle2.u), "v": (stelle1.v + stelle2.v)};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function stelle_exportieren(stelle : typ_stelle) : string
{
return stelle_hash(stelle);
}
2017-11-08 15:05:06 +01:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function stelle_importieren(stelle_ : string) : typ_stelle
{
return stelle_von_hash(stelle_);
}
2017-11-08 15:05:06 +01:00
}