Esempio n. 1
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
def is_user_move_valid(input_move):
    if len(input_move) <= 1:
        print 'wrong input try again'
        return False

    if cm.is_check(input_move):
        print "CHECK"
        input_move = input_move[:-1]

    if cm.is_castling(input_move):
        return True

    if cm.is_special_case(input_move):
        return True

    if not cm.is_pawn(input_move) and not cm.is_main_piece(input_move):
        print 'case not valid, not a chess piece'
        return False

    if validate_eat_case(input_move):
        print 'valid eat input'
        return True

    if validate_move_case(input_move):
        print 'valid move input'
        return True

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

    if promotion:
        if not is_valid_promotion(input_move,promoted_to):
            return False
        else:
            return True

    print 'wrong input, try again'
    return False
 def test_is_check(self):
     expected = True
     test_cases = ['Bf5+','a6#']
     for input_move in test_cases:
         actual = cm.is_check(input_move)
         self.assertEqual(actual,expected)