from Tkinter import * from threading import Thread from time import sleep ## just used to slow down Thread function so far ## Breakdown of the FIBS Board State string (editted by don). ## source: FIBS Client Protocol Detailed Specification ## Copyright 2003-2004 Evan McLean. Revision 1.2 (October 10, 2004) ## http://www.fibs.com/fibs_interface.html#board_state ## ## board [0] ## :You:someplayer [1..2] You're name will be "You" if you are playing ## :3:0:0 [3..5] matchLen(9999=unLim), yourScore, opponentScore ## :0:-2:0:0:0:0:5:0:3:0:0:0:-5:5:0:0:0:-3:0:-5:0:0:0:0:2:0 ## [6..31] bar..bar ## :1 [32] turn is -1 if it's X's turn, 1 if it's O's turn ## :6:2:0:0 [33..36] Your dice, opponent's dice ## :1 [37] current cube value ## :1:1 [38..39] You/opponent may double if == 1 ## :0 Was doubled ## :1:-1 [41..42] Color, direction ## :0:25 ?obsolete home and bar? ## :0:0 on home for Player/opponent ## :0:0 on bar for Player/opponent ## :2 [49] How many (0-4) may be moved with current roll ## :0:0 ## :0 instant redoubles allowed in unlimited ## ## Which represents the board: ## ## +13-14-15-16-17-18-------19-20-21-22-23-24-+ X: someplayer - score: 0 ## | O X | | X O | ## | O X | | X O | ## | O X | | X | ## | O | | X | ## | O | | X | ## v| |BAR| | 3-point match ## | X | | O | ## | X | | O | ## | X O | | O | ## | X O | | O X | ## | X O | | O X | ## +12-11-10--9--8--7--------6--5--4--3--2--1-+ O: myself - score: 0 ## ## BAR: O-0 X-0 OFF: O-0 X-0 Cube: 1 You rolled 6 2. def printa(event,name): print "aaaaaaaaaaaaaaaaaa",event.widget,name def printb(event, torb, side, point): print ">>inprintb:", torb, side, point, event.widget, event.num def printdump(event, p): print " event.num(%d)" % event.num, p._dump() class Point: """ A class to store any ol' info about a point-widget, defined loosely as a container of checkers, I suppose. (bar, home, ...) """ def __init__(self, baseX, baseY, direction, type, canHold=5): self.baseX = baseX ## left corner location of widget self.baseY = baseY ## upper(lower) location of widget self.direction = direction ## -1|1 for up|down on board ##self.widget = 0 ## internal widget ID, handled exteriorly self.widgets = [] ## for holding widgets/checkers on point self.canHold = canHold ## n checkers without stacking self.number = 0 ## FIBS point number self.occupied = 0 ## -n|n for color and number of checkers self.type = type ## What am I? def _dump(self): """ util: just dump info to console """ print "x=%2d y=%2d dir=%2d canHold=%d number=%2d occupied=%2d" %( self.baseX, self.baseY, self.direction, self.canHold, self.number, self.occupied),\ "widget=%3d(%d) type(%d)" % (self.widget, len(self.widgets), self.type) class FIBSgraphics(Frame): """ This is the event driven core for the FIBS GUI. Actual telnet input is handled in Thread'ed method, connectFIBS. """ def __init__(self, master=None): Frame.__init__(self, master) self.grid() self.checkerSize = 26 ## this EVEN determines size of board ## The colors "white", "black", "red", "green", "blue", "cyan", ## "yellow", and "magenta" will always be available. For rgb's, ## ref: http://infohost.nmt.edu/tcc/help/pubs/tkinter/colors.html self.backgroundColor = "cyan" self.edgeColor = "black" self.pointColor = ("blue", "green") self.checkerColor = ("magenta", "yellow") ## The following are for the FIBS' board-state. See above comments ## on the breakdown of the FIBS Board State string or visit the html. self.color = 0 ## -1 if you are X, 1 if you are O. self._colorIndex = 41 self.direction = 0 ## -1 if you play from 24 to 1, else 1 self._directionIndex = 42 ## some utility internal variables: self._down = 1 self._up = -1 self.createWidgets() ## test threading.... change to method, connectFIBS Thread(target=self.ordfunction).start() ## method, connectFIBS def createWidgets(self): self.drawBoard() self.textWin = Text(self, height=10, width=80) self.textWin.grid() def drawBoard(self): """ size here is self.checkerSize. The methods to draw areas on the Canvas generally operate at 1/2 checkersize. This is where board area widgets are defined and stored in: self.points [24] 0..11 top ; 12..23 bottom ; both left->right self.dice [2] 0 left, 1 right side of board self.homes [4] topleft, topright, bottomleft, bottomright self.bar [2] top, bottom""" self.b = Canvas(self, height = self.checkerSize*14 - 1, width = self.checkerSize*18 - 1, background = self.backgroundColor) self.b.grid(sticky=W) ## draw top/bottom horizontals self.rectangle(0, 0, 37, 1, self.edgeColor) self.rectangle(0, 27, 37, 28, self.edgeColor) ## draw left/right verticals for v in (0, 3, 32, 35): self.rectangle(v, 1, v+1, 27, self.edgeColor) self.rectangle( 1, 13, 3, 15, self.edgeColor) ## l center area self.rectangle(33, 13, 35, 15, self.edgeColor) ## r center area ## draw bar self.rectangle(16, 1, 20, 27, self.edgeColor) ## *could* place some hinges on bar... ## draw points and point location widgets (invisible, bound to mouse) ## ***Note*** These points will NOT map directly to self.pointdata self.points = [] for topbot in (1,27): ## top, bottom for half in (2,10): ## left/right halves for point in range(6): ## each has 6 points ## no need to remember the triangle widget self.triangle(point, half, topbot, self.checkerSize) p = Point(2*(half+point), topbot, self._down if topbot == 1 else self._up, point) self.pointRectangle(p) self.points.append(p) ## bind area to mouse button def handler(event, p=p): return self._temp(event, p) self.b.tag_bind(p.widget, "