Example #1
0
    def test_CanPlaceStone_PlaceBlackStoneAfterWhiteMove_Passes(self):
        "Checking if user with black stones can place stone after white move"

        # Create new black stone on board
        stone = {'board': 1, 'user': 1, 'row': 0, 'col': 2, 'color': 0}
        form = StoneCreateForm(data=stone)
        self.assertTrue(form.is_valid())
Example #2
0
    def test_Coordinates_CheckNewStone_Passes(self):
        "Checking stone with new coordinates."

        # Validate stone
        stone = {'board': 1, 'user': 1, 'row': 0, 'col': 2, 'color': 0}
        form = StoneCreateForm(data=stone)
        self.assertTrue(form.is_valid())
Example #3
0
File: forms.py Project: somnam/xo
    def test_CanPlaceStone_PlaceBlackStoneAfterWhiteMove_Passes(self):
        "Checking if user with black stones can place stone after white move"

        # Create new black stone on board
        stone = { 'board': 1, 'user': 1, 'row': 0, 'col': 2, 'color': 0 }
        form  = StoneCreateForm(data=stone)
        self.assertTrue(form.is_valid())
Example #4
0
File: forms.py Project: somnam/xo
    def test_Coordinates_CheckNewStone_Passes(self):
        "Checking stone with new coordinates."

        # Validate stone
        stone = { 'board': 1, 'user': 1, 'row': 0, 'col': 2, 'color': 0 }
        form  = StoneCreateForm(data=stone)
        self.assertTrue(form.is_valid())
Example #5
0
    def test_CanPlaceStone_PlaceWhiteStoneAfterWhiteMove_RaisesException(self):
        "Checking if user with white stones can place stone after white move"

        # Validate stone
        stone = {'board': 1, 'user': 2, 'row': 0, 'col': 2, 'color': 1}
        form = StoneCreateForm(data=stone)
        self.assertFalse(form.is_valid())
        self.assertEqual(form.non_field_errors(), [u'ERR_STONE_002'])
Example #6
0
    def test_Coordinates_CheckExistingStone_RaisesException(self):
        "Checking stone with existing coordinates."

        # Validate stone
        stone = {'board': 1, 'user': 1, 'row': 0, 'col': 1, 'color': 0}
        form = StoneCreateForm(data=stone)
        self.assertFalse(form.is_valid())
        self.assertEqual(form.non_field_errors(), [u'ERR_STONE_001'])
Example #7
0
File: forms.py Project: somnam/xo
    def test_CanPlaceStone_PlaceWhiteStoneAfterWhiteMove_RaisesException(self):
        "Checking if user with white stones can place stone after white move"

        # Validate stone
        stone = { 'board': 1, 'user': 2, 'row': 0, 'col': 2, 'color': 1 }
        form  = StoneCreateForm(data=stone)
        self.assertFalse(form.is_valid())
        self.assertEqual(form.non_field_errors(), [u'ERR_STONE_002'])
Example #8
0
File: forms.py Project: somnam/xo
    def test_Coordinates_CheckExistingStone_RaisesException(self):
        "Checking stone with existing coordinates."

        # Validate stone
        stone = { 'board': 1, 'user': 1, 'row': 0, 'col': 1, 'color': 0 }
        form  = StoneCreateForm(data=stone)
        self.assertFalse(form.is_valid())
        self.assertEqual(form.non_field_errors(), [u'ERR_STONE_001'])
Example #9
0
    def test_CanPlaceStone_PlaceWhiteStoneAfterBlackMove_RaisesException(self):
        "Checking if user with white stones can place stone after black move"

        # Create new black stone on board
        stone = Stone(board_id=1, user_id=1, row=0, col=2, color=0)
        stone.save()

        # Place white stone
        stone = Stone(board_id=1, user_id=2, row=0, col=3, color=1)
        form = StoneCreateForm(data=model_to_dict(stone))
        self.assertTrue(form.is_valid())
Example #10
0
File: forms.py Project: somnam/xo
    def test_CanPlaceStone_PlaceWhiteStoneAfterBlackMove_RaisesException(self):
        "Checking if user with white stones can place stone after black move"

        # Create new black stone on board
        stone = Stone(board_id=1, user_id=1, row=0, col=2, color=0)
        stone.save()

        # Place white stone
        stone = Stone(board_id=1, user_id=2, row=0, col=3, color=1)
        form  = StoneCreateForm(data=model_to_dict(stone))
        self.assertTrue(form.is_valid())
Example #11
0
    def test_CanPlaceStone_PlaceBlackStoneAfterBlackMove_RaisesException(self):
        "Checking if user with black stones can place stone after black move"

        # Create new black stone on board
        stone = Stone(board_id=1, user_id=1, row=0, col=2, color=0)
        stone.save()

        # Place next black stone
        stone = Stone(board_id=1, user_id=1, row=0, col=3, color=0)
        form = StoneCreateForm(data=model_to_dict(stone))
        self.assertFalse(form.is_valid())
        self.assertEqual(form.non_field_errors(), [u'ERR_STONE_002'])
Example #12
0
File: forms.py Project: somnam/xo
    def test_CanPlaceStone_PlaceBlackStoneAfterBlackMove_RaisesException(self):
        "Checking if user with black stones can place stone after black move"

        # Create new black stone on board
        stone = Stone(board_id=1, user_id=1, row=0, col=2, color=0)
        stone.save()

        # Place next black stone
        stone = Stone(board_id=1, user_id=1, row=0, col=3, color=0)
        form  = StoneCreateForm(data=model_to_dict(stone))
        self.assertFalse(form.is_valid())
        self.assertEqual(form.non_field_errors(), [u'ERR_STONE_002'])
Example #13
0
File: utils.py Project: somnam/xo
def stone_update(request, game_id):
    """Update stone state with users move."""

    # Get user action performed on stone (by default assume 'add')
    action = 'add'
    if request.POST.has_key('action'):
        action = request.POST['action']

    # Get game board
    board = Board.objects.get(pk=game_id)

    # 'add' - user adds stone to board
    # 'del' - user removes stone from board
    form,stone = None,None
    if action == 'add':
        # Get first stone that isn't placed on Board
        stone = board.get_first_not_placed_stone(request.user)
        # Update stone with users move coordinates
        stone.row,stone.col = request.POST['row'],request.POST['col']
        # Create add form
        form = StoneCreateForm(
            request = request,
            data    = model_to_dict(stone),
        )
    elif action == 'del':
        try:
            # Get stone by user coordinates
            stone = board.stone_set.get(
                row = request.POST['row'],
                col = request.POST['col']
            )
        except ObjectDoesNotExist:
            pass
        else:
            # Set stone coordinates to -1, -1
            stone.row,stone.col = -1,-1
            # Create delete from
            form = StoneDeleteForm(
                request = request,
                data    = model_to_dict(stone),
            )

    # Validate stone
    if form and form.is_valid():
        # Update stone state
        stone.save()
Example #14
0
def stone_update(request, game_id):
    """Update stone state with users move."""

    # Get user action performed on stone (by default assume 'add')
    action = 'add'
    if request.POST.has_key('action'):
        action = request.POST['action']

    # Get game board
    board = Board.objects.get(pk=game_id)

    # 'add' - user adds stone to board
    # 'del' - user removes stone from board
    form, stone = None, None
    if action == 'add':
        # Get first stone that isn't placed on Board
        stone = board.get_first_not_placed_stone(request.user)
        # Update stone with users move coordinates
        stone.row, stone.col = request.POST['row'], request.POST['col']
        # Create add form
        form = StoneCreateForm(
            request=request,
            data=model_to_dict(stone),
        )
    elif action == 'del':
        try:
            # Get stone by user coordinates
            stone = board.stone_set.get(row=request.POST['row'],
                                        col=request.POST['col'])
        except ObjectDoesNotExist:
            pass
        else:
            # Set stone coordinates to -1, -1
            stone.row, stone.col = -1, -1
            # Create delete from
            form = StoneDeleteForm(
                request=request,
                data=model_to_dict(stone),
            )

    # Validate stone
    if form and form.is_valid():
        # Update stone state
        stone.save()