vtm/quelldatein/aufbau/partie.ts
2018-03-20 13:30:00 +01:00

349 lines
8.7 KiB
TypeScript

/*
* 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/>.
*/
module mod_vtm_aufbau
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
export class klasse_partie
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
private welt : klasse_welt;
/**
* @author kcf <vidofnir@folksprak.org>
*/
private figur : schnittstelle_fehlermonade<klasse_figur>;
/**
* @author kcf <vidofnir@folksprak.org>
*/
private aufgabe : schnittstelle_aufgabe;
/**
* @author kcf <vidofnir@folksprak.org>
*/
private testindex : schnittstelle_fehlermonade<int>;
/**
* @author kcf <vidofnir@folksprak.org>
*/
private modus : typ_modus;
/**
* @author kcf <vidofnir@folksprak.org>
*/
private lauscher : {[ereignis : string] : Array<(angaben ?: any)=>void>};
/**
* @author kcf <vidofnir@folksprak.org>
*/
public constructor
(
aufgabe : schnittstelle_aufgabe
)
{
this.aufgabe = aufgabe;
this.welt_leeren(false);
this.zuruecksetzen(false);
this.lauscher = {
"aenderung_aufgabe": [],
"aenderung_welt": [],
"aenderung_figur": [],
"aenderung_modus": [],
};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
public lauschen(ereignis : string, prozedur : (angaben ?: any)=>void) : void
{
if (ereignis in this.lauscher)
{
this.lauscher[ereignis].push(prozedur);
}
else
{
let meldung : string = "kein Ereignis mit diesem Name";
throw (new Error(meldung));
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
private benachrichtigen(ereignis : string, angaben : any = {}) : void
{
if (ereignis in this.lauscher)
{
this.lauscher[ereignis].forEach
(
prozedur =>
{
prozedur(angaben);
}
)
;
}
else
{
let meldung : string = "kein Ereignis mit diesem Name";
throw (new Error(meldung));
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
public welt_leeren(bescheid_geben : boolean = true) : void
{
this.welt = klasse_welt.blanko();
if (bescheid_geben)
{
this.benachrichtigen("aenderung_welt", {});
}
else
{
// nichts tun
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
public zuruecksetzen(bescheid_geben : boolean = true) : void
{
this.figur = (new klasse_nichts<klasse_figur>());
this.testindex = (new klasse_nichts<int>());
this.modus = modus_initial;
if (bescheid_geben)
{
this.benachrichtigen("aenderung_figur", {});
this.benachrichtigen("aenderung_modus", {});
}
else
{
// nichts tun
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
public aufgabe_lesen() : schnittstelle_aufgabe
{
return this.aufgabe;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
public aufgabe_setzen(aufgabe : schnittstelle_aufgabe) : void
{
this.aufgabe = aufgabe;
this.welt_leeren();
this.benachrichtigen("aenderung_aufgabe", {});
this.zuruecksetzen();
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
public welt_lesen() : klasse_welt
{
return this.welt;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
public figur_lesen() : schnittstelle_fehlermonade<klasse_figur>
{
return this.figur;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
public modus_lesen() : typ_modus
{
return this.modus;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
public welt_feld_wechseln(stelle : typ_stelle, umgekehrt : boolean = false) : void
{
if (! (this.modus === mod_vtm_aufbau.modus_initial))
{
let meldung : string = "gesperrt";
}
else
{
this.welt.feld_wechseln(stelle, umgekehrt);
this.benachrichtigen("aenderung_welt", {});
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
public welt_feld_drehen(stelle : typ_stelle, inkrement : int = +1) : void
{
if (! (this.modus === mod_vtm_aufbau.modus_initial))
{
let meldung : string = "gesperrt";
}
else
{
this.welt.feld_drehen(stelle, inkrement);
this.benachrichtigen("aenderung_welt", {});
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
public fortfahren() : void
{
switch (this.modus)
{
case modus_initial:
{
this.modus = modus_ungewiss;
this.testindex = (new klasse_schlicht<int>(0));
this.benachrichtigen("aenderung_modus", {});
break;
}
case modus_ungewiss:
{
if (! this.figur.ist_schlicht())
{
let test : schnittstelle_test = this.aufgabe.tests()[this.testindex.lesen()];
let band : Array<typ_symbol> = mod_vtm_helfer.liste_kopieren<typ_symbol>(test.eingabe());
let stelle : typ_stelle = this.welt.erzeuger_finden();
this.figur = (
new klasse_schlicht<klasse_figur>
(
new klasse_figur
(
band,
stelle
)
)
);
}
else
{
let figur : klasse_figur = this.figur.lesen();
let stelle : typ_stelle = figur.stelle_lesen();
let aktor_ : schnittstelle_fehlermonade<schnittstelle_aktor> = this.welt.feld_holen(stelle);
let aktor : schnittstelle_aktor = (aktor_.ist_schlicht() ? aktor_.lesen() : (new klasse_verwerfer()));
aktor.verwenden(figur);
let zustand : typ_zustand = figur.zustand_lesen();
if (zustand === zustand_laufend)
{
// nichts tun
}
else if ((zustand === zustand_angenommen) || (zustand === zustand_abgelehnt))
{
let angenommen : boolean = (zustand === zustand_angenommen);
let ausgabe : Array<typ_symbol> = figur.band_lesen();
this.figur = (new klasse_nichts<klasse_figur>());
let testindex : int = this.testindex.lesen();
let tests : Array<schnittstelle_test> = this.aufgabe.tests();
let test : schnittstelle_test = tests[testindex];
if (! test.pruefen(angenommen, ausgabe))
{
this.modus = modus_fehlerhaft;
this.benachrichtigen("aenderung_modus", {});
}
else
{
testindex += 1;
if (testindex < tests.length)
{
// nächsten Test auswählen
this.testindex = (new klasse_schlicht<int>(testindex));
}
else
{
// auf Modus "korrekt" wechseln
this.testindex = (new klasse_nichts<int>());
this.modus = modus_korrekt;
this.benachrichtigen("aenderung_modus", {});
}
}
}
else
{
let meldung : string = "unbehandelter Zustand";
throw (new Error(meldung));
}
}
this.benachrichtigen("aenderung_figur", {});
break;
}
case modus_fehlerhaft:
{
// nichts tun
break;
}
case modus_korrekt:
{
// nichts tun
break;
}
default:
{
let meldung : string = "unbehandelter Modus";
throw (new Error(meldung));
break;
}
}
}
}
}