4 def __init__(self, colour, address = None):
5 self.socket = socket.socket()
15 self.host = 'localhost' #socket.gethostname()
16 self.socket.bind((self.host, self.port))
19 self.src, self.address = self.socket.accept()
22 self.socket.connect(('localhost', self.port))
23 self.src = self.socket
27 while s[len(s)-1] != '\n':
34 class NetworkSender(Player,Network):
35 def __init__(self, base_player, board, address = None):
36 self.base_player = base_player
37 Player.__init__(self, base_player.name, base_player.colour)
38 Network.__init__(self, base_player.colour, address)
43 [x,y] = self.base_player.select()
44 choice = self.board.grid[x][y]
45 s = str(x) + " " + str(y)
46 print str(self) + ".select sends " + s
47 self.src.send(s + "\n")
51 [x,y] = self.base_player.get_move()
52 s = str(x) + " " + str(y)
53 print str(self) + ".get_move sends " + s
54 self.src.send(s + "\n")
58 self.base_player.update(s)
60 [x,y] = map(int, s[0:2])
61 selected = self.board.grid[x][y]
62 if selected != None and selected.colour == self.colour and len(s) > 2 and not "->" in s:
65 if selected.types_revealed[i] == True:
66 s += " " + str(selected.types[i])
69 print str(self) + ".update sends " + s
70 self.src.send(s + "\n")
73 def quit(self, final_result):
74 self.base_player.quit(final_result)
77 class NetworkReceiver(Player,Network):
78 def __init__(self, colour, board, address=None):
80 Player.__init__(self, address, colour)
82 Network.__init__(self, colour, address)
88 s = self.getline().strip(" \r\n")
89 return map(int,s.split(" "))
91 s = self.getline().strip(" \r\n")
92 print str(self) + ".get_move gets " + s
93 return map(int, s.split(" "))
95 def update(self, result):
97 result = result.split(" ")
98 [x,y] = map(int, result[0:2])
99 selected = self.board.grid[x][y]
100 if selected != None and selected.colour == self.colour and len(result) > 2 and not "->" in result:
101 s = self.getline().strip(" \r\n")
102 print str(self) + ".update - receives " + str(s)
104 selected.choice = int(s[2])
106 selected.types[i] = str(s[3+i])
107 if s[3+i] == "unknown":
108 selected.types_revealed[i] = False
110 selected.types_revealed[i] = True
111 selected.current_type = selected.types[selected.choice]
113 print str(self) + ".update - ignore result " + str(result)
116 def quit(self, final_result):