"basic multiuser gameplay possible. Milestone m6 reached!
This commit is contained in:
parent
e7e9c72e54
commit
8f5568bcd0
4 changed files with 72 additions and 24 deletions
|
@ -29,8 +29,6 @@ m5: goban inside database
|
|||
on side of the board, so that gobans of size 9x9, 13x13 and 19x19 can be
|
||||
displayed.
|
||||
|
||||
. <--- YOU ARE HERE.
|
||||
|
||||
m6: manage users and game sessions inside database
|
||||
manage user accounts and the game(s) they currently play inside the data-
|
||||
base. each game has its own table, which contains players, current turn,
|
||||
|
@ -40,6 +38,8 @@ m6: manage users and game sessions inside database
|
|||
username and password and is then given the choice of game to play. If
|
||||
there is only one active game, directly present the goban.
|
||||
|
||||
. <--- YOU ARE HERE.
|
||||
|
||||
m7: implement Go rules
|
||||
Do not accept player moves which violate go rules. Remove beaten stones.
|
||||
Count and display the score for each player.
|
||||
|
|
18
goban.py
18
goban.py
|
@ -88,6 +88,7 @@ def display_goban(goban,req,form):
|
|||
data += hoshifield
|
||||
else:
|
||||
data += defaultfield
|
||||
data += '\n<input type="hidden" name="play" value="foo">'
|
||||
data += '\n</form>'
|
||||
return data
|
||||
|
||||
|
@ -98,7 +99,7 @@ def display_goban(goban,req,form):
|
|||
|
||||
|
||||
|
||||
def process_form(goban):
|
||||
def process_form(req,form,goban):
|
||||
"""
|
||||
gets a goban dictionary.
|
||||
|
||||
|
@ -106,12 +107,13 @@ def process_form(goban):
|
|||
if the goban has been clicked, return a (x,y) tuple of the position.
|
||||
|
||||
"""
|
||||
#get instance of fieldstorage
|
||||
form = cgi.FieldStorage()
|
||||
#if form == empty (which means if page is displayed for the first time):
|
||||
if form.keys() != []:
|
||||
#cut out the name of the clicked button
|
||||
namestring = string.split(form.keys()[0],".x")[0]
|
||||
for item in form.keys():
|
||||
if string.find(item,").x")>0:
|
||||
namestring = string.split(item,".x")[0]
|
||||
|
||||
position = helper.string_to_tuple(namestring)
|
||||
ret = set_stone(goban, position)
|
||||
if (type(ret) == type("")):
|
||||
|
@ -125,7 +127,7 @@ def set_stone(goban, position):
|
|||
"""gets a goban dictionary and a (x,y)-tuple. Returns a modified goban."""
|
||||
turn = goban["turn_number"]
|
||||
if (goban[position] == 0): #empty field
|
||||
goban[position] = (turn % 2) + 1 #even turn: white tone (1), else black stone(2)
|
||||
goban[position] = (turn % 2) + 1 #even turn: white stone (1), else black stone(2)
|
||||
goban["turn_number"] += 1
|
||||
#now write changed values to database
|
||||
psql.update_goban_field(goban["name"],position[0],position[1],(turn % 2) + 1)
|
||||
|
@ -150,11 +152,11 @@ def main(gamename):
|
|||
goban = psql.fetchall_list_to_goban_dict(tmplist)
|
||||
#print goban
|
||||
|
||||
(goban,str) = process_form(goban)
|
||||
(goban,retstring) = process_form(goban)
|
||||
data += "<p> Turn number: %s, %s Player's Move.</p>" % (goban["turn_number"],("White","Black")[goban["turn_number"] % 2]) #eleet ;>
|
||||
data += display_goban(goban)
|
||||
if str != "":
|
||||
data +="\n<p>"+str+"</p>\n"
|
||||
if retstring != "":
|
||||
data +="\n<p>"+retstring+"</p>\n"
|
||||
|
||||
|
||||
data += helper.footer()
|
||||
|
|
7
main.py
7
main.py
|
@ -30,11 +30,6 @@ def handler(req):
|
|||
sessionid=""
|
||||
username=""
|
||||
|
||||
foundx = False
|
||||
for item in form.keys():
|
||||
if string.find(item,").x") > 0:
|
||||
foundx = True
|
||||
|
||||
#if sessionid=sesssionid and time < timeout:
|
||||
# set_timeout
|
||||
|
||||
|
@ -46,7 +41,7 @@ def handler(req):
|
|||
elif "delete" in form.keys():
|
||||
deletegame = apache.import_module("deletegame")
|
||||
deletegame.main(req,form)
|
||||
elif ("play" in form.keys()) or foundx:
|
||||
elif ("play" in form.keys()):
|
||||
playgame = apache.import_module("playgame")
|
||||
playgame.main(req,form)
|
||||
else:
|
||||
|
|
61
playgame.py
61
playgame.py
|
@ -1,4 +1,39 @@
|
|||
import goban,helper,psql
|
||||
import string
|
||||
|
||||
def is_my_turn(req,form,gobandict):
|
||||
"""
|
||||
gets request and util.FiedStorage form.
|
||||
check wether or not the current this is the players turn.
|
||||
return true or false.
|
||||
"""
|
||||
|
||||
#INFO: player1 is black,player2 is white. black starts the game.
|
||||
me = form["username"]
|
||||
player1 = gobandict["player1"]
|
||||
player2 = gobandict["player2"]
|
||||
|
||||
#get turn_number.
|
||||
turn_number = gobandict["turn_number"]
|
||||
#if turn_number modulo 2 == 0: are we player two?
|
||||
# yes:return True, else return False
|
||||
if turn_number % 2 == 0:
|
||||
if me == player2:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
#else:are we player1?
|
||||
# yes:return True, else return False
|
||||
else:
|
||||
if me == player1:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def main(req,form):
|
||||
"""
|
||||
|
@ -11,22 +46,38 @@ def main(req,form):
|
|||
except:
|
||||
gamename = ""
|
||||
if gamename != "":
|
||||
#do stuff
|
||||
data = helper.header()
|
||||
|
||||
#read goban table from database
|
||||
tmplist = psql.read_table(gamename)
|
||||
#make a dictionary out of the list
|
||||
gobandict = psql.fetchall_list_to_goban_dict(tmplist)
|
||||
#print goban
|
||||
#data += "<p> Turn number: %s, %s Player's Move.</p>" % (gobandict["turn_number"],("White","Black")[gobandict["turn_number"] % 2]) #eleet ;>
|
||||
#check if user has already clicked onto a field:
|
||||
foundx = False
|
||||
for item in form.keys():
|
||||
if string.find(item,").x") > 0:
|
||||
foundx = True
|
||||
if foundx:
|
||||
if is_my_turn(req,form,gobandict):
|
||||
(gobandict,retstring) = goban.process_form(req,form,gobandict)
|
||||
else:
|
||||
pass
|
||||
|
||||
#do stuff
|
||||
data = helper.header()
|
||||
|
||||
|
||||
data += "Turn number: "+str(gobandict["turn_number"])+". "
|
||||
|
||||
#check whether its our turn
|
||||
#if yes: print 'your move' and display goban and process move
|
||||
#if not: print '...s move' and display goban.
|
||||
if is_my_turn(req,form,gobandict):
|
||||
data += ("Its your turn.<br>")
|
||||
else:
|
||||
data+= ("This is not your turn. Look, but don't touch ;)<br>")
|
||||
|
||||
|
||||
|
||||
#print goban
|
||||
data += goban.display_goban(gobandict,req,form)
|
||||
data += helper.footer()
|
||||
req.write(data)
|
||||
|
|
Loading…
Reference in a new issue