Initial work on implementing a network mode for communication with a server program.
This commit is contained in:
parent
50c42e17ea
commit
ac0b65ec62
49
lib/gtpsocket.py
Normal file
49
lib/gtpsocket.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
# A socket class for communicating in Go Text Protocol
|
||||
#
|
||||
# A simple connect method is defined, and everything else is
|
||||
# suffixed with _gtp, so that this can be subclassed easily,
|
||||
# e.g. for communicating with a server
|
||||
|
||||
import socket
|
||||
|
||||
class GTPSocket:
|
||||
def __init__(self, info):
|
||||
self.info = info
|
||||
self.socket = None
|
||||
|
||||
|
||||
def connect(self):
|
||||
try:
|
||||
self.socket = socket.create_connection(info)
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
|
||||
def get_gtp(self):
|
||||
msg = None
|
||||
|
||||
while msg is None:
|
||||
try:
|
||||
msg = self.socket.recv(1024)
|
||||
if not self._validate_gtp(msg):
|
||||
print 'Error: Incoming data was not a valid GTP message'
|
||||
msg = None
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def send_gtp(self, msg):
|
||||
try:
|
||||
if _validate_gtp(msg):
|
||||
self.socket.send(msg)
|
||||
return True
|
||||
else:
|
||||
print 'Error: Outgoing data was not a valid GTP message'
|
||||
return False
|
||||
except:
|
||||
return False
|
||||
|
||||
|
||||
def _validate_gtp(self, gtp):
|
||||
return True
|
51
pygo.py
51
pygo.py
|
@ -86,11 +86,14 @@ def main():
|
|||
# Build the dict of image objects
|
||||
img_res = build_img_res()
|
||||
|
||||
# Data
|
||||
gb = goban.Goban()
|
||||
network_mode = False
|
||||
our_color = None
|
||||
|
||||
board_size = 800
|
||||
board_inc = board_size / 19
|
||||
|
||||
gb = goban.Goban()
|
||||
|
||||
screen.fill((250, 250, 250))
|
||||
board = gb.draw_board(board_size, img_res)
|
||||
screen.blit(board, (0,0))
|
||||
|
@ -120,30 +123,34 @@ def main():
|
|||
if event.type == QUIT:
|
||||
return
|
||||
|
||||
# Hover a transparent stone over our
|
||||
# cursor position, assuming play is legal
|
||||
if event.type == MOUSEMOTION:
|
||||
x, y = event.pos
|
||||
row = y / board_inc
|
||||
col = x / board_inc
|
||||
|
||||
# This set of events should only be called if we can currently play
|
||||
if network_mode == False or gb.to_move == our_color:
|
||||
# Hover a transparent stone over our
|
||||
# cursor position, assuming play is legal
|
||||
if event.type == MOUSEMOTION:
|
||||
x, y = event.pos
|
||||
row = y / board_inc
|
||||
col = x / board_inc
|
||||
|
||||
if magnitude(event.rel) < 3:
|
||||
if x <= board_size:
|
||||
gb.set_hover((row,col))
|
||||
else:
|
||||
if magnitude(event.rel) < 3:
|
||||
if x <= board_size:
|
||||
gb.set_hover((row,col))
|
||||
else:
|
||||
gb.clear_hover()
|
||||
elif gb.hover != gb._real_pos((row,col)):
|
||||
gb.clear_hover()
|
||||
elif gb.hover != gb._real_pos((row,col)):
|
||||
gb.clear_hover()
|
||||
|
||||
# Place a stone on left-click
|
||||
if event.type == MOUSEBUTTONDOWN:
|
||||
x, y = event.pos
|
||||
row = y / board_inc
|
||||
col = x / board_inc
|
||||
# Place a stone on left-click
|
||||
if event.type == MOUSEBUTTONDOWN:
|
||||
x, y = event.pos
|
||||
row = y / board_inc
|
||||
col = x / board_inc
|
||||
|
||||
if x <= board_size:
|
||||
if event.button == 1:
|
||||
gb.play_move((row, col))
|
||||
|
||||
if x <= board_size:
|
||||
if event.button == 1:
|
||||
gb.play_move((row, col))
|
||||
|
||||
# if event.type == USEREVENT:
|
||||
# gb.elapsed_time += 1
|
||||
|
|
Loading…
Reference in New Issue
Block a user