copyright = """domino.py - Play puertorrican dominos Copyright (C) 2001 - Humberto Ortiz Zuazaga This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. """ import random def generate(): "Make all dominos" dominos = [] for i in range(7): for j in range(i,7): dominos.append((i,j)) return dominos def shuffle(ls): "Shuffle a list, returns a copy" # First, make a copy to mangle. newlist = ls[:] # Make an empty list to accumulate into retlist = [] # Pop random elements into retlist, deleting from newlist while (len(newlist) > 0): i = random.randint(0,len(newlist)-1) item = newlist.pop(i) retlist.append(item) return retlist def play(hand, board): "Choose and play a bone" # Find playable dominos ps = playable(hand, board) if len(ps) == 0: # We have to pass print "tap, tap" else: doplay(ps, hand, board) def playable(hand, board): "Return a list of legal plays" if hand == []: return [] if board == []: if (6,6) in hand: return [(6,6)] else: return [] ps = [] for chip in hand: front, back = chip if (front in board[0:2]) or (back in board[0:2]): ps.append(chip) return ps def doplay(ps, hand, board): "Execute one of a list of plays" # This should choose the best move, instead of the first which = ps[0] print "Play:", which hand.remove(which) front, back = which if board == []: board.append(front) board.append(back) board.append([which]) else: if front == board[0]: board[0] = back board[2].insert(0,which) elif front == board[1]: board[1] = back board[2].append(which) elif back == board[0]: board[0] = front board[2].insert(0,which) elif back == board[1]: board[1] = front board[2].append(which) if __name__ == '__main__': print copyright dominoes = generate() newlist = shuffle(dominoes) hands = list(range(4)) for i in range(4): hands[i] = newlist[:7] del newlist[:7] print hands[i] print newlist board = [] for i in range(7): for hand in hands: play(hand, board) print "Hand:", hand print "Board:", board