webgo/old_functions.py
2005-09-06 17:52:46 +00:00

51 lines
1.5 KiB
Python
Executable file

def init_goban(size=9):
"""
Returns an empty goban in the form of a dictionary where the keys are
x,y-tuples: (1,1) is the upper left corner, (9,9) the lower right one
for a 9x9 goban.
The additional key named size contains the length of a side of the goban.
Content of a field:
0 == empty field
1 == white stone on field
2 == black stone on field
Field turn_number: Current turn to be played. Even: white, uneven: black.
Optional argument size: define length of a side of the goban (one of
9, 12 or 19). Defaults to 9.
Returns dictionary.
"""
goban = {}
for x in range(1,size+1):
for y in range(1,size+1):
goban[(x,y)] = 0
if (size == 9) or (size == 13) or (size == 19):
goban["size"] = size
else:
goban["size"] = 9
goban["turn_number"] = 1
#some stones for testing purposes
goban[(1,2)] = 1; goban[(2,1)] = 1; goban[(2,3)] = 1; goban[(3,3)] = 1; goban[(6,7)] = 1; goban[(6,9)] = 1
goban[(2,2)] = 2; goban[(1,3)] = 2; goban[(4,7)] = 2; goban[(4,9)] = 2; goban[(5,8)] = 2; goban[(7,9)] = 2
return goban
def save_goban(goban):
"""gets a goban dictionary and pickles it to a file.
"""
try:
f = open(picklefile,"w")
pickle.dump(goban,f)
f.close()
except:
print "error: could not write to file "+picklefile+"!"
def load_goban():
"""trys to load and return a pickled goban. If this does not work return
a newly initialized goban."""
try:
f = open(picklefile,"r")
return pickle.load(f)
f.close() #datei ist jetzt genullt
except:
return init_goban()