othello.html
001|
002| from sys import argv
003|
004| ROWS,COLS = 8,8
005| BSIZE = (ROWS, COLS)
006| Vecs = [(-1,0),(-1,+1),(0,+1),(+1,+1),(+1,0),(+1,-1),(0,-1),(-1,-1)]
007|
008|
009| class BoardCellState (object):
010| states = ['W', 'B']
011|
012| def __init__(self, state_index):
013| self.state = state_index
014| self.aspect = self.states[self.state]
015|
016| def __call__(self):
017| return self.state
018|
019| def __str__(self):
020| return self.aspect
021|
022| def toggle(self):
023| self.state += 1
024| if len(self.states) <= self.state:
025| self.state = 0
026| self.aspect = self.states[self.state]
027|
028| class BoardCell (object):
029| def __init__(self, row, col):
030| self.row = row
031| self.col = col
032| self.cid = 1 + ((row*COLS)+col)
033| self.color = ((col+1) % 2) if (row % 2) else (col % 2)
034| self.piece = None
035|
036| def __call__(self):
037| return self.piece
038|
039| def __str__(self):
040| return str(self.piece) if self.piece else ' '
041|
042| def __repr__(self):
043| t = (self.cid, self.row, self.col, str(self))
044| s = '{BoardCell:{id:%d, row:%d,col:%d, state:"%s"}}'
045| return s % t
046|
047| def place_piece(self, piece):
048| self.piece = piece
049|
050| def toggle_piece(self):
051| self.piece.toggle()
052|
053| class Othello (object):
054| hr = '+'+('---+' * COLS)
055|
056| def __init__(self):
057| self.board = [[BoardCell(r,c) for c in range(COLS)] for r in range(ROWS)]
058| self(3,3).place_piece(BoardCellState(0))
059| self(3,4).place_piece(BoardCellState(1))
060| self(4,3).place_piece(BoardCellState(1))
061| self(4,4).place_piece(BoardCellState(0))
062| self.turn = BoardCellState(0)
063| self.verbose = False
064|
065| def __call__(self, row, col):
066| return self.board[row][col]
067|
068| def __str__(self):
069| a = [self.hr]
070| for row in self.board:
071| t = [' %s ' % str(cell) for cell in row]
072| s = '|'.join(t)
073| a.append('|%s|' % s)
074| a.append(self.hr)
075| s = 'Othello:\n%s\n\n' % '\n'.join(a)
076| return s
077|
078| def traveler(self, callback):
079| for row in self.board:
080| for cell in row:
081| callback(cell)
082|
083| def make_move(self, row, col):
084| self.get_rays(row,col)
085| self.test_rays()
086| if len(self.caps):
087| self(row,col).place_piece(BoardCellState(self.turn()))
088| for r in self.caps:
089| for c in r:
090| c().toggle()
091| self.turn.toggle()
092| return 0 < len(self.caps)
093|
094| def get_rays(self, row, col):
095| self.rays = []
096| for v in Vecs:
097| d = [(row+(v[0]*ix), col+(v[1]*ix)) for ix in range(1,max(BSIZE))]
098| d = [t for t in d if (0 <= min(t)) and (t[0] < ROWS) and (t[1] < COLS)]
099| d = [self(t[0],t[1]) for t in d]
100| self.rays.append(d)
101| return self.rays
102|
103| def test_rays(self):
104| self.caps = []
105| for ray in self.rays:
106| if len(ray) == 0: continue
107| if not ray[0](): continue
108| p = ray[0]()
109| if p() == self.turn(): continue
110| a = []
111| for cell in ray:
112| if not cell(): a = []; break
113| if cell()() == self.turn(): break
114| a.append(cell)
115| if len(a): self.caps.append(a)
116| return self.caps
117|
118| def print_rays(self):
119| print('Rays for new move:')
120| for ix,ray in enumerate(self.rays):
121| print('Ray[%d] %s' % (ix, str(Vecs[ix])))
122| for cell in ray:
123| print(repr(cell))
124| print()
125|
126| def print_caps(self):
127| print('Rays with captures:')
128| for ix,ray in enumerate(self.caps):
129| print('Cap[%d] (captures: %d)' % (ix, len(ray)))
130| for cell in ray:
131| print(repr(cell))
132| print()
133|
134|
135|
136|
137| def main(*args):
138| b = Othello()
139| b.make_move(4,2)
140| b.make_move(5,2)
141| print(b)
142| return b
143|
144| if __name__ == '__main__':
145| obj = main(*argv[1:])
146|
147| '''eof'''
148|