def win(game,winner,timeup=False): """informs players that the game is over and updates their scores""" game.updatePlayers() #scores pls=[getPlayer(game.player0), getPlayer(game.player1)] if all(pls): loser=1-winner dScore=int(pls[loser].score/20) pls[winner].score+=dScore pls[loser].score-=dScore for pl in pls:pls[pl].put()
def __init__(self, cid, cowner, croom): 'croom accepts room object or croomid(str)' croom = rooms.getRoom(croom) cowner = players.getPlayer(cowner) croomid = croom.rid if cid in croom.cards: raise CardInUseError('Card cid: ' + cid + ' already in use.') else: croom.cards[cid] = self cowner.cards[cid] = self if cowner.proom == None or cowner.proom.rid != croom.rid: if cowner.proom == None: raise PlayerNotInRoomError( 'Player pid ' + cowner.pid + ' is not in any rome but the current room is rid ' + croom.rid) else: raise PlayerNotInRoomError('Player pid ' + cowner.pid + ' is in room rid ' + cowner.proom.rid + ' but the current room is rid ' + croom.rid) self.cid = cid self.ccolour, self.ctype, self.cnumber, self.cdescription = analyseCard( cid) self.cowner = cowner self.cownerid = cowner.pid self.croomid = croomid self.croom = croom croom.cards_not_used.remove(cid)
def get(self,gpath): """Shows a page on which a game can be played""" pl = getCurrentPlayer() GAME=GAMES[gpath] gmNum=self.request.get('gameID') gm=GAME.get_by_key_name(gmNum) if not gm: self.redirect("/") return None if not gm.state&STARTED: logging.error("accessing a game which has not started") crash if pl: tvals = {"player":pl.playername, "chtoken":pl.token} if gm.player0==pl.account: playern=0;opp=gm.player1 elif gm.player1==pl.account: playern=1;opp=gm.player0 else:return None else: pnum=self.request.get("playerID") if gm.player0.email()=="p "+pnum: playern=0;opp=gm.player1 elif gm.player1.email()=="p "+pnum: playern=1;opp=gm.player0 else:return None tvals = {"player":"You","playerID":pnum} if opp.email().startswith("p "): tvals["opponent"]="Opponent" elif gm.state&AIP: tvals["opponent"]="AI" else: tvals["opponent"]=getPlayer(opp).playername tvals["playern"]=playern tvals["player0"]=tvals["opponent"] if playern else tvals["player"] tvals["player1"]=tvals["player"] if playern else tvals["opponent"] pagetype=self.request.get("pageType") if not pagetype:pagetype=gm.views[0] tvals["gameID"]=gmNum tvals["data"]=sjd(gm.getData()) tvals["path"]=[("/","all games"), ("",pagetype+" view")] tvals["links"]=[("?pageType="+ptype+"&gameID="+gmNum+ ("" if pl else "&playerID="+pnum), ptype+" view") for ptype in gm.views if ptype!=pagetype] load(self,"/%s/%s.html"%(gpath,pagetype),tvals)
def post(self,gpath): pl=getCurrentPlayer() if not pl:return None GAME=GAMES[gpath] kname=self.request.get("gameID") gm=GAME.get_by_key_name(kname) if not gm or gm.state&STARTED: self.response.out.write("no") return None check(pl.account,gm.player1,"player replying to a game which they are not part of") answer=self.request.get("answer") assert answer in ["yes","no"] message={"request":"reply","answer":answer} p0=getPlayer(gm.player0) if p0: p0.send(message) if answer=="yes": gm.state|=STARTED gm.lastTurn=datetime.now() gm.put() if answer=="no": db.delete(gm)
MinLosses = 0 RewardWin = .5 NRounds0 = 100 #max([10, MinLosses]) KRounds = 1.0 MaxNRounds = 100 ThreshOpp = 10 Sigma0 = .1 NRoundsBenchmark = 100 NSeeds = MaxNRounds OneMinute = 60 OneHour = 60*OneMinute MaxRunningTime = 411 * OneMinute # int(1000 * OneHour) # was 10 hhours TimeBetweenBenchmarks = 10*OneMinute Game = TicTacToe Opponent = players.getPlayer(opp) CMAOpts = {} if outFn!="x.json" and os.path.exists(outFn): raise Exception("Output path [%s] exists!" % outFn) nh = [18,18] npf = NNPlayerFactory(nh = nh) print ("PARAMS: NRounds0 = %d nWorkers = %d nh = %s MinLosses = %s" % ( NRounds0, nWorkers, nh, MinLosses)) print ("PARAMS: Sigma0 = %.4e numParams = %d RewardWin = %.4f" % (Sigma0, npf.numParams(), RewardWin)) print ("PARAMS: Opponent = %s NSeeds = %s" % (str(Opponent), NSeeds)) print ("PARAMS: CMAOpts = %s" % (CMAOpts)) es = cma.CMAEvolutionStrategy([0] * npf.numParams(), Sigma0, CMAOpts)