SHARE
TWEET

halma_verify.py

a guest Feb 16th, 2013 333 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cStringIO, math, sys
  2. class Board:
  3.         start_locations = [
  4.                 [(0,0),(0,1),(0,2),(0,3),(0,4),(1,0),(1,1),
  5.                            (1,2),(1,3),(1,4),(2,0),(2,1),(2,2),
  6.                            (2,3),(3,0),(3,1),(3,2),(4,0),(4,1)],
  7.                 [(11,14),(11,15),(12,13),(12,14),(12,15),(13,12),(13,13),
  8.                                  (13,14),(13,15),(14,11),(14,12),(14,13),(14,14),
  9.                                  (14,15),(15,11),(15,12),(15,13),(15,14),(15,15)]]
  10.         adjacent = [(i,j) for i in [-1,0,1] for j in [-1,0,1] if i or j]
  11.         def __init__(self,board=None):
  12.                 if board != None:
  13.                         self.board = board
  14.                 else:
  15.                         self.board = [[-1]*16 for i in range(16)]
  16.                         for p in (0,1):
  17.                                 for (x,y) in Board.start_locations[p]:
  18.                                         self.board[x][y] = p
  19.                 self.player = 0
  20.                 self.opponent = 1
  21.         def __str__(self):
  22.                 s = cStringIO.StringIO()
  23.                 for y in range(16):
  24.                         for x in range(16):
  25.                                 s.write(['0','1','+'][self.board[x][y]])
  26.                         s.write('\n')
  27.                 v = s.getvalue()
  28.                 s.close()
  29.                 return v
  30.         def on_board(self,x,y):
  31.                 return 0<=x<16 and 0<=y<16
  32.         #Assumes move is valid
  33.         def move(self,m):
  34.                 if m != None:
  35.                         fx,fy,tx,ty = m
  36.                         self.board[tx][ty] = self.board[fx][fy]
  37.                         self.board[fx][fy] = -1
  38.                 self.player,self.opponent = self.opponent,self.player
  39.         def undo(self,m):
  40.                 self.player,self.opponent = self.opponent,self.player
  41.                 if m != None:
  42.                         fx,fy,tx,ty = m
  43.                         self.board[fx][fy] = self.board[tx][ty]
  44.                         self.board[tx][ty] = -1
  45.         #all the legal moves from the current player
  46.         def legal_moves(self, player=None):
  47.                 if player == None:
  48.                         player = self.player
  49.                 ms = [None]
  50.                 for x in range(16):
  51.                         for y in range(16):
  52.                                 if self.board[x][y] == player:
  53.                                         ms.extend(self._steps_from(x,y))
  54.                                         ms.extend(self._jumps_from(x,y))
  55.                 return ms
  56.         #the number of pieces player has in their goal
  57.         def goal_count(self, player=None):
  58.                 if player == None:
  59.                         player = self.player
  60.                 n = 0
  61.                 opponent = 1-player
  62.                 for (x,y) in Board.start_locations[opponent]:
  63.                         if self.board[x][y] == player:
  64.                                 n += 1
  65.                 return n
  66.         #return True if the game is over
  67.         def game_over(self):
  68.                 for p in (0,1):
  69.                         if self.goal_count(p) == 19:
  70.                                 return True
  71.                 return False
  72.         #verifies that a move is legal (slow)
  73.         def verify(self,m,player=None):
  74.                 return m in self.legal_moves(player)
  75.         def _steps_from(self,x,y):
  76.                 ms = []
  77.                 for (dx,dy) in Board.adjacent:
  78.                         tx,ty = x+dx, y+dy
  79.                         if self.on_board(tx,ty) and self.board[tx][ty] == -1:
  80.                                 ms.append((x,y,tx,ty))
  81.                 return ms
  82.         def _jumps_from(self,x,y,fx=None,fy=None):
  83.                 if fx == None:
  84.                         fx = x
  85.                 if fy == None:
  86.                         fy = y
  87.                 ms = []
  88.                 temp = self.board[x][y]
  89.                 self.board[x][y] = -2
  90.                 for (dx,dy) in Board.adjacent:
  91.                         mx,my = x+dx,y+dy
  92.                         tx,ty = mx+dx,my+dy
  93.                         if self.on_board(tx,ty):
  94.                                 if self.board[mx][my] >= 0 and self.board[tx][ty] == -1:
  95.                                         ms.append((fx,fy,tx,ty))
  96.                                         ms.extend(self._jumps_from(tx,ty,fx,fy))
  97.                 self.board[x][y] = temp
  98.                 return ms
  99.  
  100. board = Board()
  101. n = 0
  102. while True:
  103.         try:
  104.                 move = input()
  105.         except EOFError:
  106.                 break
  107.         if not board.verify(move):
  108.                 print 'Illegal move on line %d:\n' % (n + 1)
  109.                 print move,'player',board.player,'\n'
  110.                 print board
  111.                 sys.exit()
  112.         board.move(move)
  113.         n += 1
  114. if board.game_over():
  115.         print 'Verified at %d moves.' % n
  116. else:
  117.         print 'The game\'s not finished!'
  118.         print board
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top