Commits

Christoph Schindler committed a15a5c9

11. Dojo: Reversi Part I

Comments (0)

Files changed (3)

Reversi/reversi.py

+#!/usr/bin/env python
+# encoding: utf-8
+"""
+reversi.py
+
+Created by Coding Dojo on 2013-03-13.
+Copyright (c) 2013 __MyCompanyName__. All rights reserved.
+"""
+
+def current_player(input):
+    return input.strip()[-1]
+
+def parse(input):
+    return [line for line in input.split()[:-1]]
+
+def determine_player_positions(input):
+    """Return player positions (row, column)"""
+    player = current_player(input)
+    board = parse(input)
+    positions = []
+    # board = ["abc", "def", "ghi"]
+    for y, row in enumerate(board):
+        # (0, "abc"), (1, "def"), (2, "ghi")
+        for x, field in enumerate(row):
+            # (0, "a"), (1, "b"), (2, "c")
+            if field == player:
+                positions.append((y, x))
+    return positions
+
+def determine_attackable_enemy_positions(input, position):
+    return [
+        (3, 4),
+        (4, 3),
+    ]
+
+
+
+
+
+
+if __name__ == '__main__':
+    unittest.main()

Reversi/setup.cfg

+[nosy]
+base_path = ./
+glob_pattern = *.py
+extra_paths = setup.cfg
+options = -x --with-yanc

Reversi/test_reversi.py

+from nose.tools import *
+
+import reversi
+
+dummy_input_1 = """........
+........
+........
+...BW...
+...WB...
+........
+........
+........
+B"""
+
+player_fields_1 = [
+    (3, 3),
+    (4, 4),
+]
+
+enemy_fields_1 = [
+    (3, 4),
+    (4, 3),
+]
+
+dummy_input_2 = """........
+........
+....B...
+...BB...
+...WB...
+........
+........
+........
+W"""
+
+def test_input_return_object():
+    assert_is_not_none(reversi.parse(dummy_input_1))
+
+def test_find_current_player():
+    assert_equal(reversi.current_player(dummy_input_1), 'B')
+
+def test_find_other_current_player():
+    assert_equal(reversi.current_player(dummy_input_2), 'W')
+    
+def test_input_parses_into_list():
+    assert_equal(len(reversi.parse(dummy_input_1)), 8)
+
+def test_input_parses_into_list_of_lists():
+    result = reversi.parse(dummy_input_1)
+    for line in result:
+        assert_equal(len(line), 8)
+    
+def test_element_is_char():
+    assert_equal(reversi.parse(dummy_input_1)[0][0],'.' )
+    
+def test_determine_player_positions():
+    assert_equal(reversi.determine_player_positions(dummy_input_1),
+        player_fields_1)
+        
+def test_determine_attackable_enemy_positions():
+    assert_equal(reversi.determine_attackable_enemy_positions(dummy_input_1, (3,3)),
+        enemy_fields_1)
+        
+    
+    
+    
+