Permalink
Browse files

Implement writing of files, __str__ and __repr__ for games

Implement the following:
- is_tree property for games
- write() member for games, returning a string containing the
  .efg or .nfg format savefile
- __str__ for games returns game title
- __repr__ for games returns the .efg or .nfg format savefile,
  based on whether the game is natively a tree or table.
  • Loading branch information...
1 parent 0384e2a commit c2f99855b54762b81c256e8bc20bc91421a5550c @tturocy tturocy committed Mar 25, 2010
Showing with 34 additions and 5 deletions.
  1. +14 −2 src/python/gambit/lib/game.wrap.h
  2. +20 −3 src/python/gambit/lib/game.wrap.pyx
@@ -6,6 +6,7 @@
#include <string>
#include <fstream>
+#include <sstream>
#include "libgambit/libgambit.h"
using namespace std;
@@ -16,5 +17,16 @@ Game ReadGame(char *fn) throw (InvalidFileException)
std::ifstream f(fn);
return Gambit::ReadGame(f);
}
-
-
+
+std::string WriteGame(const Game &p_game, bool strategic)
+{
+ std::ostringstream f;
+ if (strategic) {
+ p_game->WriteNfgFile(f);
+ }
+ else {
+ p_game->WriteEfgFile(f);
+ }
+ return f.str();
+}
+
@@ -9,8 +9,11 @@ cdef extern from "string":
cdef extern from "libgambit/game.h":
ctypedef struct c_GameRep "GameRep":
+ int IsTree()
+
cxx_string GetTitle()
void SetTitle(cxx_string)
+
int NumNodes()
ctypedef struct c_Game "GameObjectPtr<GameRep>":
@@ -20,15 +23,20 @@ cdef extern from "libgambit/game.h":
cdef extern from "game.wrap.h":
c_Game ReadGame(char *) except +IOError
+ cxx_string WriteGame(c_Game, int) except +IOError
cdef class Game:
cdef c_Game game
+ def __str__(self):
+ return "<Game '%s'>" % self.title
+
def __repr__(self):
- return "<This is a Game!>"
+ return self.write()
- def num_nodes(self):
- return self.game.deref().NumNodes()
+ property is_tree:
+ def __get__(self):
+ return True if self.game.deref().IsTree() != 0 else False
property title:
def __get__(self):
@@ -38,6 +46,15 @@ cdef class Game:
s.assign(value)
self.game.deref().SetTitle(s)
+ def num_nodes(self):
+ return self.game.deref().NumNodes()
+
+ def write(self, strategic=False):
+ if strategic or not self.is_tree:
+ return WriteGame(self.game, 1).c_str()
+ else:
+ return WriteGame(self.game, 0).c_str()
+
def new_tree():
cdef Game g

0 comments on commit c2f9985

Please sign in to comment.