def test_is_user_move_valid(self):
        expected = True
        k = 1
        for input_move in ['a2','axb2','Bf3','Bxa2']:
            actual = validator.is_user_move_valid(input_move)
            self.assertEqual(expected, actual, msg='Error on test # %i: %s' % (k, input_move))
            k += 1

        expected = False
        k = 1
        for input_move  in ['ax2','B3','Abcd']:
            #print input_move, actual
            actual = validator.is_user_move_valid(input_move)
            self.assertEqual(expected, actual, msg='Error on test # %i: %s'  %(k,input_move) )
            k += 1
Esempio n. 2
0
    def parse_user_move(self, input_move):
        # TODO: Handle draws (50+ moves without exchange, or same state repeated 3 times)

        input_move = input_move.strip()

        if self.has_quit(input_move):
            return False

        if len(input_move)>1 and cm.is_check(input_move):
            input_move = input_move[:-1]

        if not validator.is_user_move_valid(input_move):
            return True

        promotion,input_move,promoted_to = cm.is_promotion(input_move)

        move_to_col,move_to_line,col_filter,line_filter = input_parser.parse_coordinates(input_move)
        
        parallel_board = copy.deepcopy(self.board)
        accepted_move = self.move_piece_to(input_move, move_to_col, move_to_line, col_filter, line_filter)

        if promotion:
            self.board.promote( move_to_col, move_to_line, self.player, promoted_to )

        if self.board.Rules.is_king_under_attack(self.board,self.player):
            print "Cannot leave %s player in check!"%self.player
            accepted_move = False
            self.board = parallel_board

        if accepted_move:
            self.print_move(input_move, move_to_col, move_to_line)

            if self.is_match() and self.hit_endgame():
                return False
            else:
                self.switch_player()
        
        if cm.is_special_case(input_move):
                self.restart()
        return True