efgfunc.cc
来自「Gambit 是一个游戏库理论软件」· CC 代码 · 共 1,499 行 · 第 1/3 页
CC
1,499 行
// IsSuccessor//---------------static Portion *GSM_IsSuccessor(GSM &, Portion **param){ Node *n1 = ((NodePortion *) param[0])->Value(); Node *n2 = ((NodePortion *) param[1])->Value(); return new BoolPortion(n1->Game()->IsSuccessor(n1, n2));}//-----------// LoadEfg//-----------static Portion *GSM_LoadEfg(GSM &, Portion **param){ gText file = ((TextPortion *) param[0])->Value(); try { gFileInput f(file); efgGame *efg = ReadEfgFile(f); if (efg) return new EfgPortion(efg); else throw gclRuntimeError(file + " is not a valid .efg file"); } catch (gFileInput::OpenFailed &) { throw gclRuntimeError("Unable to open file " + file + " for reading"); }}//-------------------// IsPerfectRecall//-------------------static Portion *GSM_IsPerfectRecall(GSM &, Portion **param){ Infoset *s1, *s2; efgGame &efg = * ((EfgPortion *) param[0])->Value(); return new BoolPortion(IsPerfectRecall(efg, s1, s2));}//-----------------// MarkSubgame//-----------------static Portion *GSM_MarkSubgame(GSM &, Portion **param){ Node *n = ((NodePortion *) param[0])->Value(); return new BoolPortion(n->Game()->MarkSubgame(n));}//------------------// MarkedSubgame//------------------static Portion *GSM_MarkedSubgame(GSM &, Portion **param){ Node *n = ((NodePortion *) param[0])->Value(); return new BoolPortion(n->GetSubgameRoot() == n);}//------------// Members//------------static Portion *GSM_Members(GSM &, Portion **param){ if (param[0]->Spec().Type == porNULL) return new ListPortion(); Infoset *s = ((InfosetPortion *) param[0])->Value(); Portion* por = ArrayToList(s->Members()); return por;}//----------------// MergeInfosets//----------------static Portion *GSM_MergeInfosets(GSM &gsm, Portion **param){ Infoset *s1 = ((InfosetPortion *) param[0])->Value(); Infoset *s2 = ((InfosetPortion *) param[1])->Value(); s1->Game()->MergeInfoset(s1, s2); gsm.UnAssignGameElement(s1->Game(), true, porBEHAV | porEFSUPPORT); return new InfosetPortion(s1);}//----------------// MoveToInfoset//----------------static Portion *GSM_MoveToInfoset(GSM &gsm, Portion **param){ Node *n = ((NodePortion *) param[0])->Value(); Infoset *s = ((InfosetPortion *) param[1])->Value(); s->Game()->JoinInfoset(s, n); gsm.UnAssignGameElement(s->Game(), true, porBEHAV | porEFSUPPORT); return new NodePortion(n);}//-------------// MoveTree//-------------static Portion *GSM_MoveTree(GSM &gsm, Portion **param){ Node *n1 = ((NodePortion *) param[0])->Value(); Node *n2 = ((NodePortion *) param[1])->Value(); gsm.UnAssignGameElement(n1->Game(), true, porBEHAV | porEFSUPPORT); return new NodePortion(n1->Game()->MoveTree(n1, n2));}//----------// Name//----------static Portion *GSM_Name(GSM &, Portion **param){ if (param[0]->Spec().Type == porNULL) return new TextPortion(""); switch (param[0]->Spec().Type) { case porACTION: return new TextPortion(((ActionPortion *) param[0])->Value()-> GetName()); case porINFOSET: return new TextPortion(((InfosetPortion *) param[0])->Value()-> GetName()); case porNODE: return new TextPortion(((NodePortion *) param[0])->Value()-> GetName()); case porEFOUTCOME: return new TextPortion(((EfOutcomePortion *) param[0])->Value()->GetGame()->GetOutcomeName(((EfOutcomePortion *) param[0])->Value())); case porEFPLAYER: return new TextPortion(((EfPlayerPortion *) param[0])->Value()-> GetName()); case porEFSUPPORT: return new TextPortion(((EfSupportPortion *) param[0])->Value()->GetName()); case porEFG: return new TextPortion(((EfgPortion *) param[0])->Value()-> GetTitle()); default: throw gclRuntimeError("Unknown type passed to Name[]"); }}//------------// NewEfg//------------static Portion *GSM_NewEfg(GSM &, Portion **param){ efgGame *E = new efgGame; ListPortion *players = (ListPortion *) param[0]; for (int i = 1; i <= players->Length(); i++) E->NewPlayer()->SetName(((TextPortion *) (*players)[i])->Value()); return new EfgPortion(E);}//--------------// NewInfoset//--------------static Portion *GSM_NewInfoset(GSM &gsm, Portion **param){ EFPlayer *p = ((EfPlayerPortion *) param[0])->Value(); int n = ((NumberPortion *) param[1])->Value(); if (n <= 0) throw gclRuntimeError("Information sets must have at least one action"); Infoset *s = p->Game()->CreateInfoset(p, n); gsm.UnAssignGameElement(p->Game(), true, porBEHAV | porEFSUPPORT); return new InfosetPortion(s);} //--------------// NewOutcome//--------------static Portion *GSM_NewOutcome(GSM &, Portion **param){ efgGame *efg = ((EfgPortion *) param[0])->Value(); efgOutcome *outcome = efg->NewOutcome(); return new EfOutcomePortion(outcome);}//---------------// NewPlayer//---------------static Portion *GSM_NewPlayer(GSM &gsm, Portion **param){ efgGame &E = *((EfgPortion*) param[0])->Value(); EFPlayer *p = E.NewPlayer(); gsm.UnAssignGameElement(&E, true, porBEHAV | porEFSUPPORT); return new EfPlayerPortion(p);}//----------------// NextSibling//----------------static Portion *GSM_NextSibling(GSM &, Portion **param){ Node *n = ((NodePortion *) param[0])->Value()->NextSibling(); if (!n) return new NullPortion(porNODE); return new NodePortion(n);}//----------// Nodes//----------static Portion *GSM_Nodes(GSM &, Portion **param){ efgGame *efg = ((EfgPortion *) param[0])->Value(); gList<Node *> nodes; Nodes(*efg, nodes); Portion *por = ArrayToList(nodes); return por;}//---------------// NthChild//---------------static Portion *GSM_NthChild(GSM &, Portion **param){ Node *n = ((NodePortion *) param[0])->Value(); efgGame *e = ((efgGame*) param[0]->Game()); int child = ((NumberPortion *) param[1])->Value(); if (child < 1 || child > e->NumChildren(n)) return new NullPortion(porNODE); return new NodePortion(n->GetChild(child));}//------------// Outcome//------------static Portion *GSM_Outcome(GSM &, Portion **param){ if (param[0]->Spec().Type == porNULL) { return new NullPortion(porEFOUTCOME); } Node *n = ((NodePortion *) param[0])->Value(); efgOutcome *outcome = n->Game()->GetOutcome(n); if (!outcome) { return new NullPortion(porEFOUTCOME); } else { return new EfOutcomePortion(outcome); }}//------------// Outcomes//------------static Portion *GSM_Outcomes(GSM &, Portion **param){ efgGame *efg = ((EfgPortion*) param[0])->Value(); ListPortion *ret = new ListPortion; for (int outc = 1; outc <= efg->NumOutcomes(); outc++) { ret->Append(new EfOutcomePortion(efg->GetOutcome(outc))); } return ret;}//------------// Parent//------------static Portion *GSM_Parent(GSM &, Portion **param){ if( param[0]->Spec().Type == porNULL ) { return new NullPortion(porNODE); } Node *n = ((NodePortion *) param[0])->Value(); if (!n->GetParent()) return new NullPortion(porNODE); return new NodePortion(n->GetParent());}//-----------// Payoff//-----------static Portion *GSM_Payoff(GSM &, Portion **param){ if (param[0]->Spec().Type == porNULL) return new NumberPortion(0); efgOutcome *outcome = ((EfOutcomePortion *) param[0])->Value(); EFPlayer *player = ((EfPlayerPortion *) param[1])->Value(); efgGame *efg = player->Game(); return new NumberPortion(efg->Payoff(outcome, player));}//----------// Player//----------static Portion *GSM_Player(GSM &, Portion **param){ if( param[0]->Spec().Type == porNULL ) { return new NullPortion( porEFPLAYER ); } Infoset *s = ((InfosetPortion *) param[0])->Value(); return new EfPlayerPortion(s->GetPlayer());}//------------// Players//------------static Portion *GSM_Players(GSM &, Portion **param){ efgGame &E = *((EfgPortion*) param[0])->Value(); return ArrayToList(E.Players());}//------------------------// PossibleNashSupports//------------------------#include "game/efgensup.h"static Portion *GSM_PossibleNashSupports(GSM &gsm, Portion **param){ efgGame &E = *((EfgPortion*) param[0])->Value(); gList<const EFSupport> list = PossibleNashSubsupports(EFSupport(E), gsm.GetStatusMonitor()); return ArrayToList(list);}//--------------// PriorAction//--------------static Portion *GSM_PriorAction(GSM &, Portion** param){ Node *n = ((NodePortion *) param[0])->Value(); efgGame *e = ((efgGame*) param[0]->Game()); const Action* a = LastAction(*e,n); if(a == 0) return new NullPortion(porACTION); return new ActionPortion((Action *)a);}//----------------// PriorSibling//----------------static Portion *GSM_PriorSibling(GSM &, Portion **param){ Node *n = ((NodePortion *) param[0])->Value()->PriorSibling(); if (!n) return new NullPortion(porNODE); return new NodePortion(n);}//-----------------// RemoveAction//-----------------static Portion *GSM_RemoveAction(GSM &, Portion **param){ EFSupport *support = ((EfSupportPortion *) param[0])->Value(); Action *action = ((ActionPortion *) param[1])->Value(); EFSupport *S = new EFSupport(*support); S->RemoveAction(action); return new EfSupportPortion(S);}static Portion *GSM_RemoveBasisAction(GSM &, Portion **param){ EFBasis *support = ((EfBasisPortion *) param[0])->Value(); Action *action = ((ActionPortion *) param[1])->Value(); EFBasis *S = new EFBasis(*support); S->RemoveAction(action); return new EfBasisPortion(S);}//-----------------// RemoveNode//-----------------static Portion *GSM_RemoveBasisNode(GSM &, Portion **param){ EFBasis *basis = ((EfBasisPortion *) param[0])->Value(); Node *node = ((NodePortion *) param[1])->Value(); EFBasis *S = new EFBasis(*basis); S->RemoveNode(node); return new EfBasisPortion(S);}//-------------// Reveal//-------------static Portion *GSM_Reveal(GSM &gsm, Portion **param){ Infoset *s = ((InfosetPortion *) param[0])->Value(); ListPortion *players = (ListPortion *) param[1]; gBlock<EFPlayer *> player(players->Length()); for (int i = 1; i <= players->Length(); i++) player[i] = ((EfPlayerPortion *) (*players)[i])->Value(); s->Game()->Reveal(s, player); gsm.UnAssignGameElement(s->Game(), true, porBEHAV | porEFSUPPORT); return new InfosetPortion(s);}//-------------// RootNode//-------------static Portion *GSM_RootNode(GSM &, Portion **param){ efgGame &E = *((EfgPortion*) param[0])->Value(); return new NodePortion(E.RootNode());}//-------------// SaveEfg//-------------extern NumberPortion _WriteGameDecimals;static Portion *GSM_SaveEfg(GSM &, Portion **param){ efgGame* E = ((EfgPortion *) param[0])->Value(); gText text = ((TextPortion *) param[1])->Value(); try { gFileOutput f(text); E->WriteEfgFile(f, _WriteGameDecimals.Value()); } catch (gFileOutput::OpenFailed &) { throw gclRuntimeError("Cannot open file " + text + " for writing"); } return param[0]->ValCopy();}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?