work_5
2013-11-04 03:13 heiheitian 阅读(183) 评论(0) 编辑 收藏 举报第五次作业对我个人来说是很难的,因为之前没怎么接触过这方面的内容,有幸能跟宗毅组成一队,我也仔细看了他的Python代码,因为对于Python也是第一次接触,所以我感觉在有限的时间里学会并且灵活运用还是很困难的,所以虽然付出了一些努力,还是一直处于学习的状态,没有得到想象中的结果,但是也理清了思路,实践了一些东西,还是很有收获,我相信通过我的努力一定会越来越好。下面附上代码和截图:
服务器程序:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# -*- coding: utf-8 -*- import socket import thread import random import hashlib import urllib, urlparse import time connLock = 0 def sendMsg(conn, msg): global connLock while connLock == 1: continue connLock = 1 conn.send(msg) connLock = 0 class player(): username = '' token = '' point = 0 guess = 0 conn = None timeoutCount = 0 def __init__(self, username, conn): self.username = username self.token = hashlib.md5(str(random.randint(1, 2**256))).hexdigest() self.point = 0 self.timeoutCount = 0 self.conn = conn def getToken(self): return self.token def win(self): self.point = self.point + 10 def lose(self): self.point = self.point - 5 def timeout(self): self.point = self.point - 10 self.timeoutCount = self.timeoutCount + 1 def submit(self, guess): self.guess = guess def getGuess(self): return self.guess def getUsername(self): return self.username def getPoint(self): return self.point def newRound(self, timeout, count): req = { 'cmd': 'newround', 'count': count, 'timeout': str(timeout), 'yourpoint': str(self.getPoint()) } sendMsg(conn, urllib.urlencode(req)) class game(): roundTime = 30 lastWinner = '' lastLosers = [] lastG = 0 players = [] waiting = 0 roundCount = 0 def __init__(self): self.roundTime = 5 def newRound(self): print 'users:' for i in self.players: print i.getUsername(), i.point #compute G self.waiting = 0 i = [i.getGuess() for i in self.players] if len(i) != 0: self.lastG = sum(i)*0.618/len(i) if len(self.players) > 0: minabs = abs(self.players[0].getGuess() - self.lastG) maxabs = abs(self.players[0].getGuess() - self.lastG) #calculate winner point for i in self.players[1:]: #calculate timeouts if i.getGuess == 0: i.timeout() if i.timeoutCount >= 3: players.remove(i) continue if abs(i.getGuess() - self.lastG) < minabs: self.lastWinner = i.getUsername() minabs = abs(i.getGuess() - self.lastG) if abs(i.getGuess() - self.lastG) > maxabs: maxabs = abs(i.getGuess() - self.lastG) i.guess = 0 #calculate losers self.lastLosers = [i.getUsername for i in self.players if abs(i.getGuess() - self.lastG) == maxabs] #add or minus points self.roundCount = self.roundCount + 1 for i in self.players: if i.getUsername() == self.lastWinner: i.win() elif i.getUsername() in self.lastLosers: i.lose() i.newRound(self.roundTime, self.roundCount) self.waiting = 1 def newPlayer(self, username, conn): t = player(username, conn) self.players.append(t) return t def getPara(d, p): if d.has_key(p): return d[p][0] return None def dealWithClient(conn, g): p = None while True: buf = conn.recv(1024) d = urlparse.parse_qs(buf) cmd = getPara(d, 'cmd') if (cmd == 'newplayer'): username = getPara(d, 'username') p = g.newPlayer(username, conn) req = { 'cmd': 'confirmplayer', 'username': username, 'token': p.getToken() } sendMsg(conn, urllib.urlencode(req)) elif (cmd == 'submit'): point = int(getPara(d, 'point')) print 'submit from ',p.getUsername(), 'point=',point p.submit(point) req = { 'cmd': 'confirmsubmit', 'point': point } sendMsg(conn, urllib.urlencode(req)) elif (cmd == 'query'): yourpoint = str(p.getPoint()) lastg = str(g.lastG) lastwinner = g.lastWinner req = { 'cmd': 'confirmquery', 'yourpoint': yourpoint, 'lastg': lastg, 'lastwinner': lastwinner } sendMsg(conn, urllib.urlencode(req)) def timer(g): while True: timeout = g.roundTime time.sleep(timeout) g.newRound() if __name__ == '__main__': g = game() thread.start_new_thread(timer, (g, )) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost', 9210)) sock.listen(5) while True: conn,address = sock.accept() print 'New client connected.' thread.start_new_thread(dealWithClient, (conn, g))
客户端程序:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# -*- coding: utf-8 -*- import urllib, urlparse import thread import socket import time class player: username = '' token = '' point = 0 guess = 0 conn = None timeout = 0 roundCount = 0 def __init__(self, conn): self.username = '' self.token = '' self.point = 0 self.guess = 0 self.conn = conn def submit(self, guess): req = { 'cmd': 'submit', 'point': str(guess) } self.conn.send(urllib.urlencode(req)) def query(self): req = { 'cmd': 'query' } self.conn.send(urllib.urlencode(req)) def newPlayer(self, username): req = { 'cmd': 'newplayer', 'username': username } self.conn.send(urllib.urlencode(req)) def getPara(d, p): if d.has_key(p): return d[p][0] return None def dealWithServer(conn, p): while True: buf = conn.recv(1024) d = urlparse.parse_qs(buf) cmd = getPara(d, 'cmd') if cmd == 'newround': timeout = getPara(d, 'timeout') count = getPara(d, 'count') yourpoint = getPara(d, 'yourpoint') p.timeout = int(timeout) p.roundCount = int(count) p.point = int(yourpoint) print 'newround, timeout:',timeout elif cmd == 'confirmplayer': username = getPara(d, 'username') token = getPara(d, 'token') p.username = username p.token = token print 'confirmplayer' elif cmd == 'confirmsubmit': point = getPara(d, 'point') p.guess = point print 'confirm submit' elif cmd == 'confirmquery': yourpoint = int(getPara(d, 'yourpoint')) lastg = getPara(d, 'lastg') lastwinner = getPara(d, 'lastwinner') p.point = yourpoint print 'Query result:\nyourpoint:', yourpoint, '\nlastG:', lastg, '\nlastWinner:', lastwinner, '\n' if __name__ == '__main__': sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(('localhost', 9210)) p = player(sock) thread.start_new_thread(dealWithServer, (sock, p)) while True: time.sleep(1) print '1. newplayer\n2.submit\n3.query' cmd = raw_input('choice:') if cmd == '1': username = raw_input('username:') p.newPlayer(username) if cmd == '2': point = raw_input('point:') p.submit(int(point)) if cmd == '3': p.query()
代码还在完善当中,包括用户界面,黄金点曲线等等,有了改动我会继续上传到Github和博客中。