this is a german Web-Mirror of PYTHON.ORG powered by Domainunion AG

Attachment 'marijn_guyon_tdd_dojo.py'

Download

   1 from nose.tools import assert_equals
   2 
   3 
   4 def legal_moves(board, player):
   5     """
   6     Given a board encoded as a list of strings and a player char
   7     returns pairs identifying positions of legal moves for given player
   8     and given situation on the board.
   9     """
  10     result = []
  11     b = board2dict(board)
  12     pieces = find_color(b, player)
  13     for piece in pieces:
  14         for neighbour in  find_different_neighbours(b, piece):
  15             vd = valid_direction(b, piece, neighbour)
  16             if not vd: continue
  17             result.append(vd)
  18     return sorted(set(result))
  19 
  20 TEST_BOARD = ["....",
  21               ".bw.",
  22               ".wb.",
  23               "...."]
  24 
  25 CASE1 = [     "....",
  26               ".w..",
  27               "bww.",
  28               "wb.."]
  29 
  30 CASE2 = [     ".wb",
  31               "w..",
  32               "b.."]
  33 
  34 CASE3 = [     ".wb.",
  35               ".bw."]
  36 
  37 CASE4 = [     ".bbb",
  38               ".bwb",
  39               ".bbb"]
  40 
  41 def valid_direction(board, piece, neighbour):
  42     r1, c1 = piece
  43     r2, c2 = neighbour
  44     
  45     diff1, diff2 = r2-r1, c2-c1    
  46     r, c = r2, c2    
  47     color = board[piece]
  48     
  49     while True:
  50         r += diff1
  51         c += diff2        
  52         target = r,c
  53         
  54         try:
  55             if board[target] == '.':
  56                 return target
  57             if board[target] == color:
  58                 return
  59         except KeyError:
  60             return
  61         
  62         
  63 
  64 def find_color(board, find_color):
  65     results = []
  66     for piece, color in board.items():
  67         if color == find_color:
  68             results.append(piece)
  69     results.sort()
  70     return results
  71 
  72 def board2dict(board):
  73     d = {}
  74     for r, row in enumerate(board):
  75         for c, col in enumerate(row):
  76             d[(r,c)] = col
  77     return d
  78 
  79 def find_different_neighbours(board,  piece):
  80     results = []
  81     
  82     d = board
  83     color = negate_color(d[piece])
  84     
  85     r, c = piece
  86     
  87     for y in (r-1, r, r+1):
  88         for x in (c-1, c, c+1):
  89             if (r,c) == (y, x): continue
  90             
  91             try:
  92                 if d[y, x] == color:
  93                     results.append((y, x))
  94             except KeyError:
  95                 continue
  96                 
  97     return results
  98 
  99 def negate_color(color):
 100     if color == 'w':
 101         return 'b'
 102     return 'w'
 103 
 104 def test_simple():
 105     assert_equals(legal_moves(TEST_BOARD, "w"), [(0, 1), (1, 0), (2, 3), (3, 2)])
 106 
 107 def test_find_color():
 108     assert_equals(find_color(board2dict(TEST_BOARD), 'w'), [(1,2), (2,1)])
 109     assert_equals(find_color(board2dict(TEST_BOARD), 'b'), [(1,1), (2,2)])
 110 
 111 def test_find_different_neighbours():
 112     assert_equals(find_different_neighbours(board2dict(TEST_BOARD), (1, 1)), [(1,2), (2,1)])
 113 
 114 def test_negate_color():
 115     assert_equals(negate_color('w'), 'b')
 116     assert_equals(negate_color('b'), 'w')
 117     
 118 def test_board2dict():
 119     test_board = [".b", 
 120                   "w."]
 121     expected_dict = {
 122         (0,0): '.',
 123         (0,1): 'b',
 124         (1,0): 'w',
 125         (1,1): '.'
 126     }
 127     assert_equals(board2dict(test_board), expected_dict)
 128     
 129 def test_valid_direction():
 130     test_board1 = ["..bbw"]
 131     assert_equals(valid_direction(board2dict(test_board1), (0,4), (0,3)), (0,1))
 132     
 133     test_board1 = ["..bbbbw"]
 134     assert_equals(valid_direction(board2dict(test_board1), (0,6), (0,5)), (0,1))
 135     
 136     test_board1 = ["..bw"]
 137     assert_equals(valid_direction(board2dict(test_board1), (0,3), (0,2)), (0,1))
 138     
 139     assert_equals(valid_direction(board2dict(TEST_BOARD), (2,2), (2,1)), (2,0))
 140     
 141     assert_equals(valid_direction(board2dict(TEST_BOARD), (1,1), (1,2)), (1,3))
 142     
 143     assert_equals(valid_direction(board2dict(TEST_BOARD), (1,1), (2,1)), (3,1))
 144     
 145     assert_equals(valid_direction(board2dict(TEST_BOARD), (2,2), (1,2)), (0,2))
 146     
 147 def test_case1():
 148     assert_equals(legal_moves(CASE1, "b"), [(0,1),(0,2),(1,3),(2,3)])
 149     
 150 def test_case2():
 151     assert_equals(legal_moves(CASE2, "b"), [(0,0)])
 152     
 153 def test_case3():
 154     assert_equals(legal_moves(CASE3, "b"), [(0,0), (1,3)])
 155     
 156 def test_case4():
 157     assert_equals(legal_moves(CASE4, "b"), [])

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2010-04-22 10:37:12, 2.2 KB) [[attachment:konrad_todd_tdd_dojo.py]]
  • [get | view] (2010-04-22 10:35:07, 4.1 KB) [[attachment:marijn_guyon_tdd_dojo.py]]
  • [get | view] (2010-04-21 15:53:39, 150.7 KB) [[attachment:presentation.pdf]]
  • [get | view] (2010-04-22 12:13:37, 1.3 KB) [[attachment:todd_gijs_reversi.py]]
  • [get | view] (2010-04-22 12:14:08, 1.8 KB) [[attachment:todd_gijs_test.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

Unable to edit the page? See the FrontPage for instructions.